我正在使用自定义对话框,我希望点击该按钮,单击该按钮时不会发生任何事情。请看一下。
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();
}
答案 0 :(得分: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,你必须设置"这个"作为按钮的点击监听器
答案 1 :(得分:1)
您需要在onClickListener
方法
onCreate
Button myButton = findViewById(R.id.blue0);
myButton.setOnClickListener(this);
并在类
上实现View.onClickListener