方向改变时Android应用程序崩溃
当方向改变时,这个应用程序崩溃,特别是在我可以给变量返回值的状态下。但是在我的例子中,我只想清空变量方向改变时Android应用程序崩溃,android,media-player,orientation,Android,Media Player,Orientation,当方向改变时,这个应用程序崩溃,特别是在我可以给变量返回值的状态下。但是在我的例子中,我只想清空变量mp。当我添加mp.reset()时,当我旋转手机时,应用程序开始崩溃 package com.phone.sensor; import android.app.Activity; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventList
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;
}
}
}
}
}
我不是大师,但感觉你可以把你的
mp
保存在:
@Override
public Object onRetainCustomNonConfigurationInstance() {
return this.mp;
}
在onCreate()
中,您将像他的一样得到它
mp = (MediaPlayer) getLastCustomNonConfigurationInstance();
选中此项,因为尝试将音乐播放器保持在
活动中时会出现问题请尝试使用以下功能重置音乐播放器:
private MediaPlayer mpSound;
private void stopPlaying() {
if (mpSound != null) {
mpSound.stop();
mpSound.release();
mpSound = null;
}
}
每次调用该选项可确保声音设置为空并释放,以便下次播放时,应用程序不会崩溃(在这种情况下,当它更改方向时)
我的应用程序也有类似的问题,上面的代码修复了它。每次活动改变方向时,它都会重新加载视图。你的应用程序在播放和更改方向时是否崩溃?在我的应用程序中,当我有手机人像时,音乐播放正常;当我旋转手机以横向移动时,音乐停止,应用程序崩溃@Sikni8将您的logcat与错误一起发布。将logcat添加到答案@szymon此解决方案帮助我保持应用程序的活动状态,这很好,但现在的问题是,声音会同时重复播放。这意味着mp是nu reinitialize@SIKNI8,因为Logcat表明重新启动活动会导致您播放声音的错误。
private MediaPlayer mpSound;
private void stopPlaying() {
if (mpSound != null) {
mpSound.stop();
mpSound.release();
mpSound = null;
}
}