PeakGen
PeakGen

Reputation: 23045

Unable to set a View to the Dialog

Please have a look at the following code

The code used to create the Custom Dialog. This class is an action class for an AlertDialog. When the user clicks on "Yes" button on that AlertDialog, this class get fired. private class PositiveDialogBtnAction implements DialogInterface.OnClickListener {

        @Override
        public void onClick(DialogInterface arg0, int arg1) {
            // TODO Auto-generated method stub
            //Toast.makeText(getApplicationContext(), databaseConnector.getStreetAddress(selectedBranch), Toast.LENGTH_LONG).show();

            Dialog dialog = new Dialog(getApplicationContext());
            dialog.setContentView(R.layout.activity_call_dialog);
            dialog.setTitle("Select a Phone Number");

            dialog.show();
        }

    }

activity_call_dialog.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".CallDialog" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="@string/hello_world" />

</RelativeLayout>

CallDialog.java

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class CallDialog extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_call_dialog);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_call_dialog, menu);
        return true;
    }

}

When I try to run the Custom Dialog, I get the following error

02-15 11:04:02.170: E/AndroidRuntime(585): FATAL EXCEPTION: main
02-15 11:04:02.170: E/AndroidRuntime(585): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
02-15 11:04:02.170: E/AndroidRuntime(585):  at android.view.ViewRoot.setView(ViewRoot.java:531)
02-15 11:04:02.170: E/AndroidRuntime(585):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
02-15 11:04:02.170: E/AndroidRuntime(585):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
02-15 11:04:02.170: E/AndroidRuntime(585):  at android.app.Dialog.show(Dialog.java:241)
02-15 11:04:02.170: E/AndroidRuntime(585):  at com.example.esoftcallmanager.Form$PositiveDialogBtnAction.onClick(Form.java:155)
02-15 11:04:02.170: E/AndroidRuntime(585):  at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:159)
02-15 11:04:02.170: E/AndroidRuntime(585):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-15 11:04:02.170: E/AndroidRuntime(585):  at android.os.Looper.loop(Looper.java:123)
02-15 11:04:02.170: E/AndroidRuntime(585):  at android.app.ActivityThread.main(ActivityThread.java:3683)
02-15 11:04:02.170: E/AndroidRuntime(585):  at java.lang.reflect.Method.invokeNative(Native Method)
02-15 11:04:02.170: E/AndroidRuntime(585):  at java.lang.reflect.Method.invoke(Method.java:507)
02-15 11:04:02.170: E/AndroidRuntime(585):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-15 11:04:02.170: E/AndroidRuntime(585):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-15 11:04:02.170: E/AndroidRuntime(585):  at dalvik.system.NativeStart.main(Native Method)
02-15 11:04:05.520: I/Process(585): Sending signal. PID: 585 SIG: 9

Why is this? Please help!

Upvotes: 0

Views: 73

Answers (1)

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132992

use

Dialog dialog = new Dialog(CallDialog.this);

instead of

Dialog dialog = new Dialog(getApplicationContext());

because for showing Dialog you will need to pass Activity Context instead of Application

Upvotes: 2

Related Questions