当前位置: 首页 > 知识库问答 >
问题:

AlertDialog:显示对话框后,背景画布渲染继续,但对话框从未关闭

钱稳
2023-03-14

以下是我的AlertDialog无响应状态的屏幕截图:

在背景中,绿色瓷砖、黑色和白色目标中的红色球,所有三个位图都在一个渲染线程上连续渲染,该线程锁定/解锁手机上的背景画布。当我按下OK按钮时,无论我在EditText中输入了什么文本,都会执行此无响应操作。

这是代码:

游戏活动班:

package nttu.edu.activities;

import nttu.edu.graphics.RenderView;
import nttu.edu.handler.Accelero;
import nttu.edu.handler.TouchHandler;
import nttu.edu.score.Score;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;

public class PlayActivity extends Activity implements OnClickListener
{
    public RenderView renderView = null;
    public Accelero accelerometer = null;
    public AssetManager assetManager = null;
    public TouchHandler touchHandler = null;
    public Score leaderboard = null;
    final int REQUEST_CODE = 0x1;

    private EditText input = null;
    private AlertDialog.Builder dialog = null;

    public void onCreate(Bundle b)
    {
        super.onCreate(b);
        accelerometer = new Accelero();
        touchHandler = new TouchHandler();
        leaderboard = new Score(this);
        renderView = new RenderView(this, accelerometer, touchHandler);
        renderView.setOnTouchListener(touchHandler);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        input = new EditText(this);
        dialog = new AlertDialog.Builder(this);
        dialog.setTitle("Enter High Score!");
        dialog.setMessage("Enter your name: ");
        dialog.setView(input);
        dialog.setPositiveButton("OK", PlayActivity.this);
        setContentView(renderView);
    }

    public void onResume()
    {
        super.onResume();
        if(renderView != null)
            renderView.resume();
        if(accelerometer != null)
            accelerometer.resume(this);
    }

    public void onPause()
    {
        super.onPause();
        if(renderView != null)
            renderView.pause();
        if(accelerometer != null)
            accelerometer.pause();
    }

    public void addPlayer()
    {
        // FIXME: The dialog won't exit.

        Looper.prepare();
        dialog.show();
        Looper.loop();
    }

    public void onClick(DialogInterface dialog, int button)
    {
        String name = input.getText().toString();
        if(name == null)
            name = "Player";
        this.leaderboard.open();
        this.leaderboard.createEntry(name, this.renderView.getScore());
        this.leaderboard.close();
        if (Looper.myLooper() != null)
            Looper.myLooper().quit();
        else if (Looper.getMainLooper() != null)
            Looper.getMainLooper().quit();
        Log.d("PlayActivity.onClick()", "I'm escaping from Looper.loop()"); 
        //renderView.running = false;
        //PlayActivity.this.finish();

    }
}

由于未知原因,只有在AlertDialog之前存在准备好的活套时,AlertDialog才会执行。调用show()。

问题是什么时候启动警报对话框。调用show(),并在循环器之后调用。当调用quit()时,AlertDialog不知何故没有退出,这使得手机没有响应。

当我在onClark()方法中调用PlayActivity.this.finish()时,我可以退出对话框,停止背景渲染,然后回到主菜单(这是我想要的),但是Eclipse DDMS报告了这个:

06-26 16:40:29.049: E/WindowManager(1642):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
06-26 16:40:29.049: E/WindowManager(1642):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
06-26 16:40:29.049: E/WindowManager(1642):  at android.view.Window$LocalWindowManager.addView(Window.java:433)
06-26 16:40:29.049: E/WindowManager(1642):  at android.app.Dialog.show(Dialog.java:265)
06-26 16:40:29.049: E/WindowManager(1642):  at android.app.AlertDialog$Builder.show(AlertDialog.java:802)
06-26 16:40:29.049: E/WindowManager(1642):  at nttu.edu.activities.PlayActivity.addPlayer(PlayActivity.java:77)
06-26 16:40:29.049: E/WindowManager(1642):  at nttu.edu.graphics.RenderView.tick(RenderView.java:119)
06-26 16:40:29.049: E/WindowManager(1642):  at nttu.edu.graphics.RenderView.run(RenderView.java:69)
06-26 16:40:29.049: E/WindowManager(1642):  at java.lang.Thread.run(Thread.java:1027)
06-26 16:40:29.079: W/MessageQueue(1642): Handler{405399b8} sending message to a Handler on a dead thread
06-26 16:40:29.079: W/MessageQueue(1642): java.lang.RuntimeException: Handler{405399b8} sending message to a Handler on a dead thread
06-26 16:40:29.079: W/MessageQueue(1642):   at android.os.MessageQueue.enqueueMessage(MessageQueue.java:196)

简而言之,当我想通过调用finish()退出PlayActivity时,AlertDialog会泄漏。我不知道还能做些什么来解决这个问题。

请帮帮我。谢谢!如果你需要更多的源代码,请让我知道。

编辑:

就在泄漏错误出现之前,以下是DDMS记录的内容:

06-26 16:40:22.182: D/Level.render()(1642): Drawing Tee.
06-26 16:40:22.182: D/Level.render()(1642): Drawing Hole.
06-26 16:40:22.182: D/Level.render()(1642): Drawing Ball.
06-26 16:40:22.192: D/Level.render()(1642): Drawing background.
06-26 16:40:22.202: D/Level.render()(1642): Drawing Tee.
06-26 16:40:22.202: D/Level.render()(1642): Drawing Hole.
06-26 16:40:22.202: D/Level.render()(1642): Drawing Ball.
06-26 16:40:22.212: D/Level.render()(1642): Drawing background.
06-26 16:40:22.212: D/Level.render()(1642): Drawing Tee.
06-26 16:40:22.222: D/Level.render()(1642): Drawing Hole.
06-26 16:40:22.222: D/Level.render()(1642): Drawing Ball.
06-26 16:40:22.222: D/FrameRate(1642): 59
06-26 16:40:22.232: D/Level.tick()(1642): Obtaining score.
06-26 16:40:22.232: D/Level.render()(1642): Drawing background.
06-26 16:40:22.232: D/Level.render()(1642): Drawing Tee.
06-26 16:40:22.242: D/Level.render()(1642): Drawing Hole.
06-26 16:40:22.242: D/Level.render()(1642): Drawing Ball.
06-26 16:40:22.242: D/Level.render()(1642): Outputting the phrase: Game Over
06-26 16:40:23.713: D/Database(1642): dbopen(): path = /data/data/nttu.edu/databases/Scoreboard, flag = 6, file size = 5120
06-26 16:40:23.713: D/Database(1642): dbopen(): path = /data/data/nttu.edu/databases/Scoreboard, mode: wal, disk free size: 1014 M, handle: 0x38b4a8
06-26 16:40:23.844: D/Database(1642): dbclose(): path = /data/data/nttu.edu/databases/Scoreboard, handle = 0x38b4a8
06-26 16:40:28.608: D/PlayActivity.onClick()(1642): I'm escaping from Looper.loop()
06-26 16:40:28.618: D/Level.render()(1642): Drawing background.
06-26 16:40:28.628: D/Level.render()(1642): Drawing Tee.
06-26 16:40:28.628: D/Level.render()(1642): Drawing Hole.
06-26 16:40:28.628: D/Level.render()(1642): Drawing Ball.
06-26 16:40:28.628: D/Level.render()(1642): Outputting the phrase: Game Over
06-26 16:40:28.648: D/FrameRate(1642): 2
06-26 16:40:28.798: W/MessageQueue(1642): Handler{4053dbd0} sending message to a Handler on a dead thread
06-26 16:40:28.798: W/MessageQueue(1642): java.lang.RuntimeException: Handler{4053dbd0} sending message to a Handler on a dead thread
06-26 16:40:28.798: W/MessageQueue(1642):   at android.os.MessageQueue.enqueueMessage(MessageQueue.java:196)
06-26 16:40:28.798: W/MessageQueue(1642):   at android.os.Handler.sendMessageAtTime(Handler.java:457)
06-26 16:40:28.798: W/MessageQueue(1642):   at android.os.Handler.sendMessageDelayed(Handler.java:430)
06-26 16:40:28.798: W/MessageQueue(1642):   at android.os.Handler.sendMessage(Handler.java:367)
06-26 16:40:28.798: W/MessageQueue(1642):   at com.android.internal.view.IInputConnectionWrapper.dispatchMessage(IInputConnectionWrapper.java:240)
06-26 16:40:28.798: W/MessageQueue(1642):   at com.android.internal.view.IInputConnectionWrapper.finishComposingText(IInputConnectionWrapper.java:151)
06-26 16:40:28.798: W/MessageQueue(1642):   at com.android.internal.view.IInputContext$Stub.onTransact(IInputContext.java:137)
06-26 16:40:28.798: W/MessageQueue(1642):   at android.os.Binder.execTransact(Binder.java:320)
06-26 16:40:28.798: W/MessageQueue(1642):   at dalvik.system.NativeStart.run(Native Method)
06-26 16:40:28.888: W/MessageQueue(1642): Handler{405399b8} sending message to a Handler on a dead thread
06-26 16:40:28.888: W/MessageQueue(1642): java.lang.RuntimeException: Handler{405399b8} sending message to a Handler on a dead thread
06-26 16:40:28.888: W/MessageQueue(1642):   at android.os.MessageQueue.enqueueMessage(MessageQueue.java:196)
06-26 16:40:28.888: W/MessageQueue(1642):   at android.os.Handler.sendMessageAtTime(Handler.java:457)
06-26 16:40:28.888: W/MessageQueue(1642):   at android.os.Handler.sendMessageDelayed(Handler.java:430)

共有1个答案

龙玄天
2023-03-14

您应该检测应用程序最终到达的所有位置,并使用Dialog.dismiss()

 类似资料:
  • 继承自Popup 对话框是一个原生对话弹出框,显示一个消息与最多三个按钮。关闭时会被自动释放。 使用“const {AlertDialog} = require('tabris');”引入该类。 属性 buttons Type: {ok?: string, cancel?: string, neutral?: string} 含有按钮文本内容的对象。分别是这三种按钮:ok, cancel和neut

  • 在Netbeans中,我创建了一个GUI项目,该项目使用框架前端,我可以添加组件并双击以编辑其事件。我指的是有“源”、“设计”和“历史”选项卡的窗口。 以下是组件和关系: 1-打开文件选择器的按钮。 2-显示文件选择器结果的文本区域。如果用户选择一个文件,它将在文本区域显示文件名;否则它会写“被用户取消”。 3-同时,如果用户选择了一个文件,我想打开一个“please wait”(请等待)对话框,

  • 我希望对话框下有模糊的屏幕,所以我拍摄活动的“截图”,模糊它,并将其设置为可位图绘制的对话框窗口背景。奇怪的是对话框不再集中在屏幕上,即使调用了setCanceledOnTouchOutside(true),触摸外部对话框也不会关闭它。 问题是:为什么这不起作用?分别如何创建背景模糊的对话框?

  • 本文向大家介绍Android AlertDialog实现分享对话框/退出对话框/下载对话框,包括了Android AlertDialog实现分享对话框/退出对话框/下载对话框的使用技巧和注意事项,需要的朋友参考一下 一.摘要 弹窗通常用于提示用户进行某种操作,比如:点击分享按钮,弹窗分享对话框;双击返回按钮,弹窗退出对话框;下载文件,提示下载对话框等等,分享对话框/退出对话框/下载对话框,都可以直

  • 主要内容:本节引言:,1.基本使用流程,2.几种常用的对话框使用示例,3.通过Builder的setView()定制显示的AlertDialog,4.示例代码下载,本节小结:本节引言: 本节继续给大家带来是显示提示信息的第三个控件AlertDialog(对话框),同时它也是其他 Dialog的的父类!比如ProgressDialog,TimePickerDialog等,而AlertDialog的父类是:Dialog! 另外,不像前面学习的Toast和Notification,AlertDialo

  • 正如我们从API 14知道的,下面的模糊已经被弃用 有没有其他方法可以使屏幕模糊我已经尝试过快速模糊