Reputation: 603
I get the following android exception when I try to open a dialog. when i am press my own SoftKeyboard key how can I fix this problem?
BadTokenException: Unable to add window -- token null is not for an application
com.example.android.softkeyboard.SoftKeyboard.diqalog(SoftKeyboard.java:759)
com.example.android.softkeyboard.SoftKeyboard.onKey(SoftKeyboard.java:526)
android.inputmethodservice.KeyboardView.onModifiedTouchEvent(KeyboardView.java:1252)
Upvotes: 1
Views: 847
Reputation: 1548
First of all, you cannot present a dialog from a remote service, you can only do so from within a running Activity
, that's why you're getting a BadTokenException
. But there are solutions to this problem:
1) Present an Activity
with Theme.Dialog
theme:
<activity
android:name="com.srgtuszy.activity"
android:theme="@android:style/Theme.Dialog"
/>
And start the activity as a new task:
Intent intent = new Intent(context, MyActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
This way, you'll get an activity which will look just like a dialog.
2) Present an empty and transparent Activity
and show an AlertDialog
from within the activity
Declare and start the activity in manifest just as before, but use a transparent theme:
<style name="Theme.Transparent" parent="android:Theme">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
In the activity, override the onCreate()
method and don's call setContentView()
and present the AlertDialog
:
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Context context = this;
AlertDialog.Builder dialog = new AlertDialog.Builder(context);
dialog.setTitle("Hello!");
dialog.show();
}
This is a more hacky approach, but in this way you can show a dialog to the user without dismissing the input method, you can use to to present edit options for instance.
If you just want to notify the user about a certain event, consider using Notifications
, they won't distract the user and pollute the UI.
Upvotes: 1