按钮OnClick Listener不会调用Alert Dialog

时间:2013-03-12 21:40:40

标签: java android eclipse onclicklistener

我试图在按下按钮时提示AlertDialog但是我只能在三个按钮中的第一个按钮上成功实现(使用教程)成功的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>

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

3 个答案:

答案 0 :(得分:3)

通过逐个调用这些:

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"

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

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

答案 1 :(得分:2)

删除onPopupBtClick关闭xml视图。它在您的活动中找不到名为onPopupBtClick的方法,因此崩溃

此外,您无法为同一个Button变量分配3个不同的视图。

答案 2 :(得分:0)

public class MainActivity extends Activity {
private Button button1,button2,button3;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button1 = (Button) findViewById(R.id.button1);
    button2 = (Button) findViewById(R.id.button2);
    button3 = (Button) findViewById(R.id.button3);

    /*
     * Implementation for dynamic OnClicklisteners
     */
//      button1.setOnClickListener(new OnClickListener(){
//          @Override
//          public void onClick(View v) {
//              openDialog(v);
//          }
//      });
//      button2.setOnClickListener(new OnClickListener(){
//          @Override
//          public void onClick(View v) {
//              openDialog(v);
//          }
//      });
//      button3.setOnClickListener(new OnClickListener(){
//          @Override
//          public void onClick(View v) {
//              openDialog(v);
//          }
//      });

}
public void openDialog(View v){
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this)
    .setTitle("Settings Menu")
    .setMessage("Delete Edit or Link?")
    .setCancelable(false)
    .setNeutralButton("Edit", new DialogInterface.OnClickListener() {
         public void onClick(DialogInterface dialog,int id) {
             Toast.makeText(MainActivity.this, "Clicked Edit",Toast.LENGTH_LONG).show();
         }
    })
    .setPositiveButton("Link",new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog,int id) {
            Toast.makeText(MainActivity.this, "Clicked Link",Toast.LENGTH_LONG).show();
        }
    })    
    .setNegativeButton("Delete",new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog,int id) {

            dialog.cancel();
        }
     });

    AlertDialog alertDialog = alertDialogBuilder.create();

    alertDialog.show();
}        





<?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="#FFF"
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="openDialog" />
<Button android:id="@+id/button2" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Core Device 2" 
    android:onClick="openDialog" />
<Button android:id="@+id/button3" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Core Device 3" 
    android:onClick="openDialog" />
</LinearLayout>

你可以看到来源 https://github.com/darussian/SimpleAndroidDialogExample