Hy. I tried to create an alert dialog but when I run my application, is throw an exception
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)
This is my code:
这是我的代码:
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();
}
};
}
Can someone to help me?
有人可以帮帮我吗?
6 个解决方案
#1
44
To add AlertDialog
to your Activity
or Fragment
you have to use your Activity
's instance, not your application's which you are doing in your code. Create your AlertDialog
like this :
要将AlertDialog添加到Activity或Fragment,您必须使用Activity的实例,而不是您在代码中使用的应用程序。像这样创建AlertDialog:
AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());
And that should do the trick for you! : )
这应该为你做的伎俩! :)
#2
8
In my case I was trying to create my dialog like this:
在我的情况下,我试图像这样创建我的对话框:
new Dialog(getApplicationContext());
So I had to change for:
所以我不得不改变:
new Dialog(MyActivity.this);
And it started working.
它开始工作了。
#3
5
Below are the things you can do to get it over from issue.
以下是您可以从问题中解决的问题。
-
Instead of using
而不是使用
context
ActivityName.this
-
if you are using getApplicationContext(), then use below flag before using getApplicationContext ()
如果你使用的是getApplicationContext(),那么在使用getApplicationContext()之前使用下面的标志
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT)
Add the following permission to your manifest:
将以下权限添加到清单:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
#4
3
I changed:
ProgressDialog(getApplicationContext());
to:
ProgressDialog(MainActivity.this);
,
#5
2
Send in activity, rather than context.
发送活动,而不是上下文。
AlertDialog.Builder alertDialog = new AlertDialog.Builder(activity);
#6
1
I changed getApplicationContext()
to
我将getApplicationContext()更改为
activity.this
it will work
它会工作
#1
44
To add AlertDialog
to your Activity
or Fragment
you have to use your Activity
's instance, not your application's which you are doing in your code. Create your AlertDialog
like this :
要将AlertDialog添加到Activity或Fragment,您必须使用Activity的实例,而不是您在代码中使用的应用程序。像这样创建AlertDialog:
AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());
And that should do the trick for you! : )
这应该为你做的伎俩! :)
#2
8
In my case I was trying to create my dialog like this:
在我的情况下,我试图像这样创建我的对话框:
new Dialog(getApplicationContext());
So I had to change for:
所以我不得不改变:
new Dialog(MyActivity.this);
And it started working.
它开始工作了。
#3
5
Below are the things you can do to get it over from issue.
以下是您可以从问题中解决的问题。
-
Instead of using
而不是使用
context
ActivityName.this
-
if you are using getApplicationContext(), then use below flag before using getApplicationContext ()
如果你使用的是getApplicationContext(),那么在使用getApplicationContext()之前使用下面的标志
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT)
Add the following permission to your manifest:
将以下权限添加到清单:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
#4
3
I changed:
ProgressDialog(getApplicationContext());
to:
ProgressDialog(MainActivity.this);
,
#5
2
Send in activity, rather than context.
发送活动,而不是上下文。
AlertDialog.Builder alertDialog = new AlertDialog.Builder(activity);
#6
1
I changed getApplicationContext()
to
我将getApplicationContext()更改为
activity.this
it will work
它会工作