HY。我尝试创建一个警告对话框,但是当我运行我的应用程序时,会抛出异常
09-26 12:43:21.949: E/AndroidRuntime(14618): FATAL EXCEPTION: main
09-26 12:43:21.949: E/AndroidRuntime(14618): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
09-26 12:43:21.949: E/AndroidRuntime(14618): at android.view.ViewRootImpl.setView(ViewRootImpl.java:687)
09-26 12:43:21.949: E/AndroidRuntime(14618): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:301)
09-26 12:43:21.949: E/AndroidRuntime(14618): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:215)
09-26 12:43:21.949: E/AndroidRuntime(14618): at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:140)
09-26 12:43:21.949: E/AndroidRuntime(14618): at android.app.Dialog.show(Dialog.java:278)
09-26 12:43:21.949: E/AndroidRuntime(14618): at com.example.stampp.UI.Rewards.DeleteRewardsFragment$1.onItemClick(DeleteRewardsFragment.java:80)
09-26 12:43:21.949: E/AndroidRuntime(14618): at android.widget.AdapterView.performItemClick(AdapterView.java:292)
09-26 12:43:21.949: E/AndroidRuntime(14618): at android.widget.AbsListView.performItemClick(AbsListView.java:1394)
09-26 12:43:21.949: E/AndroidRuntime(14618): at android.widget.AbsListView$PerformClick.run(AbsListView.java:3024)
09-26 12:43:21.949: E/AndroidRuntime(14618): at android.widget.AbsListView.onTouchEvent(AbsListView.java:3846)
09-26 12:43:21.949: E/AndroidRuntime(14618): at android.view.View.dispatchTouchEvent(View.java:5629)
09-26 12:43:21.949: E/AndroidRuntime(14618): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1964)
09-26 12:43:21.949: E/AndroidRuntime(14618): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1725)
09-26 12:43:21.949: E/AndroidRuntime(14618): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1970)
09-26 12:43:21.949: E/AndroidRuntime(14618): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1739)
09-26 12:43:21.949: E/AndroidRuntime(14618): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1970)
09-26 12:43:21.949: E/AndroidRuntime(14618): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1739)
09-26 12:43:21.949: E/AndroidRuntime(14618): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1970)
09-26 12:43:21.949: E/AndroidRuntime(14618): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1739)
09-26 12:43:21.949: E/AndroidRuntime(14618): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1970)
09-26 12:43:21.949: E/AndroidRuntime(14618): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1739)
09-26 12:43:21.949: E/AndroidRuntime(14618): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1970)
09-26 12:43:21.949: E/AndroidRuntime(14618): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1739)
09-26 12:43:21.949: E/AndroidRuntime(14618): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1970)
09-26 12:43:21.949: E/AndroidRuntime(14618): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1739)
09-26 12:43:21.949: E/AndroidRuntime(14618): at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1970)
09-26 12:43:21.949: E/AndroidRuntime(14618): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1739)
09-26 12:43:21.949: E/AndroidRuntime(14618): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2062)
09-26 12:43:21.949: E/AndroidRuntime(14618): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1400)
09-26 12:43:21.949: E/AndroidRuntime(14618): at android.app.Activity.dispatchTouchEvent(Activity.java:2369)
09-26 12:43:21.949: E/AndroidRuntime(14618): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2010)
09-26 12:43:21.949: E/AndroidRuntime(14618): at android.view.View.dispatchPointerEvent(View.java:5809)
09-26 12:43:21.949: E/AndroidRuntime(14618): at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3130)
09-26 12:43:21.949: E/AndroidRuntime(14618): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2658)
09-26 12:43:21.949: E/AndroidRuntime(14618): at android.view.ViewRootImpl.processInputEvents(ViewRootImpl.java:1015)
09-26 12:43:21.949: E/AndroidRuntime(14618): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2667)
09-26 12:43:21.949: E/AndroidRuntime(14618): at android.os.Handler.dispatchMessage(Handler.java:99)
09-26 12:43:21.949: E/AndroidRuntime(14618): at android.os.Looper.loop(Looper.java:137)
09-26 12:43:21.949: E/AndroidRuntime(14618): at android.app.ActivityThread.main(ActivityThread.java:4517)
09-26 12:43:21.949: E/AndroidRuntime(14618): at java.lang.reflect.Method.invokeNative(Native Method)
09-26 12:43:21.949: E/AndroidRuntime(14618): at java.lang.reflect.Method.invoke(Method.java:511)
09-26 12:43:21.949: E/AndroidRuntime(14618): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
09-26 12:43:21.949: E/AndroidRuntime(14618): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
09-26 12:43:21.949: E/AndroidRuntime(14618): at dalvik.system.NativeStart.main(Native Method)
这是我的代码:
public class DeleteRewardsFragment extends Fragment{
private Context context;
private FragmentsActivity activity;
private ListView listView;
private View myFragmentView;
private DeleteRewardsAdapter adapter;
private String[] values;
@SuppressLint("ResourceAsColor")
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
myFragmentView = inflater.inflate(R.layout.listview, container, false);
listView = (ListView) myFragmentView.findViewById(android.R.id.list);
activity = (FragmentsActivity) getActivity();
context = activity.getApplicationContext();
values = new String[4];
adapter = new DeleteRewardsAdapter(context, values);
listView.setAdapter(adapter);
listView.setOnItemClickListener(deleteEvent);
return myFragmentView;
}
private OnItemClickListener deleteEvent = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
alertDialog .setTitle("Jetzt löschen?");
alertDialog .setMessage("Die in deisem Store gesammelten stampss gehen debei verloren!")
.setCancelable(false)
.setPositiveButton("Abbrechen", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
values = new String[values.length-1];
adapter = new DeleteRewardsAdapter(context, values);
listView.setAdapter(adapter);
}
})
.setNegativeButton("Löschen", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog dialog = alertDialog.create();
dialog.show();
}
};
}
有人可以帮助我吗?
答案 0 :(得分:49)
要将AlertDialog
添加到Activity
或Fragment
,您必须使用Activity
的实例,而不是您在代码中使用的应用程序。像这样创建AlertDialog
:
AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());
这应该为你做的伎俩! :)
答案 1 :(得分:10)
就我而言,我试图像这样创建对话框:
new Dialog(getApplicationContext());
所以我不得不改变:
new Dialog(MyActivity.this);
它开始工作了。
答案 2 :(得分:5)
以下是您可以从问题中解决问题的方法。
您应该使用
context
ActivityName.this
如果你使用的是getApplicationContext(),那么在使用getApplicationContext()之前使用下面的标志
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT)
将以下权限添加到您的清单:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
答案 3 :(得分:3)
发送活动,而不是上下文。
AlertDialog.Builder alertDialog = new AlertDialog.Builder(activity);
答案 4 :(得分:3)
我改变了:
ProgressDialog(getApplicationContext());
为:
ProgressDialog(MainActivity.this);
,
答案 5 :(得分:1)
我改变了getApplicationContext()
到
activity.this
它会起作用
答案 6 :(得分:0)
从getApplicationContext()
或getApplication()
更改为ActivityName.this
答案 7 :(得分:0)
由于上下文不适当:
需要确保您提供的上下文是活动上下文。
AlertDialog.Builder alertDialog = new AlertDialog.Builder
( 在此处提供活动上下文 );
答案 8 :(得分:0)
长时间奋斗!
如果您在Android SDK 26+中有此问题,则是由于以下错误设置引起的:
mWindowParams.type = LayoutParams.TYPE_TOAST;
TYPE_TOAST: 在API级别26中不赞成使用此常数。 非系统应用。请改用TYPE_APPLICATION_OVERLAY。
答案 9 :(得分:0)
在清单上添加权限
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>