无法启动活动ComponentInfo {..}:java.lang.NullPointerException

时间:2013-04-08 20:22:33

标签: java android

我正在尝试在单击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);

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>

1 个答案:

答案 0 :(得分:2)

疯狂猜测:

btnPlaylist为空,且playlistBtn中没有ID为activity_main.xml的按钮。

修改

问题出在PlaylistActivity内(第68行)。无论您访问的是什么对象,都是空的。