如何解决“不幸的Android应用已停止”

时间:2014-08-06 10:28:33

标签: android

我为shoutcast流制作应用程序并且工作正常,但是当我停止它并通过后退按钮返回时 此消息显示unfortunately android app has stopped这个我的代码我想要关闭应用程序当我通过停止按钮停止它并按下后退按钮关闭不幸的是Android应用程序已经停止

import android.os.Bundle;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

    Button startButton, stopButton;
    static Context context;
    boolean isPlaying;
    Intent streamService;
    SharedPreferences prefs;
    // headset
    private int headsetSwitch = 1 ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context = this;
        startButton = (Button) findViewById(R.id.startButton);
        stopButton = (Button) findViewById(R.id.stopButton);
        prefs = PreferenceManager.getDefaultSharedPreferences(context);
        getPrefs();
        streamService = new Intent(MainActivity.this, StreamService.class);     

        startButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                startService(streamService);
                startButton.setEnabled(false);
            }
        });

        stopButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                stopService(streamService);
                startButton.setEnabled(true);

                //headset2
                registerReceiver(headsetReceiver, new IntentFilter(Intent.ACTION_HEADSET_PLUG));



            }
        });
    }


    // If headset gets unplugged, stop music and service.
    private BroadcastReceiver headsetReceiver = new BroadcastReceiver() {
        private boolean headsetConnected = false;

        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            // Log.v(TAG, "ACTION_HEADSET_PLUG Intent received");
            if (intent.hasExtra("state")) {
                if (headsetConnected && intent.getIntExtra("state", 0) == 0) {
                    headsetConnected = false;
                    headsetSwitch = 0;
                    // Log.v(TAG, "State =  Headset disconnected");
                    // headsetDisconnected();
                } else if (!headsetConnected
                        && intent.getIntExtra("state", 0) == 1) {
                    headsetConnected = true;
                    headsetSwitch = 1;
                    // Log.v(TAG, "State =  Headset connected");
                }

            }

            switch (headsetSwitch) {
            case (0):
                headsetDisconnected();
                break;
            case (1):
                break;
            }
        }

    };

    private void headsetDisconnected() {
        stopService(streamService);
        startButton.setEnabled(true);

    }


    public void getPrefs() {
            isPlaying = prefs.getBoolean("isPlaying", false);
            if (isPlaying) startButton.setEnabled(false);

            PhoneStateListener phoneStateListener = new PhoneStateListener() {
                @Override
                public void onCallStateChanged(int state, String incomingNumber) {
                    if (state == TelephonyManager.CALL_STATE_RINGING) {
                        //Incoming call: Pause music
                        stopService(streamService);
                        startButton.setEnabled(true);
                    } else if(state == TelephonyManager.CALL_STATE_IDLE) {
                        //Not in call: Play music
                        startService(streamService);
                        startButton.setEnabled(false);
                    } else if(state == TelephonyManager.CALL_STATE_OFFHOOK) {
                        //A call is dialing, active or on hold
                        stopService(streamService);
                        startButton.setEnabled(true);
                    }
                    super.onCallStateChanged(state, incomingNumber);
                }
            };
            TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
            if(mgr != null) {
                mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
            }

    }
    public void onDestroy() {
        stopService(streamService);
        startButton.setEnabled(true);
    }
}

08-06 13:12:25.519: D/StreamService(22065): onDestroy
08-06 13:12:25.519: V/MediaPlayer-JNI(22065): stop
08-06 13:12:25.519: V/MediaPlayer(22065): stop
08-06 13:12:25.529: V/MediaPlayer-JNI(22065): release
08-06 13:12:25.529: V/MediaPlayer(22065): setListener
08-06 13:12:25.529: V/MediaPlayer(22065): disconnect
08-06 13:12:25.539: V/MediaPlayer(22065): destructor
08-06 13:12:25.539: V/MediaPlayer(22065): disconnect
08-06 13:12:26.624: D/AndroidRuntime(22039): Shutting down VM
08-06 13:12:26.624: W/dalvikvm(22039): threadid=1: thread exiting with uncaught exception (group=0x41d44700)
08-06 13:12:26.629: E/AndroidRuntime(22039): FATAL EXCEPTION: main
08-06 13:12:26.629: E/AndroidRuntime(22039): android.app.SuperNotCalledException: Activity {com.test.test/com.test.test.MainActivity} did not call through to super.onDestroy()
08-06 13:12:26.629: E/AndroidRuntime(22039):    at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3621)
08-06 13:12:26.629: E/AndroidRuntime(22039):    at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3654)
08-06 13:12:26.629: E/AndroidRuntime(22039):    at android.app.ActivityThread.access$1300(ActivityThread.java:159)
08-06 13:12:26.629: E/AndroidRuntime(22039):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1369)
08-06 13:12:26.629: E/AndroidRuntime(22039):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-06 13:12:26.629: E/AndroidRuntime(22039):    at android.os.Looper.loop(Looper.java:176)
08-06 13:12:26.629: E/AndroidRuntime(22039):    at android.app.ActivityThread.main(ActivityThread.java:5419)
08-06 13:12:26.629: E/AndroidRuntime(22039):    at java.lang.reflect.Method.invokeNative(Native Method)
08-06 13:12:26.629: E/AndroidRuntime(22039):    at java.lang.reflect.Method.invoke(Method.java:525)
08-06 13:12:26.629: E/AndroidRuntime(22039):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
08-06 13:12:26.629: E/AndroidRuntime(22039):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
08-06 13:12:26.629: E/AndroidRuntime(22039):    at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:0)

  Just override onbackpressed method 
stop everything here and close activity.

@Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        super.onBackPressed();
finish();
    }

答案 1 :(得分:0)

来自你的logcat:

android.app.SuperNotCalledException: Activity {com.test.test/com.test.test.MainActivity} did not call through to super.onDestroy()

super.onDestroy()添加到您重写的onDestroy()方法。

  

当我按下按钮时,应用程序关闭,我不想要那个

您可以覆盖活动中的onBackPressed()致电super.onBackPressed()finish(),以防止后退密钥完成您的活动。虽然通常情况下让您的应用不符合用户的期望是一个坏主意。

答案 2 :(得分:0)

添加

super.onDestroy();
on onDestroy方法