Soheyl
Soheyl

Reputation: 549

Null pointer exception at onDestroy()

Hi, here is my code, what I was trying to do is first one picture appears in background (touch2) after I touch the screen drawables starting to appear . So where is the problem ?

The problem is before I touch the screen and when I see touch2 image if I press back button I receive the force close and error in log cat which says activity cannot destroy because of null pointer . Here is my code

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;
}

}

I tried everything on stop on destroy! I get this error all time after I get force close the program dosent closes it returns to mainActivity which is strange.

25 15:29:49.543: E/AndroidRuntime(11154): FATAL EXCEPTION: main 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): at android.view.KeyEvent.dispatch(KeyEvent.java:2609) 05-25 15:29:49.543: E/AndroidRuntime(11154): at 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): at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3701) 05-25 15:29:49.543: E/AndroidRuntime(11154): at android.view.ViewRootImpl.handleImeFinishedEvent(ViewRootImpl.java:3651) 05-25 15:29:49.543: E/AndroidRuntime(11154): at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:2818) 05-25 15:29:49.543: E/AndroidRuntime(11154): at android.os.Handler.dispatchMessage(Handler.java:99) 05-25 15:29:49.543: E/AndroidRuntime(11154): at android.os.Looper.loop(Looper.java:137) 05-25 15:29:49.543: E/AndroidRuntime(11154): at 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)

Upvotes: 1

Views: 1869

Answers (2)

Tom Naessens
Tom Naessens

Reputation: 1837

Put a if(mp != null) { } around mp.stop(); in the onDestroy() method. It is possible that you try to stop the MediaPlayer when it hasn't been initialised yet.

The onDestroy() method then becomes:

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

Upvotes: 4

Yilmaz Guleryuz
Yilmaz Guleryuz

Reputation: 9745

calling onDestroy from inside onPause is not right...
- onCreate is to setup base components
- onDestroy is to do any cleanup
- and if you want to start or stop actions and and application related flows, better to rely on onResume and onPause

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

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

Upvotes: 0

Related Questions