Android-Show Custom Dialog

馋奶兔 提交于 2020-01-11 11:38:11

问题


In my android application, I am using a custom dialog. When I try to show the dialog, it causes an error. I don't know what I am doing wrong, and I am really confused.

Here is my code:

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;
}

And here is how I show the dialog

showDialog(0);

By the way, it says that showDialog(int) is deprecated.

Android logcat:

> 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

Thanks a whole lot in advance!


回答1:


Try this code

 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;

}

I just replaced dialog = new Dialog(getApplicationContext()) to dialog = new Dialog(this);




回答2:


Dialog dialog = new Dialog(YourActivity.this);
dialog.show();

Instead of the code above.

I will use this.

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();

By the way, onCreateDialog(int) is deprecated;




回答3:


i have created a CustomDialog. like this..

the xml is ..

    <?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>

and CustomDialogClass is

    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();
    }
}

And call it like this

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

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

            cdd.show();



回答4:


First of all, you're using that method wrong. The point of the createDialog method is to, well, create the dialog and then return it. You're showing the dialog within the method, then not returning it at all, which defeats the purpose of overriding that method at all. You really should just have your own method that creates and displays your dialog.

Second of all, as AVD posted in the comments to your question, that method of displaying dialogs is deprecated for apps targeting an API for Honeycomb (11) or later.

Your problem, however, is that you're using the Application Context (getApplicationContext()) object to display a dialog that should belong to your Activity. You should pass in this instead to use the Activity context and that will fix your error.




回答5:


Try it like this:

import android.app.AlertDialog;

new AlertDialog.Builder(YourActivityName.this)
    .setTitle("Game Paused")
    .setPositiveButton("OK", null)
    .show();



回答6:


Documentation says:

The Dialog class is the base class for creating dialogs. However, you typically should not instantiate a Dialog directly. Instead, you should use one of the following subclasses:

AlertDialog

ProgressDialog

DatePickerDialog

TimePickerDialog

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

Google doesn't seems to give a reason for this on this document, or the actual API reference for the Dialog class. And as you already know, the error message doesn't either. So I'm not sure why the class is not 'protected'.

Anyway, using one of the above classes should solve your issue.

Please comment if anyone knows why we can't use the Dialog class directly.



来源:https://stackoverflow.com/questions/11111796/android-show-custom-dialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!