我创建了一个布局,该布局在单击第一个按钮时将触发一个带有两个按钮的警报对话框。当我尝试添加第三个按钮时-整个应用程序崩溃了,我不确定为什么。 (我已经包含了我的源代码和logcat-希望它会有所帮助!)我知道我忽略了一些简单的事情-但对识别此问题的任何帮助将不胜感激。 (在开始崩溃之前添加的唯一代码行是:

 .setNeutralButton("Cancel", this)


...如果您删除上面的行-一切正常。



爪哇

package com.example.linkingmanager;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.content.Intent;

public class AppActivity extends Activity {

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

// 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("Link or Delete?")
        .setCancelable(false)
        .setNeutralButton("Cancel", this)
        .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" />
<Button android:id="@+id/Button3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Core Device 3" />
</LinearLayout>




日志猫

03-12 14:51:41.037: E/Trace(1425): error opening trace file: No such file or directory (2)
03-12 14:51:41.717: D/dalvikvm(1425): GC_FOR_ALLOC freed 61K, 8% free 2467K/2660K, paused 48ms, total 52ms
03-12 14:51:41.817: I/dalvikvm-heap(1425): Grow heap (frag case) to 6.049MB for 3686416-byte allocation
03-12 14:51:41.877: D/dalvikvm(1425): GC_FOR_ALLOC freed 2K, 4% free 6065K/6264K, paused 59ms, total 59ms
03-12 14:51:41.967: D/dalvikvm(1425): GC_CONCURRENT freed <1K, 4% free 6064K/6264K, paused 7ms+16ms, total 89ms
03-12 14:51:43.258: D/libEGL(1425): loaded /system/lib/egl/libEGL_emulation.so
03-12 14:51:43.268: D/(1425): HostConnection::get() New Host Connection established 0x2a1825a8, tid 1425
03-12 14:51:43.340: D/libEGL(1425): loaded /system/lib/egl/libGLESv1_CM_emulation.so
03-12 14:51:43.428: D/libEGL(1425): loaded /system/lib/egl/libGLESv2_emulation.so
03-12 14:51:43.557: W/EGL_emulation(1425): eglSurfaceAttrib not implemented
03-12 14:51:43.577: D/OpenGLRenderer(1425): Enabling debug mode 0
03-12 14:51:45.047: D/dalvikvm(1425): GC_FOR_ALLOC freed 12K, 3% free 6125K/6268K, paused 72ms, total 80ms
03-12 14:51:45.137: I/dalvikvm-heap(1425): Grow heap (frag case) to 9.623MB for 3686416-byte allocation
03-12 14:51:45.347: D/dalvikvm(1425): GC_CONCURRENT freed 2K, 2% free 9722K/9872K, paused 70ms+23ms, total 193ms
03-12 14:51:48.188: W/EGL_emulation(1425): eglSurfaceAttrib not implemented
03-12 14:52:51.677: D/AndroidRuntime(1425): Shutting down VM
03-12 14:52:51.677: W/dalvikvm(1425): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
03-12 14:52:51.772: E/AndroidRuntime(1425): FATAL EXCEPTION: main
03-12 14:52:51.772: E/AndroidRuntime(1425): java.lang.Error: Unresolved compilation problem:
03-12 14:52:51.772: E/AndroidRuntime(1425):     The method setNeutralButton(int, DialogInterface.OnClickListener)   in the type AlertDialog.Builder is not applicable for the arguments (String, new View.OnClickListener(){})
03-12 14:52:51.772: E/AndroidRuntime(1425):     at   com.example.linkingmanager.AppActivity$1.onClick(AppActivity.java:41)
03-12 14:52:51.772: E/AndroidRuntime(1425):     at android.view.View.performClick(View.java:4204)
03-12 14:52:51.772: E/AndroidRuntime(1425):     at android.view.View$PerformClick.run(View.java:17355)
03-12 14:52:51.772: E/AndroidRuntime(1425):     at android.os.Handler.handleCallback(Handler.java:725)
03-12 14:52:51.772: E/AndroidRuntime(1425):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-12 14:52:51.772: E/AndroidRuntime(1425):     at android.os.Looper.loop(Looper.java:137)
03-12 14:52:51.772: E/AndroidRuntime(1425):     at android.app.ActivityThread.main(ActivityThread.java:5041)
03-12 14:52:51.772: E/AndroidRuntime(1425):     at java.lang.reflect.Method.invokeNative(Native Method)
03-12 14:52:51.772: E/AndroidRuntime(1425):     at java.lang.reflect.Method.invoke(Method.java:511)
03-12 14:52:51.772: E/AndroidRuntime(1425):     at   com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-12 14:52:51.772: E/AndroidRuntime(1425):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-12 14:52:51.772: E/AndroidRuntime(1425):     at dalvik.system.NativeStart.main(Native Method)

最佳答案

您通过了错误的OnClickListener。它必须是DialogInterface.OnClickListener。通过使用this传递当前的Object实例,可以传递与View.OnClickListener不同的DialogInterface.OnClickListener
更改

 .setNeutralButton("Cancel", this)




 .setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog,int id) {
            //your implementation here
    }
  })

关于java - 在警报对话框中添加第三个按钮会使应用程序崩溃,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15364608/

10-12 02:41