Asked  7 Months ago    Answers:  5   Viewed   169 times

I'm trying to open a dialog window, but every time I try to open it it throws this exception:

Uncaught handler: thread main exiting due to uncaught exception
     Unable to add window -- token null is not for an application
  at android.view.ViewRoot.setView(
  at android.view.WindowManagerImpl.addView(
  at android.view.WindowManagerImpl.addView(

I'm creating it by calling showDialog with the display's id. The onCreateDialog handler logs fine and I can step through it without an issue, but I've attached it since it seems like I'm missing something:

public Dialog onCreateDialog(int id)
    Dialog dialog;
    Context appContext = this.getApplicationContext();
        case RENAME_DIALOG_ID:
            Log.i("Edit", "Creating rename dialog...");
            dialog = new Dialog(appContext);
            dialog.setTitle("Rename " + noteName);
            dialog = null;
    return dialog;      

Is there something missing from this? Some questions have talked about having this problem when creating a dialog from onCreate, which happens because the activity isn't created yet, but this is coming from a call from a menu object, and the appContext variable seems like it is correctly populated in the debugger.



Instead of : Context appContext = this.getApplicationContext(); you should use a pointer to the activity you're in (probably this).

I got bitten by this today too, the annoying part is the getApplicationContext() is verbatim from :(

Tuesday, June 1, 2021
answered 7 Months ago

you are showing your popup too early. You may post a delayed runnable for showatlocation in Onresume , Give it a try

Edit: This post seems to have the same problem answered Problems creating a Popup Window in Android Activity

Thursday, June 3, 2021
answered 7 Months ago

I had the same problem as you, it looks like you used the tutorial from like I did. The problem is that you cannot reliably pass the current activity to the popup window, because you have no control over the current activity. It looks like there might be a unreliable way to get the current activity, but I don't recommend that.

The way I fixed it for my app was to not use the popup window, but instead make my own through the window manager.

private void showCustomPopupMenu()
                windowManager2 = (WindowManager)getSystemService(WINDOW_SERVICE);
                LayoutInflater layoutInflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View view=layoutInflater.inflate(R.layout.xxact_copy_popupmenu, null);
                params=new WindowManager.LayoutParams(

                windowManager2.addView(view, params);  

If you want this to look like a popup window, just add a transparent gray view as the background and add a onClickListener to it to remove the view from the windowManager object.

I know this isn't as convenient as a popup, but from my experience, it is the most reliable way.

and don't forget to add permission in your manifest file

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
Thursday, June 24, 2021
answered 6 Months ago

Found the solution. Since i'm using the front camera, I need the get the camcorder profile from the front camera. I achieved thas by doing this:

mrec.setProfile(CamcorderProfile.get(1, CamcorderProfile.QUALITY_HIGH));

Instead of this:

Saturday, August 14, 2021
answered 4 Months ago

For some reason, your template file is broken during installation.

Let me attach my file as below. To apply, please do as follows.

  1. Close Android Studio.
  2. Open your recipe.xml.ftl file at C:Program FilesAndroidAndroid Studiopluginsandroidlibtemplatesgradle-projectsNewAndroidModule directory with text editor (ie, Notepad++)
  3. Copy-and-Paste below code, then Save. (UTF-8 format)
  4. Rerun Android Studio.

Note that the file is from Android Studio version 2.2.1

<?xml version="1.0"?>

<#if backwardsCompatibility!true>
    <dependency mavenUrl="${buildApi}.+"/>

<#if unitTestsSupported>
    <dependency mavenUrl="junit:junit:4.12" gradleConfiguration="testCompile" />

<#if !createActivity>
    <mkdir at="${escapeXmlAttribute(srcOut)}" />

    <mkdir at="${escapeXmlAttribute(projectOut)}/libs" />

    <merge from="root/settings.gradle.ftl"
             to="${escapeXmlAttribute(topOut)}/settings.gradle" />
    <instantiate from="root/build.gradle.ftl"
                   to="${escapeXmlAttribute(projectOut)}/build.gradle" />
    <instantiate from="root/AndroidManifest.xml.ftl"
                   to="${escapeXmlAttribute(manifestOut)}/AndroidManifest.xml" />

<mkdir at="${escapeXmlAttribute(resOut)}/drawable" />
<#if copyIcons && !isLibraryProject>
    <copy from="root/res/mipmap-hdpi"
            to="${escapeXmlAttribute(resOut)}/mipmap-hdpi" />
    <copy from="root/res/mipmap-mdpi"
            to="${escapeXmlAttribute(resOut)}/mipmap-mdpi" />
    <copy from="root/res/mipmap-xhdpi"
            to="${escapeXmlAttribute(resOut)}/mipmap-xhdpi" />
    <copy from="root/res/mipmap-xxhdpi"
            to="${escapeXmlAttribute(resOut)}/mipmap-xxhdpi" />
    <copy from="root/res/mipmap-xxxhdpi"
            to="${escapeXmlAttribute(resOut)}/mipmap-xxxhdpi" />
<#if makeIgnore>
    <copy from="root/module_ignore"
            to="${escapeXmlAttribute(projectOut)}/.gitignore" />
<#if enableProGuard>
    <instantiate from="root/proguard-rules.txt.ftl"
                   to="${escapeXmlAttribute(projectOut)}/" />
<#if !(isLibraryProject??) || !isLibraryProject>
    <instantiate from="root/res/values/styles.xml.ftl"
                   to="${escapeXmlAttribute(resOut)}/values/styles.xml" />
<#if buildApi gte 22>
    <copy from="root/res/values/colors.xml"
          to="${escapeXmlAttribute(resOut)}/values/colors.xml" />

    <instantiate from="root/res/values/strings.xml.ftl"
                   to="${escapeXmlAttribute(resOut)}/values/strings.xml" />

    <instantiate from="root/test/app_package/"
                   to="${escapeXmlAttribute(testOut)}/" />

<#if unitTestsSupported>
    <instantiate from="root/test/app_package/"
                   to="${escapeXmlAttribute(unitTestOut)}/" />
<#if includeCppSupport!false>
    <instantiate from="root/CMakeLists.txt.ftl"
                   to="${escapeXmlAttribute(projectOut)}/CMakeLists.txt" />

    <mkdir at="${nativeSrcOut}" />
    <instantiate from="root/native-lib.cpp.ftl" to="${nativeSrcOut}/native-lib.cpp" />

Saturday, November 27, 2021
Lone Learner
answered 7 Days ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :