Java代碼的獲得E/AndroidRuntime(629):了java.lang.RuntimeException:無法啓動活動ComponentInfo錯誤
public class Main extends Activity {
private AudioManager mAudioManager;
private boolean mPhoneIsSilent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
checkIfPhoneIsSilent();
setButtonOnClickListener();
mAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
}
public void setButtonOnClickListener(){
Button b = (Button) findViewById(R.id.toggleButton);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
if (mPhoneIsSilent){
//change back to normal mode
mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
mPhoneIsSilent = false;
}
else {
mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
mPhoneIsSilent = true;
}
//To toggle the UI
toggleUI();
}
});
}
public void checkIfPhoneIsSilent(){
int ringerMode = mAudioManager.getRingerMode();
if (ringerMode == AudioManager.RINGER_MODE_SILENT){
mPhoneIsSilent = true;
}
else {
mPhoneIsSilent = false;
}
}
public void toggleUI(){
ImageView iv = (ImageView) findViewById(R.id.phone_icon);
Drawable newImage;
if (mPhoneIsSilent){
newImage = getResources().getDrawable(R.drawable.phone_silent);
}
else {
newImage = getResources().getDrawable(R.drawable.phone_on);
}
iv.setImageDrawable(newImage);
}
@Override
protected void onResume(){
super.onResume();
checkIfPhoneIsSilent();
toggleUI();
}
}
日誌跟蹤
02-10 20:19:58.198: D/dalvikvm(5870): GC_EXTERNAL_ALLOC freed 45K, 53% free 2544K/5379K, external 1869K/2137K, paused 86ms
02-10 20:19:58.258: D/AndroidRuntime(5870): Shutting down VM
02-10 20:19:58.258: W/dalvikvm(5870): threadid=1: thread exiting with uncaught exception (group=0x40015560)
02-10 20:19:58.278: E/AndroidRuntime(5870): FATAL EXCEPTION: main
02-10 20:19:58.278: E/AndroidRuntime(5870): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.parasmani.silentmodetoggle/com.parasmani.silentmodetoggle.Main}: java.lang.NullPointerException
02-10 20:19:58.278: E/AndroidRuntime(5870): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-10 20:19:58.278: E/AndroidRuntime(5870): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-10 20:19:58.278: E/AndroidRuntime(5870): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-10 20:19:58.278: E/AndroidRuntime(5870): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-10 20:19:58.278: E/AndroidRuntime(5870): at android.os.Handler.dispatchMessage(Handler.java:99)
02-10 20:19:58.278: E/AndroidRuntime(5870): at android.os.Looper.loop(Looper.java:123)
02-10 20:19:58.278: E/AndroidRuntime(5870): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-10 20:19:58.278: E/AndroidRuntime(5870): at java.lang.reflect.Method.invokeNative(Native Method)
02-10 20:19:58.278: E/AndroidRuntime(5870): at java.lang.reflect.Method.invoke(Method.java:507)
02-10 20:19:58.278: E/AndroidRuntime(5870): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-10 20:19:58.278: E/AndroidRuntime(5870): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-10 20:19:58.278: E/AndroidRuntime(5870): at dalvik.system.NativeStart.main(Native Method)
02-10 20:19:58.278: E/AndroidRuntime(5870): Caused by: java.lang.NullPointerException
02-10 20:19:58.278: E/AndroidRuntime(5870): at com.parasmani.silentmodetoggle.Main.checkIfPhoneIsSilent(Main.java:53)
02-10 20:19:58.278: E/AndroidRuntime(5870): at com.parasmani.silentmodetoggle.Main.onCreate(Main.java:22)
02-10 20:19:58.278: E/AndroidRuntime(5870): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-10 20:19:58.278: E/AndroidRuntime(5870): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-10 20:19:58.278: E/AndroidRuntime(5870): ... 11 more
02-10 20:24:58.423: I/Process(5870): Sending signal. PID: 5870 SIG: 9
您應在源代碼之前放置問題。不僅在標題。一篇只包含源代碼的文章,沒有很好的闡述,並且可能很快就會被關閉,「因爲這不是一個真正的問題」 – AlexWien 2013-02-10 15:49:32