java - 按钮 OnClick 监听器不会调用警报对话框

标签 java android eclipse onclicklistener

每当按下按钮时,我都会尝试提示 AlertDialog,但是我只能在三个按钮中的第一个。

添加后:

button = (Button) findViewById(R.id.button2);
button = (Button) findViewById(R.id.button3);

所有按钮都不再起作用并且应用程序强制关闭我知道我忽略了一些简单的事情或者根本没有正确编码。

(非常感谢任何帮助!)

<小时/>

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>
<小时/>

日志:

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

最佳答案

通过依次调用这些:

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

您正在覆盖先前的变量。要么使用不同的变量,要么在覆盖它的值之前做你想要的工作:

button = (Button) findViewById(R.id.button1);
button.setOnClickListener(mOnClickListener);
//etc.

button = (Button) findViewById(R.id.button2);
button.setOnClickListener(mOnClickListener);
//etc.
<小时/>

就您看到的错误而言,您已在 XML 中定义了一个 OnClickListener:

android:onClick="onPopupBtClick"

但未能在您的 Activity 中编写此方法...

public void onPopupBtClick(View view) {
    // Do something
}

关于java - 按钮 OnClick 监听器不会调用警报对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15372732/

相关文章:

c++ - 向 Java 开发人员的 Eclipse Oxygen IDE 添加 C/C++ 语言

java - 在 Java 中读取 Windows 文件摘要属性(标题、主题、作者)

java - 如何在Spring Boot应用程序中正确使用log4j2

Android 如何在我的自定义 Listview 中实现 AsyncTask

android - 为什么我的本地视频流在通话期间会旋转?

java - Eclipse 在每次保存时构建所有类

java - 我在 eclipse Photon Release (4.8.0)、OS-win10、java 版本 10.0.2 上安装 TestNG 时遇到问题

java - 默认构造函数的隐式 super 构造函数 AbstractNcssCountRule() 未定义。必须定义显式构造函数

java - 使用 JavaScript 可视化 Java 程序

java - 导致构建错误的 Android Intent setType 和 addCategory 方法