Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
方向改变时Android应用程序崩溃_Android_Media Player_Orientation - Fatal编程技术网

方向改变时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;
       }
    }