Question

I am using an custom dialog and i want to get the click of the button, and nothing is happening when the button is clicked. Please have a look at it.

public class custom_dialog extends Dialog {
    Context m_context;
    public custom_dialog (Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        this.m_context = context;
    }

    public custom_dialog (Context context, int theme) {
        super(context,theme);
        // TODO Auto-generated constructor stub
        this.m_context = context;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    public void onClick(View view) {
        switch(view.getId()) {
            case R.id.blue0:
                 Toast.makeText(getApplicationContext(),"hii..",Toast.LENGTH_SHORT).show();
            break;
        }
    }
}

This is the error i get in logcat.

06-01 23:41:14.062    1512-1543/?                              I/InputDispatcher: Delivering touch to current input target
06-01 23:41:14.062    1512-1543/?                              I/InputDispatcher: Delivering touch to current input target
06-01 23:41:14.062    1512-1543/?                              I/InputDispatcher: Delivering touch to current input target
06-01 23:41:14.250    1512-1544/?                              I/InputReader: dispatchTouch::touch event's action is 1
06-01 23:41:14.250    1512-1543/?                              I/InputDispatcher: Delivering touch to current input target
06-01 23:41:14.328    2954-2954/com.aavishkaar.quickies        W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x40018578)
06-01 23:41:14.351    2954-2954/com.aavishkaar.quickies        E/AndroidRuntime: FATAL EXCEPTION: main
        java.lang.IllegalStateException: Could not execute method of the activity
        at android.view.View$1.onClick(View.java:2144)
        at android.view.View.performClick(View.java:2485)
        at android.view.View$PerformClick.run(View.java:9080)
        at android.os.Handler.handleCallback(Handler.java:587)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:130)
        at android.app.ActivityThread.main(ActivityThread.java:3687)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:507)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
        at dalvik.system.NativeStart.main(Native Method)
        Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:507)
        at android.view.View$1.onClick(View.java:2139)
        ... 11 more
        Caused by: java.lang.NullPointerException
        at com.aavishkaar.quickies.MainActivity$custom_dialog.onCreate(MainActivity.java:238)
        at android.app.Dialog.dispatchOnCreate(Dialog.java:307)
        at android.app.Dialog.show(Dialog.java:225)
        at com.aavishkaar.quickies.MainActivity.button1(MainActivity.java:96)
        ... 14 more
06-01 23:41:14.359   1512-10029/?                              E/liblog: failed to call dumpstate
06-01 23:41:14.359    1512-1524/?                              E/: Dumpstate > /data/log/dumpstate_app_error
06-01 23:41:14.359   1512-10029/?                              D/PowerManagerService: acquireWakeLock flags=0x1 tag=ActivityManager-Launch
06-01 23:41:16.531    1512-1536/?                              D/BatteryService: update start
06-01 23:41:16.937    1566-1566/?                              E/StatusBarPolicy: ecio: 19
06-01 23:41:16.937    1566-1566/?                              E/StatusBarPolicy: iconLevel: 4

This is the button event which calls the custom dialog.

  public void button1(View view)
  {
     final custom_dialog dialog = new custom_dialog(this,R.style.Theme_Dialog);
        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
    lp.dimAmount = 0.1f;

    dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);


  // set the custom dialog components - text, image and button
awesomeAdapter = new AwesomePagerAdapter();
awesomePager = (ViewPager)dialog. findViewById(R.id.pager);
 awesomePager.setAdapter(awesomeAdapter);
  dialog.show();

}

Was it helpful?

Solution

public class custom_dialog extends Dialog implements View.OnClickListener {

  @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.yourLayout);
        findViewById(R.id.blue0).setOnClickListener(this);
    }

}

your custom_dialog has to implement OnClickListener and you have to set "this" as click listener for your button

OTHER TIPS

You need to set the buttons onClickListener in the onCreate methody

Button myButton = findViewById(R.id.blue0); 
myButton.setOnClickListener(this);

and implement the View.onClickListener on the class

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top