0

I'm testing code given in answer to my question:

remove line between custom option menu items

answer related to this pic:

enter image description here

I'm trying to fire dialog when press feedback by adding this code to WidgetMenu class but it gave me force close, the code I add :

  public void onClick(View arg0) {       
        final Dialog dialog = new Dialog(context);
        dialog.setContentView(R.layout.custom_dialog);
        dialog.setTitle("Title...");

        // set the custom dialog components - text, image and button
        TextView text = (TextView) dialog.findViewById
                           (R.id.dialog_text);
        text.setText("Places contact me");

        ImageView image = (ImageView) dialog.findViewById(R.id.image);
        image.setImageResource(R.drawable.ic_launcher);

    Button dialogButton = (Button) dialog.findViewById(R.id.dialog_Button);
        // if button is clicked, close the custom dialog
        dialogButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

        dialog.show();
      }
    });

my logcat :

  W/KeyCharacterMap(303): No keyboard for id 0
  W/KeyCharacterMap(303): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
  D/dalvikvm(303): GC_EXTERNAL_ALLOC freed 932 objects / 65328 bytes in 90ms
  D/AndroidRuntime(303): Shutting down VM
  W/dalvikvm(303): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
  E/AndroidRuntime(303): FATAL EXCEPTION: main
  E/AndroidRuntime(303): java.lang.NullPointerException
  E/AndroidRuntime(303): at android.app.Dialog.<init> (Dialog.java:141)
  E/AndroidRuntime(303): at android.app.Dialog.<init> (Dialog.java:123)
  E/AndroidRuntime(303): at com.tsn.dr.WidgetMenu$Ui$2.onClick (WidgetMenu.java:99)
  E/AndroidRuntime(303): at android.view.View.performClick (View.java:2408)
  E/AndroidRuntime(303): at android.view.View$PerformClick.run (View.java:8816)
  E/AndroidRuntime(303): at android.os.Handler.handleCallback (Handler.java:587)
  E/AndroidRuntime(303): at android.os.Handler.dispatchMessage (Handler.java:92)
  E/AndroidRuntime(303): at android.os.Looper.loop(Looper.java:123)
  E/AndroidRuntime(303): at android.app.ActivityThread.main (ActivityThread.java:4627)
  E/AndroidRuntime(303): at java.lang.reflect.Method.invokeNative (Native Method)
  E/AndroidRuntime(303): at java.lang.reflect.Method.invoke (Method.java:521)
  E/AndroidRuntime(303): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
  E/AndroidRuntime(303): at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:626)
  E/AndroidRuntime(303): at dalvik.system.NativeStart.main(Native Method)

Any advice will be appreciated,thanks.

Community
  • 1
  • 1
Android Stack
  • 4,314
  • 6
  • 31
  • 49

2 Answers2

1

May try this :

final Dialog dialog = new Dialog(YourActivity.this);
Alexis C.
  • 91,686
  • 21
  • 171
  • 177
0

The Dialog constructor fails with a NullPointerException. The only reason I could think of is that the context parameter you're passing in is indeed null.

How do you initialize context?

digitalbreed
  • 3,953
  • 4
  • 26
  • 24
  • i changed to this : final Dialog dialog = new Dialog(getContext()); – Android Stack Aug 11 '12 at 01:32
  • Try simply passing "this". If it still fails, please check if the stacktrace is still the same or something else fails now? – digitalbreed Aug 11 '12 at 01:34
  • i used this :ffinal Context context = this; – Android Stack Aug 11 '12 at 01:44
  • If there's a red mark then there should be an error message associated with it... try to copy and paste the error message. – digitalbreed Aug 11 '12 at 01:46
  • The context must be of type android.content.Context - see Dialog documentation at http://developer.android.com/reference/android/app/Dialog.html#pubctors --- if you're using this code in an Activity, you should be able to simply write final Dialog dialog = new Dialog(this); --- otherwise please update your posting with more code detail. – digitalbreed Aug 11 '12 at 01:52