2013-02-10 84 views
0

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

您應在源代碼之前放置問題。不僅在標題。一篇只包含源代碼的文章,沒有很好的闡述,並且可能很快就會被關閉,「因爲這不是一個真正的問題」 – AlexWien 2013-02-10 15:49:32

回答

2

您的訂單在onCreate()中的作業與方法調用關閉。您在嘗試使用mAudioManager之前先嚐試使用它,以便它保留爲空,並引發NPE。它必須是這樣的:因爲我們給了它一些東西來支持提前時checkIfPhoneIsSilent()被稱爲

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    mAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE); 
    checkIfPhoneIsSilent(); 
    setButtonOnClickListener(); 
    } 

這樣mAudioManager將不能爲空。

+0

非常感謝,它的工作正常。 :) – Paras 2013-02-10 15:37:29

0

只是改變:

 checkIfPhoneIsSilent(); 
     setButtonOnClickListener(); 
     mAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE); 

到:

mAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE); 
    checkIfPhoneIsSilent(); 
    setButtonOnClickListener(); 
+0

請不要解釋爲什麼不提供答案。請參閱A-C答案瞭解如何完成。 – Simon 2013-02-10 15:40:13

+0

不知道是否值得解釋.... – 2013-02-10 15:53:14

相關問題