当方向发生变化时,Android应用会崩溃

时间:2013-10-05 23:19:42

标签: 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.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

2 个答案:

答案 0 :(得分:0)

我不是大师,但它可能会保存你的mp

@Override
public Object onRetainCustomNonConfigurationInstance() {
    return this.mp;
}

并且在onCreate()中你会像他的

一样得到它
mp = (MediaPlayer) getLastCustomNonConfigurationInstance();

选中此out ,因为尝试将MediaPlayer alive保留在Activity中时会遇到问题

答案 1 :(得分:0)

尝试使用以下功能重置音乐播放器:

private MediaPlayer mpSound;
    private void stopPlaying() {
        if (mpSound != null) {
            mpSound.stop();
            mpSound.release();
            mpSound = null;
       }
    }

每次调用确保声音设置为null并释放,以便下次播放时,您的应用不会崩溃(在这种情况下,当它改变方向时)

我的应用程序存在类似问题,上面的代码修复了它。