1

Just a simple things, the app should shows dialog box when button is clicked. But when it is clicked, it crashed. And I don't understand what are the logCat error trying to said.

 addImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                final Dialog dialog = new Dialog(getApplication());
                dialog.setContentView(R.layout.custom_dialog_box);
                dialog.setTitle("Alert Dialog View");
                Button btnExit = (Button) dialog.findViewById(R.id.btnExit);
                btnExit.setOnClickListener(new View.OnClickListener() {
                    @Override public void onClick(View v) {
                        dialog.dismiss();
                    }
                });
                dialog.findViewById(R.id.btnChoosePath)
                        .setOnClickListener(new View.OnClickListener() {
                            @Override public void onClick(View v) {
                               // activeGallery();
                            }
                        });
                dialog.findViewById(R.id.btnTakePhoto)
                        .setOnClickListener(new View.OnClickListener() {
                            @Override public void onClick(View v) {
                                //activeTakePhoto();
                            }
                        });

                // show dialog on screen
                dialog.show();
            }

            });

xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:background="@color/light_gray"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:layout_height="fill_parent">

    <Button
        android:onClick="btnChoosePathClicked"
        android:id="@+id/btnChoosePath"
        android:background="@color/honey_dew2"
        android:textColor="@color/black"
        android:layout_centerHorizontal="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:width="200dp"
        android:text="From Gallery"/>

    <Button
        android:onClick="btnTakePhotoClicked"
        android:id="@+id/btnTakePhoto"
        android:background="@color/honey_dew2"
        android:textColor="@color/black"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/btnChoosePath"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:width="200dp"
        android:text="Take Photo"/>

    <Button
        android:onClick="btnExitClicked"
        android:id="@+id/btnExit"
        android:background="@color/honey_dew2"
        android:textColor="@color/black"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/btnTakePhoto"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:width="200dp"
        android:text="Exit"/>
</RelativeLayout>

LogCat error

12-09 17:57:34.344  18804-18804/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
            at android.view.ViewRootImpl.setView(ViewRootImpl.java:698)
            at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:248)
            at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
            at android.app.Dialog.show(Dialog.java:281)
            at com.example.project.myapplication.GUI.AddMoreClaims$1.onClick(AddMoreClaims.java:68)
            at android.view.View.performClick(View.java:4230)
            at android.view.View$PerformClick.run(View.java:17660)
            at android.os.Handler.handleCallback(Handler.java:800)
            at android.os.Handler.dispatchMessage(Handler.java:100)

where (AddMoreClaims.java:68) refer to dialog.show();. What's wrong here ?

Pankaj
  • 7,908
  • 6
  • 42
  • 65
John
  • 684
  • 11
  • 35

5 Answers5

2

Make your dialog declaration like below code:

For Activity:

final Dialog dialog = new Dialog(Your_Activity_Name.this);

For Fragment:

final Dialog dialog = new Dialog(getActivity());
Pankaj
  • 7,908
  • 6
  • 42
  • 65
  • Thanks !! I just follow the tutorial without realizing it is `fragment` not `activity` lol – John Dec 09 '15 at 10:09
1

Unable to add window -- token null is not for an application

Because getApplication() retuning null.

Use v.getContext() or ActivityName.this to create Dialog object:

final Dialog dialog = new Dialog(v.getContext());
OR
final Dialog dialog = new Dialog(ActivityName.this);
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
1

You can't create Dialog with an application Context, you should use Activity Context.

new Dialog(MyActivity.this); //if you use Activity
new Dialog(getActivity()); //if you use Fragment 
bongo
  • 733
  • 4
  • 12
1

You should show your dialog inside activity and never in application. Open your dialog in your current open activity and it should work for you.

SacreDeveloper
  • 1,253
  • 1
  • 9
  • 16
1

As others have stated above your Context i.e. getApplication() is wrong.

This is because you need to have the current Activity's context to display anything (popup, view, dialog) over it.

Whereas you could have a getApplicationContext() to start a new Activity, you will have to use this (inside an Activity) or getActivity() inside a Fragment.

Here's more info on Context and which one to use and when

Zain
  • 2,336
  • 1
  • 16
  • 25
  • can you explain [this](http://stackoverflow.com/questions/34125541/why-the-selected-item-in-spinner-doesnt-display-on-edittext) ? – Tony Dec 09 '15 at 10:17