0

当方向改变时,这个应用程序会崩溃,特别是在我可以给变量返回值的状态下。但就我而言,我只想清空变量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
4

2 回答 2

0

我不是大师,但它就像你可以保存你mp的:

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

你会onCreate()像他一样把它找回来

mp = (MediaPlayer) getLastCustomNonConfigurationInstance();

看看这个因为试图让MediaPlayer 保持活力Activity被问题包围

于 2013-10-05T23:28:10.493 回答
0

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

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

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

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

于 2013-10-05T23:30:15.687 回答