Ad

Android Calling Custom Alertdialog Class

- 1 answer

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)
Ad

Answer

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);
Ad
source: stackoverflow.com
Ad