1

I want to setup the Dialog on list.When the user select the list item, it should show alert box with "Yes" /"No".I have tried like this. but it give BadTokenException

     @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        HashMap<String, String> itemAtPosition = (HashMap<String, String>) l.getItemAtPosition(position);
        HashMap<String, String> hashMap = itemAtPosition;
        keyword = hashMap.get("routeCode");
        positions = position;
        if(itinerarySelection.equals("1") || itinerarySelection.equals(" ") ||itinerarySelection == null){
            AlertDialog.Builder routeDefaultQue = new AlertDialog.Builder(this);
            routeDefaultQue.setMessage("Do you want to set this route as defalut route?");
            routeDefaultQue.setCancelable(false);
            routeDefaultQue.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
//                      String[] nameFilds = {"BusinessUnit","ExecutiveCode","VisitNumber","TerritoryCode","RouteCode","VisitDate","StartTime","Status","UploadedOn","UploadedBy"};
//                      String[] valuesFilds = {strBusinessUnit,strExecutive,"",strTerritoryCode,keyword,DateFormat.getInstance().format(date),DateFormat.getTimeInstance(DateFormat.SHORT).format(date),"1",DateFormat.getInstance().format(date),strExecutive};
//                      insertRecords("", nameFilds, valuesFilds);
//                      getListView().getChildAt(positions).setBackgroundColor(R.color.red);
                }
            });
            routeDefaultQue.setNegativeButton("No", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    dialog.cancel();
                }
            });

            routeDefaultQue.setTitle("Default Route setup...");
            routeDefaultQue.setIcon(R.drawable.icon);
            routeDefaultQue.show();
         }

This is i am doing this list on activityGroup.

This is error

    08-25 10:29:46.809: ERROR/AndroidRuntime(597): FATAL EXCEPTION: main
08-25 10:29:46.809: ERROR/AndroidRuntime(597): android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@4056e178 is not valid; is your activity running?
08-25 10:29:46.809: ERROR/AndroidRuntime(597):     at android.view.ViewRoot.setView(ViewRoot.java:527)
08-25 10:29:46.809: ERROR/AndroidRuntime(597):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
08-25 10:29:46.809: ERROR/AndroidRuntime(597):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
08-25 10:29:46.809: ERROR/AndroidRuntime(597):     at android.view.Window$LocalWindowManager.addView(Window.java:424)
08-25 10:29:46.809: ERROR/AndroidRuntime(597):     at android.app.Dialog.show(Dialog.java:241)
08-25 10:29:46.809: ERROR/AndroidRuntime(597):     at android.app.AlertDialog$Builder.show(AlertDialog.java:802)
08-25 10:29:46.809: ERROR/AndroidRuntime(597):     at com.xont.controller.sales.SalesRouteActivity.onListItemClick(SalesRouteActivity.java:145)
08-25 10:29:46.809: ERROR/AndroidRuntime(597):     at android.app.ListActivity$2.onItemClick(ListActivity.java:319)
08-25 10:29:46.809: ERROR/AndroidRuntime(597):     at android.widget.AdapterView.performItemClick(AdapterView.java:284)
08-25 10:29:46.809: ERROR/AndroidRuntime(597):     at android.widget.ListView.performItemClick(ListView.java:3513)
08-25 10:29:46.809: ERROR/AndroidRuntime(597):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812)
08-25 10:29:46.809: ERROR/AndroidRuntime(597):     at android.os.Handler.handleCallback(Handler.java:587)
08-25 10:29:46.809: ERROR/AndroidRuntime(597):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-25 10:29:46.809: ERROR/AndroidRuntime(597):     at android.os.Looper.loop(Looper.java:123)
08-25 10:29:46.809: ERROR/AndroidRuntime(597):     at android.app.ActivityThread.main(ActivityThread.java:3683)
08-25 10:29:46.809: ERROR/AndroidRuntime(597):     at java.lang.reflect.Method.invokeNative(Native Method)
08-25 10:29:46.809: ERROR/AndroidRuntime(597):     at java.lang.reflect.Method.invoke(Method.java:507)
08-25 10:29:46.809: ERROR/AndroidRuntime(597):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-25 10:29:46.809: ERROR/AndroidRuntime(597):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-25 10:29:46.809: ERROR/AndroidRuntime(597):     at dalvik.system.NativeStart.main(Native Method)

Please help me

Thanks in advance

Samuel
  • 9,883
  • 5
  • 45
  • 57
Piraba
  • 6,974
  • 17
  • 85
  • 135

4 Answers4

1
AlertDialog.Builder routeDefaultQue = new AlertDialog.Builder(yourRunningActivity.this);

try this

Mohammed Azharuddin Shaikh
  • 41,633
  • 14
  • 96
  • 115
1

Well you are using the wrong context here, try to use the right context that you are passing to the AlertDialog.Builder and your problem will be done.

You have used this

AlertDialog.Builder routeDefaultQue = new AlertDialog.Builder(this); 

inside the onListItemClick and you are passing this to create the AlertDialog.Builder which is wrong, you have to pass a context of Activity or Application there.

Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
  • Yes.This correct. `AlertDialog.Builder routeDefaultQue = new AlertDialog.Builder(getParent());` – Piraba Aug 25 '11 at 05:20
1

if the content of a Tab is an Activity then you need to pass the context of the TabActivity to things like alert dialogs rather than the content activity's context.

Try this:

AlertDialog.Builder routeDefaultQue = new AlertDialog.Builder(getParent());
Piraba
  • 6,974
  • 17
  • 85
  • 135
1

Try below, Code

AlertDialog.Builder routeDefaultQue = new AlertDialog.Builder(getApplicationContext());
Niranj Patel
  • 32,980
  • 10
  • 97
  • 133