3

When i run my code i get this error:

  : E/InputEventReceiver(1363): Exception dispatching input event.

  : E/MessageQueue-JNI(1363): Exception in MessageQueue callback: handleReceiveCallback

  : D/dalvikvm(1363): GC_CONCURRENT freed 1898K, 30% free 4921K/6992K, paused 78ms+107ms, total 413ms

  : E/MessageQueue-JNI(1363):  android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for  an application

  : E/MessageQueue-JNI(1363):at  com.example.ikmantest2.MainActivity$6.onItemClick(MainActivity.java:269)

And this is the code that i get error:

gallery.setOnItemClickListener(new OnItemClickListener(){

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            final Dialog dialog = new Dialog(getBaseContext());
            dialog.setContentView(R.layout.image_dialog_layout);


            // set the custom dialog components - text, image and button
            imageView=(ImageView)dialog.findViewById(R.id.bigger_image);
            imageView.setImageBitmap(exListAdapter.getImageByPosition(lastClickedGroup, arg2));
            pre=(ImageButton)dialog.findViewById(R.id.btn_pre);
            back=(ImageButton)dialog.findViewById(R.id.btn_back);
            next=(ImageButton)dialog.findViewById(R.id.btn_next); 

            // if button is clicked, close the custom dialog
            pre.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
            back.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
            next.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });

            /*this is line number 269 as in error code*/  dialog.show();
        }

    }); 

So how can i solve this error?

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
user2722364
  • 65
  • 1
  • 8

5 Answers5

9

do this way

final Dialog dialog = new Dialog(MainActivity.this);
Biraj Zalavadia
  • 28,348
  • 10
  • 61
  • 77
  • Direct hit------->>It's working.Can you tell me what happening there.Why getBaseContext() not work? – user2722364 Sep 09 '13 at 10:41
  • you can not use baseContext() because it refers to other context and UI need a current context so you were getting getBaseContext() null – Biraj Zalavadia Sep 09 '13 at 10:46
  • for more info visit this http://stackoverflow.com/questions/10641144/difference-between-getcontext-getapplicationcontext-getbasecontext-g – Biraj Zalavadia Sep 09 '13 at 10:48
  • Worth noting that this can still happen with an activity context. –  May 31 '14 at 15:13
2

change final

Dialog dialog = new Dialog(getBaseContext());

to

final Dialog dialog = new Dialog(YourActivity.this);
Piyush
  • 18,895
  • 5
  • 32
  • 63
0

This error occur when wrong context is passed, simply change the Dailog context to activity context.

Use:

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

Instead:

final Dialog dialog = new Dialog(getBaseContext());

RobinHood
  • 10,897
  • 4
  • 48
  • 97
0

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

or

final Dialog dialog = new Dialog(getApplicationContext());

Brijesh Patel
  • 676
  • 1
  • 5
  • 13
0

A Dialog can be showed by an Activity and not (directly) by the Application (and principally not by a Service), therefore the Dialog's constructor expects the Context of the Activity and not the Context of the Application.

If you know the name of your activity, then

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

of course works.

But if you don't want to burn the name of your Activity in your code, use this snippets:

public class MainActivity extends Activity {
Context context; // for context of the application
Context acontext; // for context of the activity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    context = getApplicationContext(); // Don't use for Dialog
    acontext = this; // Use for Dialog
    setContentView(R.layout.activity_main);

    // Dialog block started
    final Dialog dialog = new Dialog(acontext);
    dialog.setContentView(R.layout.custom);
    dialog.setTitle("Title");
    Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
    // if button is clicked, close the custom dialog
    dialogButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });     
    dialog.show();
    // Dialog block ended
}}
Skalár Wag
  • 2,247
  • 2
  • 18
  • 21