当单击 Button playlistBtn 时,我正在尝试启动一个新活动,并且我得到一个 NullPointerException,但我看不出我的引用哪里出错了。
完整的错误日志:
04-08 17:02:07.847: E/AndroidRuntime(5869): FATAL EXCEPTION: main
04-08 17:02:07.847: E/AndroidRuntime(5869): java.lang.RuntimeException: Unable to start activity ComponentInfo{app.and.androidmusic/app.and.androidmusic.PlaylistActivity}: java.lang.NullPointerException
04-08 17:02:07.847: E/AndroidRuntime(5869): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
04-08 17:02:07.847: E/AndroidRuntime(5869): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-08 17:02:07.847: E/AndroidRuntime(5869): at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-08 17:02:07.847: E/AndroidRuntime(5869): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-08 17:02:07.847: E/AndroidRuntime(5869): at android.os.Handler.dispatchMessage(Handler.java:99)
04-08 17:02:07.847: E/AndroidRuntime(5869): at android.os.Looper.loop(Looper.java:137)
04-08 17:02:07.847: E/AndroidRuntime(5869): at android.app.ActivityThread.main(ActivityThread.java:5039)
04-08 17:02:07.847: E/AndroidRuntime(5869): at java.lang.reflect.Method.invokeNative(Native Method)
04-08 17:02:07.847: E/AndroidRuntime(5869): at java.lang.reflect.Method.invoke(Method.java:511)
04-08 17:02:07.847: E/AndroidRuntime(5869): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-08 17:02:07.847: E/AndroidRuntime(5869): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-08 17:02:07.847: E/AndroidRuntime(5869): at dalvik.system.NativeStart.main(Native Method)
04-08 17:02:07.847: E/AndroidRuntime(5869): Caused by: java.lang.NullPointerException
04-08 17:02:07.847: E/AndroidRuntime(5869): at app.and.androidmusic.PlaylistActivity.onCreate(PlaylistActivity.java:68)
04-08 17:02:07.847: E/AndroidRuntime(5869): at android.app.Activity.performCreate(Activity.java:5104)
04-08 17:02:07.847: E/AndroidRuntime(5869): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-08 17:02:07.847: E/AndroidRuntime(5869): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
MainActivity.java:
public class MainActivity extends ListActivity {
...
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
Button btnPlaylist = (Button) findViewById(R.id.playlistBtn);
btnPlaylist.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent in = new Intent(getApplicationContext(), PlaylistActivity.class);
startActivity(in);
播放列表活动.java:
public class PlaylistActivity extends ListActivity{
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_playlist); {
AndroidManifest.xml:
<activity
android:name="app.and.androidmusic.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".PlaylistActivity" />
好的,这里是你问的activity_main.xml,它很长:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<app.and.androidmusic.DragNDropListView
android:id="@+id/android:list"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_below="@+id/playlistBtn"
android:layout_alignParentLeft="true" >
</app.and.androidmusic.DragNDropListView>
<Button
android:id="@+id/playBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/android:list"
android:layout_toRightOf="@+id/prevBtn"
android:text="@string/playBtn" />
<Button
android:id="@+id/pauseBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/android:list"
android:layout_toRightOf="@+id/playBtn"
android:text="@string/pauseBtn" />
<Button
android:id="@+id/prevBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/android:list"
android:text="@string/prevBtn" />
<Button
android:id="@+id/nextBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/android:list"
android:layout_toRightOf="@+id/pauseBtn"
android:text="@string/nextBtn" />
<Button
android:id="@+id/songsBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/songTxt"
android:text="@string/songsBtn" />
<Button
android:id="@+id/playlistBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/songTxt"
android:layout_toRightOf="@+id/songsBtn"
android:text="@string/playlistBtn" />
<Button
android:id="@+id/eqBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/songTxt"
android:layout_toRightOf="@+id/playlistBtn"
android:text="@string/eqBtn" />
<TextView
android:id="@+id/songTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="" />
</RelativeLayout>