简体   繁体   中英

Android calling custom alertdialog class

Here my custom dialogalertdialog class. I want to create custom dialog and i call every activity but i got the some errors. i could not fix it. I dont know how can i fix it.

public class AlertForSelection extends AlertDialog {
private Context context;
private List<UpperCategory> lstUpper;
private int id;
private LinearLayout lnrList;
private String name;

public AlertForSelection(Context context, List<UpperCategory> lstUpper) {
    super(context);
    this.context = context;
    this.lstUpper = lstUpper;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.alert_for_selection_view);
    this.lnrList = (LinearLayout) findViewById(R.id.lnrList);
    this.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
  }
}

here i called with button click.

final AlertForSelection alertForSelection = new
AlertForSelection(getApplicationContext(), listUpperCategory);
alertForSelection.show();

this is error

android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
                                                                              at android.view.ViewRootImpl.setView(ViewRootImpl.java:769)
                                                                              at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:356)
                                                                              at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:94)
                                                                              at android.app.Dialog.show(Dialog.java:330)
                                                                              at com.example.samcro.petshop.Activities.NewActivity.BtnUpCategory_Click(NewActivity.java:80)
                                                                              at com.example.samcro.petshop.Activities.NewActivity.access$000(NewActivity.java:24)
                                                                              at com.example.samcro.petshop.Activities.NewActivity$1.onClick(NewActivity.java:56)
                                                                              at android.view.View.performClick(View.java:6294)
                                                                              at android.view.View$PerformClick.run(View.java:24774)
                                                                              at android.os.Handler.handleCallback(Handler.java:790)
                                                                              at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                              at android.os.Looper.loop(Looper.java:164)
                                                                              at android.app.ActivityThread.main(ActivityThread.java:6518)
                                                                              at java.lang.reflect.Method.invoke(Native Method)
                                                                              at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
                                                                              at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

you can't use the "getApplicationContext() " to new a AlertDialog , you should attach a activity context to new a AlertDialog,because

    Dialog(@NonNull Context context, @StyleRes int themeResId, boolean createContextThemeWrapper) {
    // something not important
    mWindowManager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);

    final Window w = new PhoneWindow(mContext);
    mWindow = w;
    w.setCallback(this);
    w.setOnWindowDismissedCallback(this);
    w.setWindowManager(mWindowManager, null, null);
    w.setGravity(Gravity.CENTER);

    mListenersHandler = new ListenersHandler(this);
  }

here the new Dialog() function will use the context to new mWindowManager, when u use show()

 public void show() {
    // something not important
    mDecor = mWindow.getDecorView();

    WindowManager.LayoutParams l = mWindow.getAttributes();
    if ((l.softInputMode
            & WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) == 0) {
        WindowManager.LayoutParams nl = new WindowManager.LayoutParams();
        nl.copyFrom(l);
        nl.softInputMode |=
                WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION;
        l = nl;
    }

    try {
        mWindowManager.addView(mDecor, l);
        mShowing = true;

        sendShowMessage();
    } finally {
    }
}

here mWindowManager needs addView ,but if the context is applicationContext, the token of mWindowManager will be null. if the context is activity, activity will bind its token to mWindowManager, it can run well.

If you're using getApplicationContext() as Context for the dialog like this

AlertForSelection alertForSelection = new AlertForSelection(getApplicationContext(), listUpperCategory);

then use YourActivityName.this

AlertForSelection alertForSelection = new AlertForSelection(YourActivityName.this, listUpperCategory);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM