简体   繁体   中英

Android app crashes when the orientation changes

This app crashes when the orientation changes, specially in the state there i can give the variables back there value. But in my case i just want to empty the variable mp . When i add mp.reset() the app begins to crash when i rotate the phone.

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

Im no guru but it fells like you could save your mp in:

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

and in onCreate() you will get it back like his

mp = (MediaPlayer) getLastCustomNonConfigurationInstance();

Check this out because trying to keep the MediaPlayer alive in an Activity is surrounded with problems

Try to use the following function to reset your music player:

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

Calling that each time ensures the sound is set to null and released so the next time it plays, your app doesn't crash (in this case when it changes orientation)

I had similar issue with my app and the above code fixed it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM