列表上的对话框 onListItemClick() 方法..它给出 BadTokenException
我想在列表上设置对话框。当用户选择列表项时,它应该显示带有“是”/“否”的警报框。我已经尝试过这样。但它给出了 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)
请帮助我
提前致谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
试试这个
try this
好吧,您在这里使用了错误的上下文,请尝试使用您传递给 AlertDialog.Builder 的正确上下文,并且
你的问题将会解决。
您已在
onListItemClick
中使用了此内容,并且传递了
this
来创建 AlertDialog.Builder,这是错误的,您必须在那里传递 Activity 或应用程序的上下文。Well you are using the wrong
context
here, try to use the right context that you are passing to the AlertDialog.Builder andyour problem will be done.
You have used this
inside the
onListItemClick
and you are passingthis
to create the AlertDialog.Builder which is wrong, you have to pass a context of Activity or Application there.如果选项卡的内容是活动,那么您需要将 TabActivity 的上下文传递给警报对话框等内容,而不是内容活动的上下文。
试试这个:
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:
尝试下面,代码
Try below, Code