将 Android ListView onClick 连接到另一个 Activity

Connecting an Android ListView onClick to another Activity

我正在制作一个 MP3 播放器 Android 应用程序。我想通过 ListView 单击打开艺术家目录以显示该艺术家的歌曲来启动另一个 activity。我的代码基于 [Android 教程][1]。

下面是我在 ArtistList.java

中的 onCreate
    ListView lv = getListView();
    lv.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View v,int position, long id) {

            Intent intent = new Intent(getApplicationContext(), SongList.class);

            String artistSongPath = artistsList.get(position).get("artistPath");//gets artist SDcard path

            System.out.println("artistSongPath = " + artistSongPath); //prints out correct path

            intent.putExtra("A_S_PATH", artistSongPath); //puts the artistSongPath static A_S_Path

            startActivity(intent); //start the intent
        }
    });

在我的ArtistList.java顶部我有

    public final static String A_S_PATH = "wecode.mp3playerapp.MESSAGE";

在我的 SongList.java

 @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.song_list);

    Intent intent = getIntent();
    String path = intent.getStringExtra(ArtistList.A_S_PATH) + "/";
    System.out.println(path);
    try {
        File fp = new File(path);
        findArtistSongs(fp);
    }catch(NullPointerException npe){
        System.out.println("Could not find path.");
    }

}

A_S_Path 为空,我没有进入我的陷阱。请帮忙!这是我的第一个应用!!

改变这个

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.song_list);

    Intent intent = getIntent();
    String path = intent.getStringExtra("A_S_PATH") + "/"; //here is the line that I changed
    System.out.println(path);
    try {
        File fp = new File(path);
        findArtistSongs(fp);
    }catch(NullPointerException npe){
        System.out.println("Could not find path.");
    }

在你的 ArtistList.java 你有:

intent.putExtra("A_S_PATH", artistSongPath);

但在 SongList.java 中,您有:

String path = intent.getStringExtra(ArtistList.A_S_PATH) + "/";

你注意到"A_S_PATH"和你定义的ArtistList.A_S_PATH不一样了吗?