按钮的OnClick听众打电话习惯警报对话框 [英] Button OnClick Listener wont call Alert Dialog
问题描述
我试图促使一个 AlertDialog
每当一个按钮$ P $然而pssed我只能够成功实现(使用教程)成功 AlertDialog
在第一的三个按钮。
在我又说:
按钮=(按钮)findViewById(R.id.button2);
按钮=(按钮)findViewById(R.id.button3);
所有按钮都工作了,应用程序强制关闭我知道我可以俯瞰简单的东西或者干脆不正确编码这一点。
(任何帮助是极大AP preciated!)
JAVA:
包com.example.linkingmanager;进口android.app.Activity;
进口android.app.AlertDialog;
进口android.content.Context;
进口android.content.DialogInterface;
进口android.content.Intent;
进口android.os.Bundle;
进口android.view.Menu;
进口android.view.MenuInflater;
进口android.view.MenuItem;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;公共类AppActivity延伸活动{公共布尔onCreateOptionsMenu(菜单菜单){
MenuInflater吹气= getMenuInflater();
inflater.inflate(R.menu.main,菜单);
返回true;
} 公共布尔onOptionsItemSelected(菜单项项){
开关(item.getItemId()){
案例R.id.newdevicebtn:
startActivity(新意图(这一点,App2Activity.class));
返回true;
默认:
返回super.onOptionsItemSelected(项目);
}
}最后上下文的背景下=这;
私人Button按钮;
@覆盖
公共无效的onCreate(捆绑savedInstanceState){super.onCreate(savedInstanceState);
的setContentView(R.layout.main);按钮=(按钮)findViewById(R.id.button1);
按钮=(按钮)findViewById(R.id.button2);
按钮=(按钮)findViewById(R.id.button3);//添加按钮监听器
button.setOnClickListener(新OnClickListener(){@覆盖
公共无效的onClick(查看为arg0){ AlertDialog.Builder alertDialogBuilder =新AlertDialog.Builder(
背景); //设置标题
alertDialogBuilder.setTitle(设置菜单); //设置对话框消息
alertDialogBuilder
.setMessage(删除编辑或链接?)
.setCancelable(假)
.setNeutralButton(编辑,新DialogInterface.OnClickListener(){
公共无效的onClick(DialogInterface对话,诠释的id){
//启动新的活动
意图intentApp2Activity =新意图(AppActivity.this,User1Settings.class);
startActivity(intentApp2Activity);}
})
.setPositiveButton(链接,新DialogInterface.OnClickListener(){
公共无效的onClick(DialogInterface对话,诠释的id){
//启动新的活动 意图intentApp2Activity =新意图(AppActivity.this,User1.class);
startActivity(intentApp2Activity); //如果单击此按钮,关闭
// 目前的活动
AppActivity.this.finish();
}
})
.setNegativeButton(删除,新DialogInterface.OnClickListener(){
公共无效的onClick(DialogInterface对话,诠释的id){
//如果单击此按钮,只要关闭
//对话框,什么也不做
dialog.cancel();
}
}); //创建警报对话框
AlertDialog alertDialog = alertDialogBuilder.create(); // 展示下
alertDialog.show();
}
});
}}
XML:
<?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:ID =@ + ID / linearLayout1
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:背景=@绘制/ link_devices
机器人:方向=垂直> < TextView的机器人:机器人:textAppearanceLargetextAppearance =机器人:ID =@ + ID / textView1的android:layout_width =WRAP_CONTENT机器人:layout_height =WRAP_CONTENT机器人:文本=/>
<按钮机器人:ID =@ + ID / button1的机器人:layout_width =FILL_PARENT机器人:layout_height =WRAP_CONTENT机器人:文本=核心设备1机器人:的onClick =onPopupBtClick/>
<按钮机器人:ID =@ + ID /按钮2的android:layout_width =FILL_PARENT机器人:layout_height =WRAP_CONTENT机器人:文本=核心设备2的机器人:的onClick =onPopupBtClick/>
<按钮机器人:ID =@ + ID /按钮3的android:layout_width =FILL_PARENT机器人:layout_height =WRAP_CONTENT机器人:文本=核心设备3安卓的onClick =onPopupBtClick/>
< / LinearLayout中>
logcat的:
21 03-12:22:28.194:E /跟踪(1648):错误打开跟踪文件:没有这样的文件或目录(2)
03-12 21:22:28.714:D / dalvikvm(1648):GC_FOR_ALLOC释放64K,8%的免费2467K / 2676K,暂停83ms,总86ms
03-12 21:22:28.794:I / dalvikvm堆(1648):成长堆(frag的情况下),以6.062MB为3686416字节分配
03-12 21:22:28.854:D / dalvikvm(1648):GC_FOR_ALLOC释放2K,4%免费6065K / 6280K,暂停53ms,总53ms
03-12 21:22:28.971:D / dalvikvm(1648):GC_CONCURRENT释放< 1K,4%免费6065K / 6280K,暂停为8ms + 7毫秒,总118ms
03-12 21:22:30.784:D / libEGL(1648):加载/system/lib/egl/libEGL_emulation.so
03-12 21:22:30.793:D /(1648):HostConnection ::得到()新主机连接建立0x2a1a5180,TID 1648
03-12 21:22:30.885:D / libEGL(1648):加载/system/lib/egl/libGLESv1_CM_emulation.so
03-12 21:22:30.904:D / libEGL(1648):加载/system/lib/egl/libGLESv2_emulation.so
03-12 21:22:31.024:W / EGL_emulation(1648):eglSurfaceAttrib没有实现
03-12 21:22:31.055:D / OpenGLRenderer(1648):启用调试模式0
03-12 21:22:32.973:D / dalvikvm(1648):GC_FOR_ALLOC释放12K,3%的自由6125K / 6280K,暂停84ms,总99ms
03-12 21:22:33.063:I / dalvikvm堆(1648):成长堆(frag的情况下),以9.634MB为3686416字节分配
03-12 21:22:33.333:D / dalvikvm(1648):GC_CONCURRENT释放2K,2%自由9722K / 9884K,暂停110毫秒+ 8ms的,总261ms
03-12 21:22:35.783:W / EGL_emulation(1648):eglSurfaceAttrib没有实现
03-12 21:25:49.243:D / AndroidRuntime(1648):关闭VM
03-12 21:25:49.243:W / dalvikvm(1648):主题ID = 1:螺纹未捕获的异常退出(组= 0x40a71930)
03-12 21:25:49.393:E / AndroidRuntime(1648):致命异常:主要
03-12 21:25:49.393:E / AndroidRuntime(1648):java.lang.IllegalStateException:找不到在活动课com.example.linkingmanager.AppActivity对视图类的Android的onClick处理程序的方法onPopupBtClick(查看)。 widget.Button ID为按钮1
03-12 21:25:49.393:E / AndroidRuntime(1648):在android.view.View $ 1.onClick(View.java:3586)
03-12 21:25:49.393:E / AndroidRuntime(1648):在android.view.View.performClick(View.java:4204)
03-12 21:25:49.393:E / AndroidRuntime(1648):在android.view.View $ PerformClick.run(View.java:17355)
03-12 21:25:49.393:E / AndroidRuntime(1648):在android.os.Handler.handleCallback(Handler.java:725)
03-12 21:25:49.393:E / AndroidRuntime(1648):在android.os.Handler.dispatchMessage(Handler.java:92)
03-12 21:25:49.393:E / AndroidRuntime(1648):在android.os.Looper.loop(Looper.java:137)
03-12 21:25:49.393:E / AndroidRuntime(1648):在android.app.ActivityThread.main(ActivityThread.java:5041)
03-12 21:25:49.393:E / AndroidRuntime(1648):在java.lang.reflect.Method.invokeNative(本机方法)
03-12 21:25:49.393:E / AndroidRuntime(1648):在java.lang.reflect.Method.invoke(Method.java:511)
03-12 21:25:49.393:E / AndroidRuntime(1648):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)
03-12 21:25:49.393:E / AndroidRuntime(1648):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-12 21:25:49.393:E / AndroidRuntime(1648):在dalvik.system.NativeStart.main(本机方法)
03-12 21:25:49.393:E / AndroidRuntime(1648):java.lang.NoSuchMethodException:致onPopupBtClick [类android.view.View]
03-12 21:25:49.393:E / AndroidRuntime(1648):在java.lang.Class.getConstructorOrMethod(Class.java:460)
03-12 21:25:49.393:E / AndroidRuntime(1648):在java.lang.Class.getMethod(Class.java:915)
03-12 21:25:49.393:E / AndroidRuntime(1648):在android.view.View $ 1.onClick(View.java:3579)
03-12 21:25:49.393:E / AndroidRuntime(1648):11 ...更多
通过调用这些一个接一个:
按钮=(按钮)findViewById(R.id.button1);
按钮=(按钮)findViewById(R.id.button2); //这个抹杀按钮的previous价值
按钮=(按钮)findViewById(R.id.button3); //这个抹杀按钮的previous价值
您是的覆盖的的previous变量。或者使用不同的变量或覆盖值之前做你想做的工作:
按钮=(按钮)findViewById(R.id.button1);
button.setOnClickListener(mOnClickListener);
//等等。按钮=(按钮)findViewById(R.id.button2);
button.setOnClickListener(mOnClickListener);
//等等。
至于你看到错误,你已经在你的XML与定义的OnClickListener:
安卓的onClick =onPopupBtClick
但没有写在你的活动这种方法...
公共无效onPopupBtClick(查看视图){
// 做一点事
}
I'm attempting to prompt an AlertDialog
whenever a button is pressed however I've only been able to successfully implement (using a tutorial) a successful AlertDialog
on the first of three buttons.
Once I added:
button = (Button) findViewById(R.id.button2);
button = (Button) findViewById(R.id.button3);
none of the buttons work anymore and the application force closes I know I'm overlooking something simple or simply not coding this correctly.
(Any help is GREATLY appreciated!)
JAVA:
package com.example.linkingmanager;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class AppActivity extends Activity {
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.newdevicebtn:
startActivity(new Intent(this, App2Activity.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
final Context context = this;
private Button button;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.button1);
button = (Button) findViewById(R.id.button2);
button = (Button) findViewById(R.id.button3);
// add button listener
button.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
// set title
alertDialogBuilder.setTitle("Settings Menu");
// set dialog message
alertDialogBuilder
.setMessage("Delete Edit or Link?")
.setCancelable(false)
.setNeutralButton("Edit", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
//start new activity
Intent intentApp2Activity = new Intent(AppActivity.this, User1Settings.class);
startActivity(intentApp2Activity);
}
})
.setPositiveButton("Link",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
//start new activity
Intent intentApp2Activity = new Intent(AppActivity.this, User1.class);
startActivity(intentApp2Activity);
// if this button is clicked, close
// current activity
AppActivity.this.finish();
}
})
.setNegativeButton("Delete",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// if this button is clicked, just close
// the dialog box and do nothing
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
});
}}
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/link_devices"
android:orientation="vertical" >
<TextView android:textAppearance="?android:textAppearanceLarge" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" />
<Button android:id="@+id/button1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Core Device 1" android:onClick="onPopupBtClick" />
<Button android:id="@+id/button2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Core Device 2" android:onClick="onPopupBtClick" />
<Button android:id="@+id/button3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Core Device 3" android:onClick="onPopupBtClick" />
</LinearLayout>
LOGCAT:
03-12 21:22:28.194: E/Trace(1648): error opening trace file: No such file or directory (2)
03-12 21:22:28.714: D/dalvikvm(1648): GC_FOR_ALLOC freed 64K, 8% free 2467K/2676K, paused 83ms, total 86ms
03-12 21:22:28.794: I/dalvikvm-heap(1648): Grow heap (frag case) to 6.062MB for 3686416-byte allocation
03-12 21:22:28.854: D/dalvikvm(1648): GC_FOR_ALLOC freed 2K, 4% free 6065K/6280K, paused 53ms, total 53ms
03-12 21:22:28.971: D/dalvikvm(1648): GC_CONCURRENT freed <1K, 4% free 6065K/6280K, paused 8ms+7ms, total 118ms
03-12 21:22:30.784: D/libEGL(1648): loaded /system/lib/egl/libEGL_emulation.so
03-12 21:22:30.793: D/(1648): HostConnection::get() New Host Connection established 0x2a1a5180, tid 1648
03-12 21:22:30.885: D/libEGL(1648): loaded /system/lib/egl/libGLESv1_CM_emulation.so
03-12 21:22:30.904: D/libEGL(1648): loaded /system/lib/egl/libGLESv2_emulation.so
03-12 21:22:31.024: W/EGL_emulation(1648): eglSurfaceAttrib not implemented
03-12 21:22:31.055: D/OpenGLRenderer(1648): Enabling debug mode 0
03-12 21:22:32.973: D/dalvikvm(1648): GC_FOR_ALLOC freed 12K, 3% free 6125K/6280K, paused 84ms, total 99ms
03-12 21:22:33.063: I/dalvikvm-heap(1648): Grow heap (frag case) to 9.634MB for 3686416-byte allocation
03-12 21:22:33.333: D/dalvikvm(1648): GC_CONCURRENT freed 2K, 2% free 9722K/9884K, paused 110ms+8ms, total 261ms
03-12 21:22:35.783: W/EGL_emulation(1648): eglSurfaceAttrib not implemented
03-12 21:25:49.243: D/AndroidRuntime(1648): Shutting down VM
03-12 21:25:49.243: W/dalvikvm(1648): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
03-12 21:25:49.393: E/AndroidRuntime(1648): FATAL EXCEPTION: main
03-12 21:25:49.393: E/AndroidRuntime(1648): java.lang.IllegalStateException: Could not find a method onPopupBtClick(View) in the activity class com.example.linkingmanager.AppActivity for onClick handler on view class android.widget.Button with id 'button1'
03-12 21:25:49.393: E/AndroidRuntime(1648): at android.view.View$1.onClick(View.java:3586)
03-12 21:25:49.393: E/AndroidRuntime(1648): at android.view.View.performClick(View.java:4204)
03-12 21:25:49.393: E/AndroidRuntime(1648): at android.view.View$PerformClick.run(View.java:17355)
03-12 21:25:49.393: E/AndroidRuntime(1648): at android.os.Handler.handleCallback(Handler.java:725)
03-12 21:25:49.393: E/AndroidRuntime(1648): at android.os.Handler.dispatchMessage(Handler.java:92)
03-12 21:25:49.393: E/AndroidRuntime(1648): at android.os.Looper.loop(Looper.java:137)
03-12 21:25:49.393: E/AndroidRuntime(1648): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-12 21:25:49.393: E/AndroidRuntime(1648): at java.lang.reflect.Method.invokeNative(Native Method)
03-12 21:25:49.393: E/AndroidRuntime(1648): at java.lang.reflect.Method.invoke(Method.java:511)
03-12 21:25:49.393: E/AndroidRuntime(1648): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-12 21:25:49.393: E/AndroidRuntime(1648): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-12 21:25:49.393: E/AndroidRuntime(1648): at dalvik.system.NativeStart.main(Native Method)
03-12 21:25:49.393: E/AndroidRuntime(1648): Caused by: java.lang.NoSuchMethodException: onPopupBtClick [class android.view.View]
03-12 21:25:49.393: E/AndroidRuntime(1648): at java.lang.Class.getConstructorOrMethod(Class.java:460)
03-12 21:25:49.393: E/AndroidRuntime(1648): at java.lang.Class.getMethod(Class.java:915)
03-12 21:25:49.393: E/AndroidRuntime(1648): at android.view.View$1.onClick(View.java:3579)
03-12 21:25:49.393: E/AndroidRuntime(1648): ... 11 more
By calling these one after another:
button = (Button) findViewById(R.id.button1);
button = (Button) findViewById(R.id.button2); // This obliterates the previous value of button
button = (Button) findViewById(R.id.button3); // This obliterates the previous value of button
You are overwriting the previous variable. Either use different variables or do the work you want before overriding its value:
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(mOnClickListener);
//etc.
button = (Button) findViewById(R.id.button2);
button.setOnClickListener(mOnClickListener);
//etc.
As far as the error you see, you have defined an OnClickListener in your XML with:
android:onClick="onPopupBtClick"
But failed to write this method in your Activity...
public void onPopupBtClick(View view) {
// Do something
}
这篇关于按钮的OnClick听众打电话习惯警报对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!