2013-12-08 54 views
-2

我試圖調用一個新的活動(「OtherActivity」),當我點擊行動欄中的行動項目...但它給了我java.lang.RuntimeException:無法啓動活動ComponentInfo。如何調試「java.lang.RuntimeException:無法啓動活動ComponentInfo」?

我已經在manifest.xml文件中指定了MainActivity和OtherActivity。

package com.example.notice; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.View; 
import android.widget.Button; 

public class MainActivity extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Button btn = (Button)findViewById(R.id.action_settings); 
    btn.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      // TODO Auto-generated method stub 
      Intent i = new    Intent(MainActivity.this,OtherActivity.class); 
      startActivity(i); 
     } 
    }); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

} 

,這是我的logcat

12-08 01:51:38.118: E/AndroidRuntime(2823): FATAL EXCEPTION: main 
12-08 01:51:38.118: E/AndroidRuntime(2823): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.notice/com.example.notice.MainActivity}: java.lang.NullPointerException 
12-08 01:51:38.118: E/AndroidRuntime(2823):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211) 
12-08 01:51:38.118: E/AndroidRuntime(2823):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) 
12-08 01:51:38.118: E/AndroidRuntime(2823):  at android.app.ActivityThread.access$600(ActivityThread.java:141) 
12-08 01:51:38.118: E/AndroidRuntime(2823):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) 
12-08 01:51:38.118: E/AndroidRuntime(2823):  at android.os.Handler.dispatchMessage(Handler.java:99) 
12-08 01:51:38.118: E/AndroidRuntime(2823):  at android.os.Looper.loop(Looper.java:137) 
12-08 01:51:38.118: E/AndroidRuntime(2823):  at android.app.ActivityThread.main(ActivityThread.java:5103) 
12-08 01:51:38.118: E/AndroidRuntime(2823):  at java.lang.reflect.Method.invokeNative(Native Method) 
12-08 01:51:38.118: E/AndroidRuntime(2823):  at java.lang.reflect.Method.invoke(Method.java:525) 
12-08 01:51:38.118: E/AndroidRuntime(2823):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
12-08 01:51:38.118: E/AndroidRuntime(2823):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
12-08 01:51:38.118: E/AndroidRuntime(2823):  at dalvik.system.NativeStart.main(Native Method) 
12-08 01:51:38.118: E/AndroidRuntime(2823): Caused by: java.lang.NullPointerException 
12-08 01:51:38.118: E/AndroidRuntime(2823):  at com.example.notice.MainActivity.onCreate(MainActivity.java:17) 
12-08 01:51:38.118: E/AndroidRuntime(2823):  at android.app.Activity.performCreate(Activity.java:5133) 
12-08 01:51:38.118: E/AndroidRuntime(2823):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
12-08 01:51:38.118: E/AndroidRuntime(2823):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175) 
12-08 01:51:38.118: E/AndroidRuntime(2823):  ... 11 more 

OtherActivity

package com.example.notice; 

import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 

public class OtherActivity extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_other); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.other, menu); 
    return true; 
} 

}

+3

如果您需要儘快回答,那麼您應該儘快發佈完整的logcat,以便我們可以看到錯誤 – codeMagic

+0

您最好發佈您的logcat消息! – rokonoid

+0

我確實..請幫助 –

回答

0

的問題是,有在activity_main.xml沒有Buttonaction_settingsid。所以當你嘗試設置OnClickListener時,你會得到一個NPE。如果是從menu那麼你需要使用onOptionsItemSelected()

我們知道這是因爲在這兩條線看這裏

Caused by: java.lang.NullPointerException 
12-08 01:51:38.118: E/AndroidRuntime(2823):  at  
com.example.notice.MainActivity.onCreate(MainActivity.java:17) 

它說的東西是nullMainActivity線17是你設置你的listener,這意味着它是不正確intitalized。

OnOptionsItemSelected Docs

+0

感謝它的工作...... –

0

最有可能此行

Button btn = (Button)findViewById(R.id.action_settings); 

是一個麻煩製造者:確保你正確初始化您的BTN - 看起來像是空

+0

是的...我認爲菜單按鈕也可以像任何普通的按鈕處理...非常感謝您的幫助 –

0

的action_setting是一個菜單項不是一個按鈕在活動佈局中,所以您應該使用按鈕名稱(在活動佈局「R.layout.activity_main」內)。不是菜單項

相關問題