我試圖調用一個新的活動(「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;
}
}
如果您需要儘快回答,那麼您應該儘快發佈完整的logcat,以便我們可以看到錯誤 – codeMagic
您最好發佈您的logcat消息! – rokonoid
我確實..請幫助 –