ListView 和 ArrayAdapter 的 NullPointerException
NullPointerException with ListView and ArrayAdapter
当我 select 我的 main.java 中的 btnPlaylist 时,我在按钮处收到了错误代码。我不知道我做错了什么。它说我需要一个带有 R.id.list 的 ListView。我假设它指的是我的 playlist_item_row.xml 但我试过了并继续清理我的项目。
public class PlayListActivity extends ListActivity {
ListView listView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.playlist);
listView = (ListView)findViewById(R.id.listView);
//double check that it's list_item
String[] songs = this.getResources().getStringArray(R.array.TrackNames);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.playlist_item_row,R.id.song_title,songs);
listView.setAdapter(adapter);
}
}
这是 PlayListActivity.java
的 xml 调用
playlist.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="#242424"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_selector" />
这个xml我坚持我的价值观
资源 xml in res>values>'track_array.xml'
<resources>
<string-array name="TrackNames">
<item>100 - Welcome to London</item>
<item>102 - Trafalgarsquare</item>
</string-array>
</resources>
playlist_item_row.xml
<TextView
android:id="@+id/song_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:padding="10dp" />
main.java
private MediaPlayer mp;
private MediaPlayer nextMp;
private TextView tvTime;
private long currentTime;
private long duration;
private boolean isPlaying;
private boolean mCompatMode = true;
private MediaPlayer mNextPlayer;
private OnCompletionListener mCompletion;
private ImageButton btnPlay;
private ImageButton btnPlaylist;
private SeekBar songProgressBar;
private TextView songTitleLabel;
//private Handler mHandler = new Handler(); // Handler to update UI timer, progress bar etc,.
//private SongsManager songManager;
private Utilities utils;
private String[] fileList;
//private int seekForwardTime = 5000; // 5000 milliseconds
//private int seekBackwardTime = 5000; // 5000 milliseconds
//private int currentSongIndex = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.player);
// All player buttons
btnPlay = (ImageButton) findViewById(R.id.btnPlay);
btnNext = (ImageButton) findViewById(R.id.btnNext);
btnBack = (ImageButton) findViewById(R.id.btnBack);
btnPlaylist = (ImageButton) findViewById(R.id.btnPlaylist);
songProgressBar = (SeekBar) findViewById(R.id.songProgressBar);
songTitleLabel = (TextView) findViewById(R.id.songTitle);
// PlayListActivity():
fillAssetList();
Toast.makeText(getApplicationContext(), "fill asset list", Toast.LENGTH_LONG).show();
if (savedInstanceState != null) {
duration = savedInstanceState.getLong("duration");
currentTime = savedInstanceState.getLong("currentTime");
isPlaying = savedInstanceState.getBoolean("isPlaying");
if (isPlaying) {
pauseMusic(null);
}
}
btnPlaylist.setOnClickListener(new View.OnClickListener() {
//@Override
public void onClick(View arg0) {
Intent i = new Intent(getApplicationContext(), PlayListActivity.class);
startActivityForResult(i, 100);
}
});
}
main_layout.xml
<!-- Playlist button -->
<ImageButton
android:id="@+id/btnPlaylist"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="@drawable/btn_playlist"
android:background="@null"/>
</LinearLayout>
错误
03-08 06:16:14.149 25914-25914/com.macmac.tourplayer W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41854e90)
03-08 06:16:14.159 25914-25914/com.macmac.tourplayer E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.macmac.tourplayer, PID: 25914
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.macmac.tourplayer/com.macmac.tourplayer.PlayListActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2205)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2255)
at android.app.ActivityThread.access0(ActivityThread.java:142)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1203)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5118)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
at android.app.ListActivity.onContentChanged(ListActivity.java:243)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:322)
at android.app.Activity.setContentView(Activity.java:1973)
at com.macmac.tourplayer.PlayListActivity.onCreate(PlayListActivity.java:16)
at android.app.Activity.performCreate(Activity.java:5275)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2255)
at android.app.ActivityThread.access0(ActivityThread.java:142)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1203)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5118)
at java.lang.reflect.Method.invokeNative(Native Method)
更改playlist.xml
中listview的id
android:id="@android:id/list"
已检查 ListView
上的参考资料。 setAdapter
使用 ListAdapter
而不是你的 ArrayAdapter
。 setAdapter(ListAdapter adapter)
好的,我明白了。我必须扩展 Activity
而不是 ListActivity
。我真的不明白为什么,但它奏效了。
当我 select 我的 main.java 中的 btnPlaylist 时,我在按钮处收到了错误代码。我不知道我做错了什么。它说我需要一个带有 R.id.list 的 ListView。我假设它指的是我的 playlist_item_row.xml 但我试过了并继续清理我的项目。
public class PlayListActivity extends ListActivity {
ListView listView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.playlist);
listView = (ListView)findViewById(R.id.listView);
//double check that it's list_item
String[] songs = this.getResources().getStringArray(R.array.TrackNames);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.layout.playlist_item_row,R.id.song_title,songs);
listView.setAdapter(adapter);
}
}
这是 PlayListActivity.java
的 xml 调用
playlist.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="#242424"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_selector" />
这个xml我坚持我的价值观 资源 xml in res>values>'track_array.xml'
<resources>
<string-array name="TrackNames">
<item>100 - Welcome to London</item>
<item>102 - Trafalgarsquare</item>
</string-array>
</resources>
playlist_item_row.xml
<TextView
android:id="@+id/song_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:padding="10dp" />
main.java
private MediaPlayer mp;
private MediaPlayer nextMp;
private TextView tvTime;
private long currentTime;
private long duration;
private boolean isPlaying;
private boolean mCompatMode = true;
private MediaPlayer mNextPlayer;
private OnCompletionListener mCompletion;
private ImageButton btnPlay;
private ImageButton btnPlaylist;
private SeekBar songProgressBar;
private TextView songTitleLabel;
//private Handler mHandler = new Handler(); // Handler to update UI timer, progress bar etc,.
//private SongsManager songManager;
private Utilities utils;
private String[] fileList;
//private int seekForwardTime = 5000; // 5000 milliseconds
//private int seekBackwardTime = 5000; // 5000 milliseconds
//private int currentSongIndex = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.player);
// All player buttons
btnPlay = (ImageButton) findViewById(R.id.btnPlay);
btnNext = (ImageButton) findViewById(R.id.btnNext);
btnBack = (ImageButton) findViewById(R.id.btnBack);
btnPlaylist = (ImageButton) findViewById(R.id.btnPlaylist);
songProgressBar = (SeekBar) findViewById(R.id.songProgressBar);
songTitleLabel = (TextView) findViewById(R.id.songTitle);
// PlayListActivity():
fillAssetList();
Toast.makeText(getApplicationContext(), "fill asset list", Toast.LENGTH_LONG).show();
if (savedInstanceState != null) {
duration = savedInstanceState.getLong("duration");
currentTime = savedInstanceState.getLong("currentTime");
isPlaying = savedInstanceState.getBoolean("isPlaying");
if (isPlaying) {
pauseMusic(null);
}
}
btnPlaylist.setOnClickListener(new View.OnClickListener() {
//@Override
public void onClick(View arg0) {
Intent i = new Intent(getApplicationContext(), PlayListActivity.class);
startActivityForResult(i, 100);
}
});
}
main_layout.xml
<!-- Playlist button -->
<ImageButton
android:id="@+id/btnPlaylist"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="@drawable/btn_playlist"
android:background="@null"/>
</LinearLayout>
错误
03-08 06:16:14.149 25914-25914/com.macmac.tourplayer W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41854e90)
03-08 06:16:14.159 25914-25914/com.macmac.tourplayer E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.macmac.tourplayer, PID: 25914
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.macmac.tourplayer/com.macmac.tourplayer.PlayListActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2205)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2255)
at android.app.ActivityThread.access0(ActivityThread.java:142)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1203)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5118)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
at android.app.ListActivity.onContentChanged(ListActivity.java:243)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:322)
at android.app.Activity.setContentView(Activity.java:1973)
at com.macmac.tourplayer.PlayListActivity.onCreate(PlayListActivity.java:16)
at android.app.Activity.performCreate(Activity.java:5275)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2255)
at android.app.ActivityThread.access0(ActivityThread.java:142)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1203)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5118)
at java.lang.reflect.Method.invokeNative(Native Method)
更改playlist.xml
中listview的idandroid:id="@android:id/list"
已检查 ListView
上的参考资料。 setAdapter
使用 ListAdapter
而不是你的 ArrayAdapter
。 setAdapter(ListAdapter adapter)
好的,我明白了。我必须扩展 Activity
而不是 ListActivity
。我真的不明白为什么,但它奏效了。