1

I am trying to open a custom dialog box that cover the full interface of the device .But I don't know it is throwing exception Please check my code and help me to solve this issue.

On click of the listview item

data.setOnItemClickListener(new OnItemClickListener() {
              @Override
              public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
                passengerInformationPopup();

              }
    });

**The Function to open the Dialog**



public void passengerInformationPopup() {
    final Dialog dialog= new Dialog(getBaseContext());
    dialog.setContentView(R.layout.passenger_details_dialog); 
    String[] tittlearray ={"Mr.","Mrs.","Ms"};
    Spinner tittleSpinner = (Spinner) dialog.findViewById(R.id.Tittle);
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, tittlearray); 

    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
    tittleSpinner.setAdapter(dataAdapter);
    dialog.show();
}

Exception

08-14 16:39:50.338: E/AndroidRuntime(30294): FATAL EXCEPTION: main
08-14 16:39:50.338: E/AndroidRuntime(30294): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
08-14 16:39:50.338: E/AndroidRuntime(30294):    at android.view.ViewRootImpl.setView(ViewRootImpl.java:656)
08-14 16:39:50.338: E/AndroidRuntime(30294):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:328)
08-14 16:39:50.338: E/AndroidRuntime(30294):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:226)
08-14 16:39:50.338: E/AndroidRuntime(30294):    at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:151)
08-14 16:39:50.338: E/AndroidRuntime(30294):    at android.app.Dialog.show(Dialog.java:277)
08-14 16:39:50.338: E/AndroidRuntime(30294):    at com.example.userdetails.MainActivity.passengerInformationPopup(MainActivity.java:181)
08-14 16:39:50.338: E/AndroidRuntime(30294):    at com.example.userdetails.MainActivity$5.onItemClick(MainActivity.java:149)
08-14 16:39:50.338: E/AndroidRuntime(30294):    at android.widget.AdapterView.performItemClick(AdapterView.java:298)
08-14 16:39:50.338: E/AndroidRuntime(30294):    at android.widget.AbsListView.performItemClick(AbsListView.java:1114)
08-14 16:39:50.338: E/AndroidRuntime(30294):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2937)
08-14 16:39:50.338: E/AndroidRuntime(30294):    at android.widget.AbsListView$1.run(AbsListView.java:3695)
08-14 16:39:50.338: E/AndroidRuntime(30294):    at android.os.Handler.handleCallback(Handler.java:615)
08-14 16:39:50.338: E/AndroidRuntime(30294):    at android.os.Handler.dispatchMessage(Handler.java:92)
08-14 16:39:50.338: E/AndroidRuntime(30294):    at android.os.Looper.loop(Looper.java:153)
08-14 16:39:50.338: E/AndroidRuntime(30294):    at android.app.ActivityThread.main(ActivityThread.java:4987)
08-14 16:39:50.338: E/AndroidRuntime(30294):    at java.lang.reflect.Method.invokeNative(Native Method)
08-14 16:39:50.338: E/AndroidRuntime(30294):    at java.lang.reflect.Method.invoke(Method.java:511)
08-14 16:39:50.338: E/AndroidRuntime(30294):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
08-14 16:39:50.338: E/AndroidRuntime(30294):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
08-14 16:39:50.338: E/AndroidRuntime(30294):    at dalvik.system.NativeStart.main(Native Method)
Developer
  • 6,292
  • 19
  • 55
  • 115

3 Answers3

1

Use

final Dialog dialog= new Dialog(getApplicationcontext()); OR  final Dialog dialog= new Dialog(YOUR_ACTIVITY.this);

instead of

final Dialog dialog= new Dialog(getBaseContext());
Hardik Joshi
  • 9,477
  • 12
  • 61
  • 113
1

Use Activity Context

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

To know when to use activity context

When to call activity context OR application context?

From the comments

How to set dialog to show with full screen?

Edit:

From Dianne Hackborn suggestion

Give its constructor a non-dialog theme, such as android.R.style.Theme or android.R.style.Theme_Light.

Look @ the themes

https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/res/res/values/themes.xml.

More info @

https://groups.google.com/forum/#!topic/android-developers/NDFo9pF8sHY

   Dialog dialog=new Dialog(ActivityName.this,android.R.style.Theme);
Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • i have done this final Dialog dialog= new Dialog(this,android.R.style.Theme_Dark_NoTitleBar_FullScreen);but is is showing the error that Theme_Dark_NoTitleBar_FullScreen cannot be resolved or is not a field – Developer Aug 14 '13 at 11:25
  • 1
    https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/res/res/values/themes.xml. – Raghunandan Aug 14 '13 at 11:26
  • i am not getting please help me on this i am trying to add the theme in my style but it is shwoing the error – Developer Aug 14 '13 at 11:31
  • @Gaurav https://groups.google.com/forum/#!topic/android-developers/NDFo9pF8sHY. define styles in styles.xml and set the same to your dialog. use the ones in the link i provided above. – Raghunandan Aug 14 '13 at 11:37
0

try this

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

instead of

final Dialog dialog= new Dialog(getBaseContext());
Ketan Ahir
  • 6,678
  • 1
  • 23
  • 45