我正在尝试向Android应用程序添加自定义对话框类型,但每当我按下应该显示对话框的按钮时,我都会关闭一个强制关闭。
强制关闭的日志输出如下:
06-05 22:53:28.413: ERROR/AndroidRuntime(187): Uncaught handler: thread main exiting due to uncaught exception
06-05 22:53:28.453: ERROR/AndroidRuntime(187): java.lang.IllegalStateException: Could not execute method of the activity
06-05 22:53:28.453: ERROR/AndroidRuntime(187): at android.view.View$1.onClick(View.java:2027)
06-05 22:53:28.453: ERROR/AndroidRuntime(187): at android.view.View.performClick(View.java:2344)
06-05 22:53:28.453: ERROR/AndroidRuntime(187): at android.view.View.onTouchEvent(View.java:4133)
06-05 22:53:28.453: ERROR/AndroidRuntime(187): at android.widget.TextView.onTouchEvent(TextView.java:6510)
06-05 22:53:28.453: ERROR/AndroidRuntime(187): at android.view.View.dispatchTouchEvent(View.java:3672)
06-05 22:53:28.453: ERROR/AndroidRuntime(187): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
06-05 22:53:28.453: ERROR/AndroidRuntime(187): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
06-05 22:53:28.453: ERROR/AndroidRuntime(187): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
06-05 22:53:28.453: ERROR/AndroidRuntime(187): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
06-05 22:53:28.453: ERROR/AndroidRuntime(187): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1712)
06-05 22:53:28.453: ERROR/AndroidRuntime(187): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1202)
06-05 22:53:28.453: ERROR/AndroidRuntime(187): at android.app.Activity.dispatchTouchEvent(Activity.java:1987)
06-05 22:53:28.453: ERROR/AndroidRuntime(187): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1696)
06-05 22:53:28.453: ERROR/AndroidRuntime(187): at android.view.ViewRoot.handleMessage(ViewRoot.java:1658)
06-05 22:53:28.453: ERROR/AndroidRuntime(187): at android.os.Handler.dispatchMessage(Handler.java:99)
06-05 22:53:28.453: ERROR/AndroidRuntime(187): at android.os.Looper.loop(Looper.java:123)
06-05 22:53:28.453: ERROR/AndroidRuntime(187): at android.app.ActivityThread.main(ActivityThread.java:4203)
06-05 22:53:28.453: ERROR/AndroidRuntime(187): at java.lang.reflect.Method.invokeNative(Native Method)
06-05 22:53:28.453: ERROR/AndroidRuntime(187): at java.lang.reflect.Method.invoke(Method.java:521)
06-05 22:53:28.453: ERROR/AndroidRuntime(187): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
06-05 22:53:28.453: ERROR/AndroidRuntime(187): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
06-05 22:53:28.453: ERROR/AndroidRuntime(187): at dalvik.system.NativeStart.main(Native Method)
06-05 22:53:28.453: ERROR/AndroidRuntime(187): Caused by: java.lang.reflect.InvocationTargetException
06-05 22:53:28.453: ERROR/AndroidRuntime(187): at nocom.autophage.bikecalc.BikeCalcMainMenu.showAddMeasurementDialog(BikeCalcMainMenu.java:69)
06-05 22:53:28.453: ERROR/AndroidRuntime(187): at java.lang.reflect.Method.invokeNative(Native Method)
06-05 22:53:28.453: ERROR/AndroidRuntime(187): at java.lang.reflect.Method.invoke(Method.java:521)
06-05 22:53:28.453: ERROR/AndroidRuntime(187): at android.view.View$1.onClick(View.java:2022)
06-05 22:53:28.453: ERROR/AndroidRuntime(187): ... 21 more
06-05 22:53:28.453: ERROR/AndroidRuntime(187): Caused by: java.lang.ClassCastException: android.widget.Button
06-05 22:53:28.453: ERROR/AndroidRuntime(187): at nocom.autophage.bikecalc.BikeCalcMainMenu.onCreateDialog(BikeCalcMainMenu.java:48)
06-05 22:53:28.453: ERROR/AndroidRuntime(187): at android.app.Activity.createDialog(Activity.java:867)
06-05 22:53:28.453: ERROR/AndroidRuntime(187): at android.app.Activity.showDialog(Activity.java:2408)
06-05 22:53:28.453: ERROR/AndroidRuntime(187): ... 25 more
创建对话框的代码如下所示: 案例DIALOG_ADD_MEASUREMENT: AlertDialog.Builder addMeasurementBuilder;
Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.add_measurement_dialog,
(ViewGroup) findViewById(R.id.add_measurement_dialog));
addMeasurementBuilder = new AlertDialog.Builder(mContext);
addMeasurementBuilder.setView(layout);
dialog = addMeasurementBuilder.create();
break;
对话框的XML(我最有信心的部分)是:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/add_measurement_dialog">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/add_measurement_dialog_title" />
<Spinner
android:id="@+id/add_measurement_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:prompt="@string/add_measurement_prompt" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/add_measurement_value" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="@string/calc"
android:onClick="showFeatureNotYetImplementedToast" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="@string/cancel"
android:onClick="showFeatureNotYetImplementedToast" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="@string/add"
android:onClick="showFeatureNotYetImplementedToast" />
</LinearLayout>
</LinearLayout>
我唯一能想到的是,也许我不应该使用xml的根元素的id作为视图组的对话框......但我无法弄清楚我还放了什么在inflater电话中。
编辑:以及BikeCalcMainMenu.java的完整内容: 包nocom.autophage.bikecalc;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
public class BikeCalcMainMenu extends Activity {
/** Called when the activity is first created. */
static final int DIALOG_HELP = 0;
static final int DIALOG_ADD_MEASUREMENT = 1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
protected Dialog onCreateDialog(int id) {
Dialog dialog;
switch(id) {
case DIALOG_HELP:
AlertDialog.Builder helpBuilder = new AlertDialog.Builder(this);
helpBuilder.setMessage(getText(R.string.h_help_text))
.setCancelable(false)
.setNeutralButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dismissDialog(DIALOG_HELP);
}
});
AlertDialog helpAlert = helpBuilder.create();
dialog = helpAlert;
break;
case DIALOG_ADD_MEASUREMENT:
AlertDialog.Builder addMeasurementBuilder;
// AlertDialog dialog;
Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.add_measurement_dialog, (ViewGroup) findViewById(R.id.add_measurement_dialog));
addMeasurementBuilder = new AlertDialog.Builder(mContext);
addMeasurementBuilder.setView(layout);
dialog = addMeasurementBuilder.create();
break;
default:
dialog = null;
}
return dialog;
}
public void showHelpScreenDialog(View v) {
showDialog(DIALOG_HELP);
}
public void showNotYetImplementedToast(View v) {
Toast not_yet_implemented_toast = Toast.makeText(getApplicationContext(), getText(R.string.feature_not_implemented), Toast.LENGTH_SHORT);
not_yet_implemented_toast.show();
}
public void showAddMeasurementDialog(View v) {
showDialog(DIALOG_ADD_MEASUREMENT);
}
}
答案 0 :(得分:0)
您是否尝试删除XML中的onClick属性?
尝试绑定它们时可能会出现问题,因为它们不存在
(另外,您使用应用程序上下文的任何特殊原因?)
答案 1 :(得分:0)
我没有对此进行过测试,但我认为此行会导致您的问题
View layout = inflater.inflate(R.layout.add_measurement_dialog, (ViewGroup) findViewById(R.id.add_measurement_dialog));
如果你提供第二个参数来膨胀那么你实际上得到了那个父组。考虑在这里传递null。
答案 2 :(得分:0)
在解决类似问题时,我刚刚把头撞到一两面墙上。
尝试拨打addMeasurementBuilder = new AlertDialog.Builder(this);
(而不是mContext
)并查看是否有效。
Android开发人员的Dialogs教程传递了getApplicationContext()
的结果,但我测试了代码(复制和粘贴),它也崩溃了!非常糟糕的形式,谷歌。
我在this question找到了答案。