2013-04-08 108 views
1

我試圖啓動一個新的活動,當按鈕playlistBtn被點擊時,我得到一個NullPointerException,但我看不到在哪裏做了我的參考文獻出錯了。無法啓動活動ComponentInfo {..}:java.lang.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); 

PlaylistActivity.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> 
+0

請注意您的問題上有更好的標題。 – 2013-04-08 20:23:25

+0

請發佈錯誤的完整堆棧跟蹤。 – thegrinner 2013-04-08 20:25:26

+0

plz還添加了xml的activity_main佈局問題 – 2013-04-08 20:36:03

回答

2

大膽猜測:

btnPlaylist爲null,並且沒有按鈕與activity_main.xml的ID playlistBtn

編輯:

問題是內部PlaylistActivity(線68)。無論你訪問什麼對象都是null。

+0

謝謝。這解決了它。 – user1477362 2013-04-08 21:05:39

相關問題