1

在我的 android 应用程序中,我使用的是自定义对话框。当我尝试显示对话框时,会导致错误。我不知道我做错了什么,我真的很困惑。

这是我的代码:

protected Dialog onCreateDialog(int id) {
    Dialog dialog;
    switch(id) {
    case 0:
        dialog = new Dialog(getApplicationContext());

        dialog.setContentView(R.layout.paused);
        dialog.setTitle("Game Paused");
        dialog.show();
        break;
    default:
        dialog = null;
    }
    return null;
}

这是我显示对话框的方式

showDialog(0);

顺便说一句,它说 showDialog(int) 已被弃用。

安卓日志猫:

> 06-19 18:44:22.399: W/dalvikvm(467): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
06-19 18:44:22.419: E/AndroidRuntime(467): FATAL EXCEPTION: main
06-19 18:44:22.419: E/AndroidRuntime(467): java.lang.IllegalStateException: Could not execute method of the activity
06-19 18:44:22.419: E/AndroidRuntime(467):  at android.view.View$1.onClick(View.java:2072)
06-19 18:44:22.419: E/AndroidRuntime(467):  at android.view.View.performClick(View.java:2408)
06-19 18:44:22.419: E/AndroidRuntime(467):  at android.view.View$PerformClick.run(View.java:8816)
06-19 18:44:22.419: E/AndroidRuntime(467):  at android.os.Handler.handleCallback(Handler.java:587)
06-19 18:44:22.419: E/AndroidRuntime(467):  at android.os.Handler.dispatchMessage(Handler.java:92)
06-19 18:44:22.419: E/AndroidRuntime(467):  at android.os.Looper.loop(Looper.java:123)
06-19 18:44:22.419: E/AndroidRuntime(467):  at android.app.ActivityThread.main(ActivityThread.java:4627)
06-19 18:44:22.419: E/AndroidRuntime(467):  at java.lang.reflect.Method.invokeNative(Native Method)
06-19 18:44:22.419: E/AndroidRuntime(467):  at java.lang.reflect.Method.invoke(Method.java:521)
06-19 18:44:22.419: E/AndroidRuntime(467):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-19 18:44:22.419: E/AndroidRuntime(467):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-19 18:44:22.419: E/AndroidRuntime(467):  at dalvik.system.NativeStart.main(Native Method)
06-19 18:44:22.419: E/AndroidRuntime(467): Caused by: java.lang.reflect.InvocationTargetException
06-19 18:44:22.419: E/AndroidRuntime(467):  at com.jlennon.gametest.PlayGameActivity.pause(PlayGameActivity.java:105)
06-19 18:44:22.419: E/AndroidRuntime(467):  at java.lang.reflect.Method.invokeNative(Native Method)
06-19 18:44:22.419: E/AndroidRuntime(467):  at java.lang.reflect.Method.invoke(Method.java:521)
06-19 18:44:22.419: E/AndroidRuntime(467):  at android.view.View$1.onClick(View.java:2067)
06-19 18:44:22.419: E/AndroidRuntime(467):  ... 11 more
06-19 18:44:22.419: E/AndroidRuntime(467): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
06-19 18:44:22.419: E/AndroidRuntime(467):  at android.view.ViewRoot.setView(ViewRoot.java:509)
06-19 18:44:22.419: E/AndroidRuntime(467):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
06-19 18:44:22.419: E/AndroidRuntime(467):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
06-19 18:44:22.419: E/AndroidRuntime(467):  at android.app.Dialog.show(Dialog.java:241)
06-19 18:44:22.419: E/AndroidRuntime(467):  at com.jlennon.gametest.PlayGameActivity.onCreateDialog(PlayGameActivity.java:131)
06-19 18:44:22.419: E/AndroidRuntime(467):  at android.app.Activity.onCreateDialog(Activity.java:2472)
06-19 18:44:22.419: E/AndroidRuntime(467):  at android.app.Activity.createDialog(Activity.java:881)
06-19 18:44:22.419: E/AndroidRuntime(467):  at android.app.Activity.showDialog(Activity.java:2547)
06-19 18:44:22.419: E/AndroidRuntime(467):  at android.app.Activity.showDialog(Activity.java:2514)
06-19 18:44:22.419: E/AndroidRuntime(467):  ... 15 more

提前非常感谢!

4

6 回答 6

2

试试这个代码

 protected Dialog onCreateDialog(int id) {
  Dialog dialog;
  switch(id) {
   case 0:
    dialog = new Dialog(this);

    dialog.setContentView(R.layout.paused);
    dialog.setTitle("Game Paused");
    dialog.show();
    break;
default:
    dialog = null;
}
return null;

}

我刚刚将 dialog = new Dialog(getApplicationContext()) 替换为 dialog = new Dialog(this);

于 2012-06-20T02:17:42.890 回答
2
Dialog dialog = new Dialog(YourActivity.this);
dialog.show();

而不是上面的代码。

我会用这个。

AlertDialog.Builder dialog = AlertDialog.Builder(this);

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View v = inflater.inflate(R.layout.yourlayout,null,false);

dialog.setView(v);

dialog.show();

顺便说一句,onCreateDialog(int)已弃用;

于 2012-06-20T02:20:52.803 回答
2

我创建了一个自定义对话框。像这样..

在此处输入图像描述

xml是..

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/createpost_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/cre"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:gravity="center_horizontal"
            android:text="CREATE POST"
            android:textColor="@color/mytextcolor"
            android:textSize="20sp" />

        <EditText
            android:id="@+id/topic_ev"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@drawable/txt_field"
            android:hint="topic" />

        <EditText
            android:id="@+id/description_ev"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="@drawable/destxt_field"
            android:hint="description"
            android:inputType="textMultiLine" />

        <Button
            android:id="@+id/upload_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="left"
            android:layout_margin="10dp"
            android:background="@drawable/upload_btn" />

        <TextView
            android:id="@+id/textu"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:text="UPLOAD A FILE JPEG,GIF OR PNG 3MB Max"
            android:textSize="10sp" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/layout_forbuttons"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/createpost_layout"
        android:layout_margin="10dp"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/post_btn"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_weight="1"
            android:background="@drawable/post_btn" />

        <Button
            android:id="@+id/cancel_btn"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_weight="1"
            android:background="@drawable/cancel_btn" />
    </LinearLayout>

</RelativeLayout>

和 CustomDialogClass 是

    import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;

import com.example.fragmentaltdd.R;

public class CustomDialogClass extends Dialog implements
android.view.View.OnClickListener {

    public Activity c;
    public Dialog d;
    public Button Post, Cancel,Upload;

    public CustomDialogClass(Activity a)
    {
        super(a);

        this.c = a;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);


        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.createpost);

        Post = (Button) findViewById(R.id.post_btn);

        Cancel = (Button) findViewById(R.id.cancel_btn);

        Upload = (Button)findViewById(R.id.upload_btn);

        Post.setOnClickListener(this);

        Cancel.setOnClickListener(this);

        Upload.setOnClickListener(this);

    }

    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.post_btn:
              //c.finish();
                Toast.makeText(c, "Post button clikced", Toast.LENGTH_LONG).show();
              break;

            case R.id.upload_btn:
                 // c.finish();
                Toast.makeText(c, "Upload button clikced", Toast.LENGTH_LONG).show();
                  break;

            case R.id.cancel_btn:
              dismiss();
              break;

            default:
              break;
        }
        dismiss();
    }
}

像这样称呼它

    CustomDialogClass cdd = new CustomDialogClass(getActivity());//while calling from fragment

CustomDialogClass cdd = new CustomDialogClass(YourActivity.this);//while calling from Activity

            cdd.show();
于 2014-07-04T11:51:43.773 回答
1

首先,您使用的方法是错误的。该createDialog方法的重点是创建对话框然后返回它。您在方法中显示对话框,然后根本不返回它,这完全违背了覆盖该方法的目的。你真的应该有你自己的方法来创建和显示你的对话框。

其次,正如 AVD 在对您的问题的评论中发布的那样,对于针对 Honeycomb (11) 或更高版本的 API 的应用程序,不推荐使用这种显示对话框的方法。

但是,您的问题是您正在使用 Application Context( getApplicationContext()) 对象来显示一个应该属于您的Activity. 您应该传入this以使用Activity上下文,这将修复您的错误。

于 2012-06-20T02:15:03.857 回答
0

试试这样:

import android.app.AlertDialog;

new AlertDialog.Builder(YourActivityName.this)
    .setTitle("Game Paused")
    .setPositiveButton("OK", null)
    .show();
于 2012-06-20T01:56:49.483 回答
0

文档说:

Dialog 类是创建对话框的基类。但是,您通常不应该直接实例化 Dialog。相反,您应该使用以下子类之一:

警报对话框

进度对话框

日期选择器对话框

时间选择器对话框

来源:http: //developer.android.com/guide/topics/ui/dialogs.html

Google 似乎没有在本文档或 Dialog 类的实际 API 参考中给出原因。正如您已经知道的那样,错误消息也没有。所以我不确定为什么这个类没有“受保护”。

无论如何,使用上述类之一应该可以解决您的问题。

如果有人知道为什么我们不能直接使用 Dialog 类,请发表评论。

于 2012-06-20T02:13:09.787 回答