Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/218.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何阻止我的应用程序在从另一个应用程序切换回来后崩溃?_Java_Android_Crash_Runtimeexception - Fatal编程技术网

Java 如何阻止我的应用程序在从另一个应用程序切换回来后崩溃?

Java 如何阻止我的应用程序在从另一个应用程序切换回来后崩溃?,java,android,crash,runtimeexception,Java,Android,Crash,Runtimeexception,当我运行我的应用程序并按下home按钮,然后返回到我的应用程序时,会出现RunTimeException和IllegalStateException错误 我制作了一个简单的安卓字母游戏,游戏结束时,通过OK按钮显示时间量和错误次数,让用户返回主屏幕。但是,当我按下home(主页)按钮,然后返回到我的应用程序玩游戏时,在游戏结束时,只要用户单击ok(确定),我就会在Logcat中看到RunTimeException和IllegalStateException错误,游戏会返回(假设不是我的应用程序重

当我运行我的应用程序并按下home按钮,然后返回到我的应用程序时,会出现RunTimeException和IllegalStateException错误

我制作了一个简单的安卓字母游戏,游戏结束时,通过OK按钮显示时间量和错误次数,让用户返回主屏幕。但是,当我按下home(主页)按钮,然后返回到我的应用程序玩游戏时,在游戏结束时,只要用户单击ok(确定),我就会在Logcat中看到RunTimeException和IllegalStateException错误,游戏会返回(假设不是我的应用程序重新启动)到游戏主页。我在这里查看了几篇文章(如),并尝试了这些解决方案,但仍然出现以下错误。我也是新手,不知道这些解决方案与我的代码有什么关系。非常感谢您的帮助

/

/显示游戏结束结果对话框
公共void showGameOverDialog(){
ViewGroup ViewGroup=findviewbyd(android.R.id.content);
查看对话框View=LayoutFlater.from(this).充气(R.layout.game\u over\u对话框,视图组,false);
dialog=新建AlertDialog.Builder(此);
dialog.setView(dialogView);
TextView mTextView_errors=dialogView.findviewbyd(R.id.TextView_errors);
String temp=getResources().getString(R.String.errors)+不正确计数;
mTextView_错误。setText(临时);
TextView mTextView_timeUsed=dialogView.findviewbyd(R.id.TextView_timeUsedText);
String temp2=getResources().getString(R.String.time_used)+mTimeUsed;
mTextView_timeUsed.setText(temp2);
final AlertDialog AlertDialog=dialog.create();
最终意图=新意图(此,MainActivity.class);
最终按钮btnReturnToHome=dialogView.findViewById(R.id.Button\u return\u to\u home);
btnReturnToHome.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
mBackground.release();
mCorrect.release();
mWrong.release();
星触觉(意向);

完成();//您是否在实际设备上而不是模拟器上试用过您的应用程序?我注意到您的GLES库无法加载,我对您的情况不是百分之百满意,但GLES通常无法在模拟器上正常工作。

在浏览完所有互联网后,我发现了一些类似于我的应用程序的东西,并在我的代码上实现了它,现在它可以正常工作了很明显。但是,自从更新Android Studio之后,我仍然无法在真实设备上运行它。现在需要看看。顺便说一句,下面是我自己问题的解决方案。希望这能帮助其他使用AlertDialog和Media player并面临同样问题的人

@Override
    protected void onPause() {
        super.onPause();
        Log.wtf("OnPause is executed--------------------", "<----------");
        if(mBackground != null){
            //mBackground.stop();  <<<----This was also in the code that I found online, but it was causing error for me so I after commenting, it worked.
            mBackground.release();
            mBackground = null;
        }
        if(mCorrect != null){
            //mCorrect.stop();
            mCorrect.release();
            mCorrect = null;
        }
        if(mWrong != null){
            //mWrong.stop();
            mWrong.release();
            mWrong = null;
        }
    }
    @Override
    protected void onResume() {
        super.onResume();

            Log.wtf("OnResume is executed--------------------", "<----------");
            if (mBackground == null || !mBackground.isPlaying()){ 
                Log.wtf("on resume---", "<---player null or not playing");
                mBackground = MediaPlayer.create(AlphabetGame.this, R.raw.mursal_bensound_littleidea);
            }
            if (mBackground.isPlaying()){
                Log.wtf("on resume---", "<---- player playing");
                mBackground.stop();
                mBackground.release();
                mBackground = MediaPlayer.create(AlphabetGame.this, R.raw.mursal_bensound_littleidea);
            }
            if (mCorrect == null || !mCorrect.isPlaying()){
                Log.wtf("on resume---", "<---player null or not playing");
                mCorrect = MediaPlayer.create(AlphabetGame.this, R.raw.correct_choice);
            }
            if (mCorrect.isPlaying()){ 
                Log.wtf("on resume---", "<---- player playing");
                mCorrect.stop();
                mCorrect.release();
                mCorrect = MediaPlayer.create(AlphabetGame.this, R.raw.correct_choice);
            }
            if (mWrong == null || !mWrong.isPlaying()){
                Log.wtf("on resume---", "<---player null or not playing");
                mWrong = MediaPlayer.create(AlphabetGame.this, R.raw.wrong_choice);
            }
            if (mWrong.isPlaying()){
                Log.wtf("on resume---", "<---- player playing");
                mWrong.stop();
                mWrong.release();
                mWrong = MediaPlayer.create(AlphabetGame.this, R.raw.wrong_choice);
            }
            mBackground.start();
            mCorrect.start();
            mWrong.start();

    }```
@覆盖
受保护的void onPause(){
super.onPause();

Log.wtf(“OnPause已执行-----------------”,“post logcat异常。很抱歉,它确实存在,但无法找出如何将纯文本从代码中分离出来,因此所有内容都混淆了。我认为您需要重新创建媒体播放器,尝试在OnPause()中释放并重新创建onResume(),但这次内存泄漏,有3个错误指向其他地方。谢谢,但实际上它在两个不同的实际设备上完全失败。
2019-07-28 22:11:58.925 12873-12873/com.example.alphabetsforkids E/lphabetsforkid: Unknown bits set in runtime_flags: 0x8000
2019-07-28 22:12:00.003 12873-12897/com.example.alphabetsforkids E/vndksupport: Could not load /vendor/lib/egl/libGLES_emulation.so from sphal namespace: dlopen failed: library "/vendor/lib/egl/libGLES_emulation.so" not found.
2019-07-28 22:12:00.004 12873-12897/com.example.alphabetsforkids E/libEGL: load_driver(/vendor/lib/egl/libGLES_emulation.so): unknown
2019-07-28 22:12:02.358 12873-12896/com.example.alphabetsforkids E/EGL_emulation: tid 12896: eglQueryString(923): error 0x3008 (EGL_BAD_DISPLAY)
2019-07-28 22:13:42.203 13035-13061/com.example.alphabetsforkids E/vndksupport: Could not load /vendor/lib/egl/libGLES_emulation.so from sphal namespace: dlopen failed: library "/vendor/lib/egl/libGLES_emulation.so" not found.
2019-07-28 22:13:42.203 13035-13061/com.example.alphabetsforkids E/libEGL: load_driver(/vendor/lib/egl/libGLES_emulation.so): unknown
2019-07-28 22:13:43.663 13035-13060/com.example.alphabetsforkids E/EGL_emulation: tid 13060: eglQueryString(923): error 0x3008 (EGL_BAD_DISPLAY)
2019-07-28 22:13:47.845 13035-13035/com.example.alphabetsforkids E/OnResume is executed--------------------: <----------
2019-07-28 22:14:00.320 13035-13035/com.example.alphabetsforkids E/OnPause is executed--------------------: <----------
2019-07-28 22:14:05.848 13035-13035/com.example.alphabetsforkids E/OnResume is executed--------------------: <----------
2019-07-28 22:14:05.882 13035-13035/com.example.alphabetsforkids E/Music started on resume--------------------: <----------
2019-07-28 22:14:05.925 13035-13035/com.example.alphabetsforkids E/MediaPlayerNative: start called in state 1, mPlayer(0x0)
2019-07-28 22:14:05.925 13035-13035/com.example.alphabetsforkids E/MediaPlayerNative: error (-38, 0)
2019-07-28 22:14:05.927 13035-13035/com.example.alphabetsforkids E/MediaPlayerNative: start called in state 1, mPlayer(0x0)
2019-07-28 22:14:05.928 13035-13035/com.example.alphabetsforkids E/MediaPlayerNative: error (-38, 0)
2019-07-28 22:14:05.929 13035-13035/com.example.alphabetsforkids E/MediaPlayerNative: start called in state 1, mPlayer(0x0)
2019-07-28 22:14:05.930 13035-13035/com.example.alphabetsforkids E/MediaPlayerNative: error (-38, 0)
2019-07-28 22:14:06.828 13035-13035/com.example.alphabetsforkids E/MediaPlayer: Error (-38,0)
2019-07-28 22:14:06.831 13035-13035/com.example.alphabetsforkids E/MediaPlayer: Error (-38,0)
2019-07-28 22:14:20.377 13035-13035/com.example.alphabetsforkids E/MediaPlayerNative: start called in state 0, mPlayer(0x0)
2019-07-28 22:14:23.544 13035-13035/com.example.alphabetsforkids E/MediaPlayerNative: start called in state 0, mPlayer(0x0)
2019-07-28 22:14:23.553 13035-13035/com.example.alphabetsforkids E/time is greater than best time:  ----
2019-07-28 22:14:25.509 13035-13035/com.example.alphabetsforkids E/OnPause is executed--------------------: <----------
2019-07-28 22:14:25.521 13035-13035/com.example.alphabetsforkids E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.alphabetsforkids, PID: 13035
    java.lang.RuntimeException: Unable to pause activity {com.example.alphabetsforkids/com.example.alphabetsforkids.AlphabetGame}: java.lang.IllegalStateException
        at android.app.ActivityThread.performPauseActivityIfNeeded(ActivityThread.java:4306)
        at android.app.ActivityThread.performPauseActivity(ActivityThread.java:4257)
        at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:4209)
        at android.app.servertransaction.PauseActivityItem.execute(PauseActivityItem.java:46)
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1935)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7116)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:925)
     Caused by: java.lang.IllegalStateException
        at android.media.MediaPlayer._reset(Native Method)
        at android.media.MediaPlayer.reset(MediaPlayer.java:2145)
        at com.example.alphabetsforkids.AlphabetGame.onPause(AlphabetGame.java:365)
        at android.app.Activity.performPause(Activity.java:7874)
        at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1500)
        at android.app.ActivityThread.performPauseActivityIfNeeded(ActivityThread.java:4296)
        at android.app.ActivityThread.performPauseActivity(ActivityThread.java:4257) 
        at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:4209) 
        at android.app.servertransaction.PauseActivityItem.execute(PauseActivityItem.java:46) 
        at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:176) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:97) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1935) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7116) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:925) 
2019-07-28 22:14:26.182 13144-13168/com.example.alphabetsforkids E/vndksupport: Could not load /vendor/lib/egl/libGLES_emulation.so from sphal namespace: dlopen failed: library "/vendor/lib/egl/libGLES_emulation.so" not found.
2019-07-28 22:14:26.182 13144-13168/com.example.alphabetsforkids E/libEGL: load_driver(/vendor/lib/egl/libGLES_emulation.so): unknown
2019-07-28 22:14:26.919 13144-13167/com.example.alphabetsforkids E/EGL_emulation: tid 13167: eglQueryString(923): error 0x3008 (EGL_BAD_DISPLAY)
@Override
    protected void onPause() {
        super.onPause();
        Log.wtf("OnPause is executed--------------------", "<----------");
        if(mBackground != null){
            //mBackground.stop();  <<<----This was also in the code that I found online, but it was causing error for me so I after commenting, it worked.
            mBackground.release();
            mBackground = null;
        }
        if(mCorrect != null){
            //mCorrect.stop();
            mCorrect.release();
            mCorrect = null;
        }
        if(mWrong != null){
            //mWrong.stop();
            mWrong.release();
            mWrong = null;
        }
    }
    @Override
    protected void onResume() {
        super.onResume();

            Log.wtf("OnResume is executed--------------------", "<----------");
            if (mBackground == null || !mBackground.isPlaying()){ 
                Log.wtf("on resume---", "<---player null or not playing");
                mBackground = MediaPlayer.create(AlphabetGame.this, R.raw.mursal_bensound_littleidea);
            }
            if (mBackground.isPlaying()){
                Log.wtf("on resume---", "<---- player playing");
                mBackground.stop();
                mBackground.release();
                mBackground = MediaPlayer.create(AlphabetGame.this, R.raw.mursal_bensound_littleidea);
            }
            if (mCorrect == null || !mCorrect.isPlaying()){
                Log.wtf("on resume---", "<---player null or not playing");
                mCorrect = MediaPlayer.create(AlphabetGame.this, R.raw.correct_choice);
            }
            if (mCorrect.isPlaying()){ 
                Log.wtf("on resume---", "<---- player playing");
                mCorrect.stop();
                mCorrect.release();
                mCorrect = MediaPlayer.create(AlphabetGame.this, R.raw.correct_choice);
            }
            if (mWrong == null || !mWrong.isPlaying()){
                Log.wtf("on resume---", "<---player null or not playing");
                mWrong = MediaPlayer.create(AlphabetGame.this, R.raw.wrong_choice);
            }
            if (mWrong.isPlaying()){
                Log.wtf("on resume---", "<---- player playing");
                mWrong.stop();
                mWrong.release();
                mWrong = MediaPlayer.create(AlphabetGame.this, R.raw.wrong_choice);
            }
            mBackground.start();
            mCorrect.start();
            mWrong.start();

    }```