我正在制作一个MP3播放器Android应用程序。我想从 ListView 单击开始另一个活动,该单击打开艺术家目录以显示该艺术家的歌曲。我的代码基于[Android教程][1]。
以下是我的在艺术家列表中创建.java
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
}
});
在我的艺术家列表的顶部.java我有
public final static String A_S_PATH = "wecode.mp3playerapp.MESSAGE";
在我的歌单中.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.");
}
在您的艺术家列表中.java您有:
intent.putExtra("A_S_PATH", artistSongPath);
但是在SongList.java中,你有:
String path = intent.getStringExtra(ArtistList.A_S_PATH) + "/";
您是否注意到"A_S_PATH"与您定义的ArtistList.A_S_PATH不同?