gpt4 book ai didi

android - onDestroy() 的空指针异常

转载 作者:行者123 更新时间:2023-11-29 16:07:38 26 4
gpt4 key购买 nike

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

问题是在我触摸屏幕之前,当我看到 touch2 图像时,如果我按下后退按钮,我会收到强制关闭和日志 cat 中的错误,它说 Activity 无法破坏,因为空指针。这是我的代码

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): 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)

最佳答案

onDestroy() 方法的 mp.stop(); 周围放置一个 if(mp != null) { }。您可能会在 MediaPlayer 尚未初始化时尝试将其停止。

然后 onDestroy() 方法变为:

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

关于android - onDestroy() 的空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16752803/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com