为什么关闭后我的应用会停止工作?

时间:2015-07-28 04:53:47

标签: java android

当我摆脱Java文件中的状态栏时,这已经开始在我的应用程序中发生了。我使用手机的任务管理器关闭应用程序(我使用真实设备),然后在我的主屏幕上,消息“我的应用程序已停止工作。”#34;我该怎么办?

Java文件

package interactive.siddiqui.survivor;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

public class Main extends Activity {

private MediaPlayer mediaPlayer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

    setContentView(R.layout.activity_main);



    mediaPlayer = MediaPlayer.create(this, R.drawable.background_musicmain);
    mediaPlayer.start();




}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
public void sendMessage(View view)
{
    Intent intent = new Intent(Main.this, GameView.class);
    startActivity(intent);
    onDestroy();
}

public void onDestroy() {
    if (mediaPlayer.isPlaying()) {
        mediaPlayer.stop();
    }
    mediaPlayer.release();
}

}

Logcat

07-28 01:06:05.389    2009-2009/? I/art﹕ Late-enabling -Xcheck:jni
07-28 01:06:05.673    2009-2009/interactive.siddiqui.survivor E/MediaPlayer﹕    Should have subtitle controller already set
07-28 01:06:05.680    2009-2030/interactive.siddiqui.survivor D/OpenGLRenderer﹕ Use EGL_SWAP_BEHAVIOR_PRESERVED: true
07-28 01:06:05.689    2009-2009/interactive.siddiqui.survivor D/Atlas﹕ Validating map...
07-28 01:06:05.701    2009-2009/interactive.siddiqui.survivor E/MediaPlayer﹕ Should have subtitle controller already set
07-28 01:06:05.739    2009-2030/interactive.siddiqui.survivor I/Adreno﹕ EGLInit: QTI Build: 03/04/15, eeab148,
07-28 01:06:05.749    2009-2030/interactive.siddiqui.survivor I/OpenGLRenderer﹕ Initialized EGL, version 1.4
07-28 01:06:05.750    2009-2030/interactive.siddiqui.survivor I/Adreno﹕ GetNativeFormatFromQctPixelFormat: Invalid qct format (611)
07-28 01:06:05.750    2009-2030/interactive.siddiqui.survivor I/Adreno﹕ GetNativeFormatFromQctPixelFormat: Invalid qct format (611)
07-28 01:06:05.750    2009-2030/interactive.siddiqui.survivor I/Adreno﹕ GetNativeFormatFromQctPixelFormat: Invalid qct format (611)
07-28 01:06:05.750    2009-2030/interactive.siddiqui.survivor I/Adreno﹕ GetNativeFormatFromQctPixelFormat: Invalid qct format (611)
07-28 01:06:05.750    2009-2030/interactive.siddiqui.survivor I/Adreno﹕ GetNativeFormatFromQctPixelFormat: Invalid qct format (612)
07-28 01:06:05.750    2009-2030/interactive.siddiqui.survivor I/Adreno﹕ GetNativeFormatFromQctPixelFormat: Invalid qct format (612)
07-28 01:06:05.750    2009-2030/interactive.siddiqui.survivor I/Adreno﹕ GetNativeFormatFromQctPixelFormat: Invalid qct format (612)
07-28 01:06:05.750    2009-2030/interactive.siddiqui.survivor I/Adreno﹕ GetNativeFormatFromQctPixelFormat: Invalid qct format (612)
07-28 01:06:05.757    2009-2030/interactive.siddiqui.survivor   D/OpenGLRenderer﹕ Enabling debug mode 0

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

onDestroy被调用,可能会遇到NullPointerException。将此检查添加为null:

public void onDestroy() {
    if (mediaPlayer == null)
    { return; }

    if (mediaPlayer.isPlaying()) {
        mediaPlayer.stop();
    }
    mediaPlayer.release();
}