当方向改变时,这个应用程序会崩溃,特别是在我可以给变量返回值的状态下。但就我而言,我只想清空变量mp
。当我添加mp.reset()
应用程序时,我旋转手机时开始崩溃。
package com.phone.sensor;
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
public class sensorActivity extends Activity implements SensorEventListener{
public boolean musStatus = false;
public boolean musDeclare = false;
public MediaPlayer mp;
Sensor accelerometer;
SensorManager sm;
TextView acceleration;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(savedInstanceState==null)
{
musDeclare = false;
musStatus = false;
mp = null;
}
else
{
mp.reset();
}
setContentView(R.layout.activity_main);
sm=(SensorManager) getSystemService(SENSOR_SERVICE);
accelerometer=sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sm.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
acceleration=(TextView)findViewById(R.id.acceleration);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSaveInstanceState (Bundle outState)
{
super.onSaveInstanceState(outState);
outState.putBoolean("musStatus", musStatus);
outState.putBoolean("musDeclare", musDeclare);
}
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
acceleration.setText("X: "+event.values[0]+
"\nY: "+event.values[1]+
"\nZ: "+event.values[2]);
float x = event.values[0];
float y = event.values[1];
float z = event.values[2];
if(musDeclare == false)
{
mp = MediaPlayer.create(this, R.raw.alexander);
musDeclare = true;
}
if(y > 8.9) {
if(musStatus == false)
{
mp.start();
musStatus = true;
}
}
if(y < 5)
{
if(musStatus == true)
{
mp.stop();
musStatus = false;
if(musDeclare == true)
{
mp = MediaPlayer.create(this, R.raw.alexander);
musDeclare = false;
}
}
}
}
}
日志猫
10-06 01:30:37.105: E/AndroidRuntime(15258): FATAL EXCEPTION: main
10-06 01:30:37.105: E/AndroidRuntime(15258): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.phone.sensor/com.phone.sensor.sensorActivity}: java.lang.NullPointerException
10-06 01:30:37.105: E/AndroidRuntime(15258): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2460)
10-06 01:30:37.105: E/AndroidRuntime(15258): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2521)
10-06 01:30:37.105: E/AndroidRuntime(15258): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4260)
10-06 01:30:37.105: E/AndroidRuntime(15258): at android.app.ActivityThread.access$700(ActivityThread.java:162)
10-06 01:30:37.105: E/AndroidRuntime(15258): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1376)
10-06 01:30:37.105: E/AndroidRuntime(15258): at android.os.Handler.dispatchMessage(Handler.java:99)
10-06 01:30:37.105: E/AndroidRuntime(15258): at android.os.Looper.loop(Looper.java:158)
10-06 01:30:37.105: E/AndroidRuntime(15258): at android.app.ActivityThread.main(ActivityThread.java:5777)
10-06 01:30:37.105: E/AndroidRuntime(15258): at java.lang.reflect.Method.invokeNative(Native Method)
10-06 01:30:37.105: E/AndroidRuntime(15258): at java.lang.reflect.Method.invoke(Method.java:511)
10-06 01:30:37.105: E/AndroidRuntime(15258): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1083)
10-06 01:30:37.105: E/AndroidRuntime(15258): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:850)
10-06 01:30:37.105: E/AndroidRuntime(15258): at dalvik.system.NativeStart.main(Native Method)
10-06 01:30:37.105: E/AndroidRuntime(15258): Caused by: java.lang.NullPointerException
10-06 01:30:37.105: E/AndroidRuntime(15258): at com.phone.sensor.sensorActivity.onCreate(sensorActivity.java:33)
10-06 01:30:37.105: E/AndroidRuntime(15258): at android.app.Activity.performCreate(Activity.java:5165)
10-06 01:30:37.105: E/AndroidRuntime(15258): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1103)
10-06 01:30:37.105: E/AndroidRuntime(15258): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
10-06 01:30:37.105: E/AndroidRuntime(15258): ... 12 more