我想在列表上设置对话框。当用户选择列表项时,它应该显示警告框“是”/“否”。我试过这样的。但它给出 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();
}
这是我在activityGroup上的这个列表。
这是错误
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)
请帮帮我
提前致谢
答案 0 :(得分:1)
AlertDialog.Builder routeDefaultQue = new AlertDialog.Builder(yourRunningActivity.this);
试试这个
答案 1 :(得分:1)
你在这里使用错误的context
,尝试使用你传递给AlertDialog.Builder的正确上下文
你的问题将会完成。
您已使用此
AlertDialog.Builder routeDefaultQue = new AlertDialog.Builder(this);
在onListItemClick
内,你传递this
来创建错误的AlertDialog.Builder,你必须在那里传递Activity或Application的上下文。
答案 2 :(得分:1)
如果Tab的内容是Activity,那么您需要将TabActivity的上下文传递给警报对话框而不是内容活动的上下文。
试试这个:
AlertDialog.Builder routeDefaultQue = new AlertDialog.Builder(getParent());
答案 3 :(得分:1)
请尝试以下代码
AlertDialog.Builder routeDefaultQue = new AlertDialog.Builder(getApplicationContext());