java - 无法在我的程序结构中创建 AlertDialog

标签 java android dialog android-alertdialog

我正在制作我的第一个 Android 游戏,并且我已经有了用 Java 和 C# 制作简单游戏的知识来实现​​我想要实现的一般内容。为此,我有扩展 Activity 的主类,然后创建扩展 SurfaceView 的 GameView 类的对象,并将 ContentView 设置为该类。 这是其中的一些代码:

public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    gameView = new GameView(this);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(gameView);

在我的 GameView 类中,我拥有简单游戏的所有常用部分,包括循环和 Sprite 对象等。这一切都工作得很好,直到打开一个 AlertDialog 以获取用户的文本输入以保存游戏。这就是我担心我“错过了 android 编程要点”的部分,因为我已经尝试实现所示的解决方案

这里: Android AlertDialog inside AsyncTask

这里: How do I display an alert dialog on Android?

这里: http://www.androidsnippets.com/prompt-user-input-with-an-alertdialog

我尝试过几次时收到错误“无法在未调用 Looper.prepare() 的线程内创建处理程序”。我已经快要完成这个项目了,现在我想我已经错过了基本的 Android 编程结构。问题是我真的不知道应该在哪里创建和显示 AlertDialog,它应该在主 Activity 中,还是在 GameView SurfaceView 中。如果它在主 Activity 中,我如何退出 GameView 回到 Activity 中,并返回一些数据供下一个( View ?)在 AlertDialog 中使用。

如果这看起来太模糊,我很抱歉,因为我试图创建一个 AlertDialog,它超出了我的理解,我觉得我错过了一些东西。

所以我的问题是,我的结构有什么问题,更具体地说,我应该把 AlertDialog 放在哪里;我可以在从 GameView 类调用的某个方法或类中运行它,还是需要退出/结束该类回到主 Activity ,并在其中运行它?

private class myAsyncTask extends AsyncTask<String, Void, String> {

AlertDialog alertDialog;
protected void onPreExecute() 
{
    super.onPreExecute();
    alertDialog = new AlertDialog.Builder(this);
}
@Override
protected String doInBackground(String... params)
{       
       return null;
}
@Override
protected void onPostExecute(String result) 
{
       super.onPostExecute(result);

       alertDialog.setTitle("The Process");  
       //alertDialog.setIcon(R.drawable.success);
       alertDialog.setCanceledOnTouchOutside(false);
       alertDialog.setMessage("All done!");  
       alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
       new DialogInterface.OnClickListener() 
       {
                       public void onClick(DialogInterface dialog, int which) 
                       {

                       }
                               });
       alertDialog.setOnDismissListener(new DialogInterface.OnDismissListener() 
       {          
 @Override
 public void onDismiss(DialogInterface dialog) 
 {

 }
 });
 alertDialog.show();
 }

}

最佳答案

您必须在 onPostExecute 方法中显示警报对话,然后只有您可以显示。您可以在后台运行方法中显示警报对话。

首选以下链接:

Android AlertDialog inside AsyncTask

已更新

 Instead of AlertDialog alertDialog use
 AlertDialog.Builder alertDialogBuilder

然后创建对话框

final AlertDialog alertDailog = alertDialogBuilder.create();

最后:

alertDailog.show();

关于java - 无法在我的程序结构中创建 AlertDialog,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22817953/

相关文章:

android - 如何在 Android 中创建完全自定义的 Dialogue/Popup(更改覆盖颜色和对话窗口布局)

c# - 'System.Nullable<bool >' does not contain a definition for ' OK'

wpf - 打开窗口作为MVVM中的对话框

java - 在 Java 中将日期从 UTC 转换为 EST?

java - 如何在 AWS Secret Manager 中存储 JKS 证书?

android - Card_view Material 设计

android - YouTube iframe 嵌入无法在 Android 上自动播放

java - 如何在 ViewPager Fragment 中更改 View

java - 使用 EasyMock 测试简单的多线程代码,但得到了奇怪的结果

android - 更新支持库后 RecyclerView 无法工作