gpt4 book ai didi

java - 将第三个按钮添加到警报对话框会使应用程序崩溃

转载 作者:行者123 更新时间:2023-11-29 03:36:16 26 4
gpt4 key购买 nike

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

 .setNeutralButton("Cancel", this) 

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


Java

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com