Android onDestroy()处出现空指针异常
您好,这是我的代码,我想做的是在触摸屏幕开始显示可绘制的内容后,第一张图片出现在背景中(touch2)。 那么问题出在哪里呢 问题是在我触摸屏幕之前,当我看到触摸2图像时,如果我按下后退按钮,我会收到日志cat中的强制关闭和错误,该错误表示由于空指针,活动无法销毁。 这是我的密码Android onDestroy()处出现空指针异常,android,null,nullpointerexception,android-drawable,Android,Null,Nullpointerexception,Android Drawable,您好,这是我的代码,我想做的是在触摸屏幕开始显示可绘制的内容后,第一张图片出现在背景中(touch2)。 那么问题出在哪里呢 问题是在我触摸屏幕之前,当我看到触摸2图像时,如果我按下后退按钮,我会收到日志cat中的强制关闭和错误,该错误表示由于空指针,活动无法销毁。 这是我的密码 RelativeLayout psirenlayout; MediaPlayer mp; private boolean played; @Override protected void onCreate(Bundl
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;
}
}
我什么都试过了停止破坏!我总是犯这个错误
在我强制关闭程序dosent后,它会返回到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):位于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):在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):位于java.lang.reflect.Method.Invokenactive(本机方法)
05-25 15:29:49.543:E/AndroidRuntime(11154):在java.lang.reflect.Method.invoke(Method.java:511)
05-25 15:29:49.543:E/AndroidRuntime(11154):在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-25 15:29:49.543:E/AndroidRuntime(11154):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
在mp.stop()周围放置if(mp!=null){}
在ondestory()方法中的code>。当MediaPlayer尚未初始化时,您可能尝试停止它
然后,onDestroy()方法变为:
@Override
protected void onDestroy() {
if(mp != null) {
mp.stop();
}
super.onDestroy();
}
在mp.stop()周围放置if(mp!=null){}
在ondestory()方法中的code>。当MediaPlayer尚未初始化时,您可能尝试停止它
然后,onDestroy()方法变为:
@Override
protected void onDestroy() {
if(mp != null) {
mp.stop();
}
super.onDestroy();
}
在暂停时从内部调用onDestroy不正确…
-onCreate用于设置基本组件
-onDestroy可以进行任何清理
-如果您想启动或停止操作以及与应用程序相关的流,最好依靠onResume和onPause
@Override
protected void onPause() {
if (mp!=null) { mp.stop(); }
}
@Override
protected void onDestroy() {
super.onDestroy();
}
在暂停时从内部调用onDestroy不正确…
-onCreate用于设置基本组件
-onDestroy可以进行任何清理
-如果您想启动或停止操作以及与应用程序相关的流,最好依靠onResume和onPause
@Override
protected void onPause() {
if (mp!=null) { mp.stop(); }
}
@Override
protected void onDestroy() {
super.onDestroy();
}
您应该在onPause()中停止mediaplayer。如果(mp!=null){mp.stop();}mp.stop();。在onDestory()中。现在我发现这个错误tnx mate已经修复:D在这里检查下面的答案您应该在onPause()中停止mediaplayer。如果(mp!=null){mp.stop();}mp.stop();。在onDestory()中。现在我得到这个错误tnx mate它已被修复:Dthere you go检查下面的答案