onDestroy()处的空指针异常

时间:2013-05-25 18:42:59

标签: android null nullpointerexception android-drawable

嗨,这是我的代码,我试图做的是在我触摸屏幕drawables开始出现后,首先在背景(touch2)中出现一张图片。 那么问题出在哪里?

问题出在我触摸屏幕之前,当我看到touch2图像时,如果我按下后退按钮,我会收到强制关闭和log cat中的错误,表示活动因空指针而无法破坏。 这是我的代码

RelativeLayout psirenlayout;
MediaPlayer mp;
private boolean played;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //full screeen
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
    //full screen
    setContentView(R.layout.constrc);
    //brightness full
    WindowManager.LayoutParams lp = getWindow().getAttributes();
    lp.screenBrightness = 100 / 100.0f;
    //brightness full
    psirenlayout = (RelativeLayout) findViewById(R.id.psirenlayout);
    this.psirenlayout.setBackgroundResource(R.drawable.touch2);
    played=false;



  psirenlayout.setOnTouchListener(new OnTouchListener() {


@SuppressWarnings("deprecation")
@Override
public boolean onTouch(View v, MotionEvent event) {
    int DELAY = 80;
    if (!played){
        playSound();
        played=true;
        }
          if(event.getAction() == MotionEvent.ACTION_UP) {    
    final RelativeLayout psirenlayout = ((RelativeLayout)  findViewById(R.id.psirenlayout));
    ColorDrawable f = new ColorDrawable(0xffff0000);
    ColorDrawable f2 = new ColorDrawable(0xffff7f00);
    ColorDrawable f3 = new ColorDrawable(0xffffff00);
    ColorDrawable f4 = new ColorDrawable(0xff00ff00);
    ColorDrawable f5 = new ColorDrawable(0xff00ffff);
    ColorDrawable f6 = new ColorDrawable(0xff0000ff);
    ColorDrawable f7 = new ColorDrawable(0xff8b00ff);
    ColorDrawable f8 = new ColorDrawable(0xffff0000);
    ColorDrawable f9 = new ColorDrawable(0xffff7f00);
    ColorDrawable f10 = new ColorDrawable(0xffffff00);
    ColorDrawable f11 = new ColorDrawable(0xff00ffff);
    ColorDrawable f12 = new ColorDrawable(0xff0000ff);
    ColorDrawable f13 = new ColorDrawable(0xff8b00ff);




    AnimationDrawable a = new AnimationDrawable();

    a.addFrame(f, DELAY);
    a.addFrame(f2, DELAY);
    a.addFrame(f3, DELAY);
    a.addFrame(f4, DELAY);
    a.addFrame(f5, DELAY);
    a.addFrame(f6, DELAY);
    a.addFrame(f7, DELAY);
    a.addFrame(f8, DELAY);
    a.addFrame(f9, DELAY);
    a.addFrame(f10, DELAY);
    a.addFrame(f11, DELAY);
    a.addFrame(f12, DELAY);
    a.addFrame(f13, DELAY);


    a.setOneShot(false);

    psirenlayout.setBackgroundDrawable(a); //  is deprecated  API 16
     //psirenlayout.setBackground(a); //  API 16
    a.start();
            }
        return true;
    }
  });
}
  private void playSound(){
      mp = MediaPlayer.create(disco.this, R.raw.disco);
  mp.setOnCompletionListener(new OnCompletionListener() {

      @Override
      public void onCompletion(MediaPlayer mp) {

          mp.release();
      }
  }); 
  mp.setLooping(true);
  mp.start();
 }

  @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if ((keyCode == KeyEvent.KEYCODE_BACK))
        {   
            mp.stop();
            finish();
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
   protected void onPause() {
  super.onDestroy();

    }
    @Override
       protected void onDestroy() {
        mp.stop();
      super.onDestroy();


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

}

我在毁灭时尝试了一切!我一直都会收到这个错误 在我强制关闭程序剂量关闭后,它返回到mainActivity,这很奇怪。

  

25 15:29:49.543:E / AndroidRuntime(11154):致命异常:主要   05-25 15:29:49.543:E / AndroidRuntime(11154):java.lang.NullPointerException   05-25 15:29:49.543:E / AndroidRuntime(11154):at com.soheil.prolight.disco.onKeyDown(disco.java:120)   05-25 15:29:49.543:E / AndroidRuntime(11154):在android.view.KeyEvent.dispatch(KeyEvent.java:2609)   05-25 15:29:49.543:E / AndroidRuntime(11154):在android.app.Activity.dispatchKeyEvent(Activity.java:2375)   05-25 15:29:49.543:E / AndroidRuntime(11154):at com.android.internal.policy.impl.PhoneWindow $ DecorView.dispatchKeyEvent(PhoneWindow.java:1847)   05-25 15:29:49.543:E / AndroidRuntime(11154):在android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3701)   05-25 15:29:49.543:E / AndroidRuntime(11154):在android.view.ViewRootImpl.handleImeFinishedEvent(ViewRootImpl.java:3651)   05-25 15:29:49.543:E / AndroidRuntime(11154):在android.view.ViewRootImpl $ ViewRootHandler.handleMessage(ViewRootImpl.java:2818)   05-25 15:29:49.543:E / AndroidRuntime(11154):在android.os.Handler.dispatchMessage(Handler.java:99)   05-25 15:29:49.543:E / AndroidRuntime(11154):在android.os.Looper.loop(Looper.java:137)   05-25 15:29:49.543:E / AndroidRuntime(11154):在android.app.ActivityThread.main(ActivityThread.java:5041)   05-25 15:29:49.543:E / AndroidRuntime(11154):at java.lang.reflect.Method.invokeNative(Native Method)   05-25 15:29:49.543:E / AndroidRuntime(11154):at java.lang.reflect.Method.invoke(Method.java:511)   05-25 15:29:49.543:E / AndroidRuntime(11154):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)   05-25 15:29:49.543:E / AndroidRuntime(11154):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)

2 个答案:

答案 0 :(得分:3)

if(mp != null) { }方法中mp.stop();周围加onDestroy()。如果MediaPlayer尚未初始化,您可能会尝试停止它。

然后onDestroy()方法变为:

@Override
protected void onDestroy() {
    if(mp != null) {
        mp.stop();
    }
    super.onDestroy();
}

答案 1 :(得分:0)

从onPause内部调用onDestroy是不对的...
- onCreate用于设置基础组件
- onDestroy是做任何清理的 - 如果你想启动或停止动作和与应用程序相关的流程,最好依靠onResume和onPause

    @Override
   protected void onPause() {
        if (mp!=null) { mp.stop(); }

    }
    @Override
       protected void onDestroy() {
      super.onDestroy();
       }