1

我正在使用自定义对话框,我想单击按钮,单击按钮时没有任何反应。请看一看。

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;
        }
    }
}

这是我在 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

这是调用自定义对话框的按钮事件。

  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();

}

4

2 回答 2

3
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);
    }

}

您的 custom_dialog 必须实现 OnClickListener 并且您必须将“this”设置为按钮的点击侦听器

于 2013-06-01T18:00:54.050 回答
1

您需要在方法中onClickListener设置onCreate按钮

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

View.onClickListener在课堂上实施

于 2013-06-01T18:01:05.703 回答