Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/386.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在警报对话框中添加第三个按钮会使应用程序崩溃_Java_Android_Eclipse_Android Alertdialog - Fatal编程技术网

Java 在警报对话框中添加第三个按钮会使应用程序崩溃

Java 在警报对话框中添加第三个按钮会使应用程序崩溃,java,android,eclipse,android-alertdialog,Java,Android,Eclipse,Android Alertdialog,我已经创建了一个布局,单击第一个按钮会触发一个带有两个按钮的警报对话框。当我尝试添加第三个按钮时,整个应用程序崩溃,我不知道为什么。(我已经包括了我的源代码和logcat——希望它能帮上忙!)我知道我忽略了一些简单的东西——但是如果您能帮我找出这个问题,我将不胜感激。(在它开始崩溃之前添加的唯一代码行是: .setNeutralButton("Cancel", this) …如果你去掉上面的线-一切正常 JAVA XML logcat 03-12 14:51:41.037:E/T

我已经创建了一个布局,单击第一个按钮会触发一个带有两个按钮的警报对话框。当我尝试添加第三个按钮时,整个应用程序崩溃,我不知道为什么。(我已经包括了我的源代码和logcat——希望它能帮上忙!)我知道我忽略了一些简单的东西——但是如果您能帮我找出这个问题,我将不胜感激。(在它开始崩溃之前添加的唯一代码行是:

 .setNeutralButton("Cancel", this) 
…如果你去掉上面的线-一切正常


JAVA
XML


logcat
03-12 14:51:41.037:E/Trace(1425):打开跟踪文件时出错:没有这样的文件或目录(2)
3-12 14:51:41.717:D/dalvikvm(1425):释放61K的所有元素的GC_,8%的自由元素2467K/2660K,暂停48ms,总计52ms
03-12 14:51:41.817:I/dalvikvm堆(1425):为3686416字节分配将堆(frag案例)增长到6.049MB
03-12 14:51:41.877:D/dalvikvm(1425):释放2K的所有物质的GC_,4%的自由6065K/6264K,暂停59毫秒,总计59毫秒

03-12 14:51:41.967:D/dalvikvm(1425):GC\u CONCURRENT freed您传递了错误的
OnClickListener
。它需要是一个
对话框接口。OnClickListener
。通过使用
this
传递当前对象实例,您传递了一个
视图。OnClickListener
,它与
对话框接口。OnClickListener
不同。 改变


您传递了错误的
OnClickListener
。它需要是一个
对话框接口。OnClickListener
。通过将当前对象实例传递给
this
,您传递了一个
视图。OnClickListener
,它与
DialogInterface.OnClickListener
不同。 改变


@user2161499不客气:-)还有,欢迎来到Stackoverflow!由于我的回答解决了您的问题,您应该通过单击来将其标记为已接受。(我已经这样做了-对延迟表示歉意-我只是在等待5分钟后才能这样做!)再次感谢@user2161499不客气:-)还有,欢迎来到Stackoverflow!由于我的回答解决了您的问题,您应该通过单击来将其标记为已接受。(我已经这样做了-对延迟表示歉意-我只是在等待5分钟后才能这样做!)再次感谢!
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 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)
 .setNeutralButton("Cancel", this)
 .setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog,int id) {
            //your implementation here
    }
  })