0

This is the code i have used for the menu item selection:

public boolean onOptionsItemSelected(MenuItem item) {
      switch (item.getItemId()) {
      case R.id.menu_settings:
            showDialog(1);
            return true;

      default:
            return super.onOptionsItemSelected(item);
      }
 }

There is only one option in the menu, and i have designed a customDialog using the following code:

protected Dialog onCreateDialog(int id) {

        Context mContext = getApplicationContext();
        Dialog dialog = new Dialog(mContext);

        dialog.setContentView(R.layout.custom_dialog);
        dialog.setTitle("About Us");

        TextView text = (TextView) dialog.findViewById(R.id.text);
        text.setText("App Created By: Prateek Garg (garg.prateek1@gmail.com)");
        ImageView image = (ImageView) dialog.findViewById(R.id.image);
        image.setImageResource(R.drawable.logo_resumes);
        return dialog;
        }
}

The problem is that whenever i press the menu item "About Us", the app force-closes itself. I am unable to rectify whatever error there is, but i am hoping you guys can.

Thanks in advance. PS. I have created the menuInflator() in onCreateOptionsMenu() .

Cheers

LOGCAT shows the following errors: [I donot know how to understand or remove them... :( ]

07-27 23:35:04.569: W/dalvikvm(648): threadid=1: thread exiting with uncaught exception (group=0x40a13300)

07-27 23:35:04.659: E/AndroidRuntime(648): FATAL EXCEPTION: main

07-27 23:35:04.659: E/AndroidRuntime(648): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

07-27 23:35:04.659: E/AndroidRuntime(648): at android.view.ViewRootImpl.setView(ViewRootImpl.java:589)

07-27 23:35:04.659: E/AndroidRuntime(648): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:326) and a lott more .

If you guys say so, i'll include the rest errors too, but i think they are just forming because of the first 1 or 2 as is the case generally

Jean-Rémy Revy
  • 5,607
  • 3
  • 39
  • 65
  • 4
    What is the error shows in the Logcat? – Aerrow Jul 27 '12 at 17:44
  • 1
    Again please post the error you see in LogCat – Code Droid Jul 27 '12 at 17:53
  • If you need help finding LogCat let us know. The error will appear in red. Look for your package name in the error message and post the whole thing. – Code Droid Jul 27 '12 at 17:54
  • Check if r.id.text and R.id.image actually exist with that name in custom_dialog. If they are in some other layout this will cause error. Also as I showed below check that they have height and width specified. W/O height or width it will crash. If not in that particular layout the elements will be null and crash. – Code Droid Jul 27 '12 at 17:57
  • R.id.image and text, plus R.layout.cutom_dialog they are correct, and exist... there is some other problem and i still can;t solve it. – Prateek Garg Jul 27 '12 at 18:58

1 Answers1

1

A force close is most likely a null pointer exception. There are actually several ways this could happen but most likely one of the UI elements not available. This could be because its not in the layout you have specified for the view/dialog or because you failed to call setContentView(R.layout.layoutName) for the View. One other possibility is an item like TextView that does not specify height and width values. On most UI elements this is required and will cause a runtime error. In order to see the logcat goto window->show view -->logcat

A runtime error will appear in red. It will usually contain the words fatal if it closes the app. So goto that point in the logcat. Look for the first line where you see something like com.mypackage.test.MyActivity or MyDialog. This line will be in red. Make sure it is one with your package name on it since the error will point to a lot of system stuff too. Click on that line, and it will take you to the point in your code where the crash occured. Also post the error. You should always post the error message on StackOverflow.

Code Droid
  • 10,344
  • 17
  • 72
  • 112
  • Log cat shows these errors, but i do not understand any of this : – Prateek Garg Jul 27 '12 at 18:10
  • See: http://stackoverflow.com/questions/2634991/android-1-6-android-view-windowmanagerbadtokenexception-unable-to-add-window – Code Droid Jul 27 '12 at 22:08
  • You sometimes need to google the main error in your case : android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application – Code Droid Jul 27 '12 at 22:08
  • It appears that it does not like the context. – Code Droid Jul 27 '12 at 22:09
  • i did the whole thing by changing intent to a new activity whole together... i still donot understand why this did not work, but my app is working fine now... Thank you for the attention and help sir... – Prateek Garg Jul 28 '12 at 12:34