0

Java 代码

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
4

2 回答 2

2

您的分配顺序与方法调用顺序onCreate()已关闭。你尝试mAudioManager在给它一些东西之前使用它,所以它是空的,并且抛出了一个 NPE。它需要是这样的:

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

这种方式在被调用mAudioManager时不会为空,checkIfPhoneIsSilent()因为我们已经提前给了它一些东西。

于 2013-02-10T15:34:23.227 回答
0

只是改变 :

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

到:

    mAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
    checkIfPhoneIsSilent();
    setButtonOnClickListener();
于 2013-02-10T15:35:49.340 回答