嗨,这是我的代码,我想做的是在我触摸屏幕可绘制对象开始出现后,第一张图片出现在背景中(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): 在 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.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): 在 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)