[英]Android app crashes when the orientation changes
當方向改變時,此應用程序崩潰,特別是在該狀態下,我可以將變量返回值。 但就我而言,我只想清空變量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;
}
}
}
}
}
Logcat
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
我不是專家,但它像您可以保存mp
:
@Override
public Object onRetainCustomNonConfigurationInstance() {
return this.mp;
}
在onCreate()
您將像他一樣將其取回
mp = (MediaPlayer) getLastCustomNonConfigurationInstance();
進行檢查,因為嘗試使MediaPlayer在Activity
保持Activity
存在問題
嘗試使用以下功能重置音樂播放器:
private MediaPlayer mpSound;
private void stopPlaying() {
if (mpSound != null) {
mpSound.stop();
mpSound.release();
mpSound = null;
}
}
每次調用都會確保將聲音設置為null並釋放,因此下次播放時,您的應用程序不會崩潰(在這種情況下,當它改變方向時)
我的應用程序遇到了類似的問題,上面的代碼修復了它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.