为自定义对话框扩展视图时出现 ClassCastException
我正在尝试向 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 作为viewgroup...但我不知道我还要在充气器调用中添加什么内容。
编辑:以及 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);
}
}
I'm trying to add a custom dialog type to an Android application, but whenever I press the button that should bring up the dialog, I get a force close instead.
The log output from the force close is as follows:
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
The code that's creating the dialog looks like:
case 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;
And the XML (which is the part I'm most confident about) for the dialog is:
<?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>
The only thing I could think of was that maybe I shouldn't be using the id of the root element of the xml for the dialog as the viewgroup... but I can't figure out what else I'd put in the inflater call.
EDIT: And the complete contents of BikeCalcMainMenu.java:
package 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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否尝试过删除 XML 中的 onClick 属性?
当它尝试绑定它们时可能会出现问题,因为它们不存在
(另外,您使用应用程序上下文有什么特殊原因吗?)
Have you tried removing the onClick attributes in the XML?
There could be a problem when it tries to bind them, since they don't exist
(also, any particular reason you're using the application context?)
我还没有测试过这一点,但我相信这条线会导致你的问题
如果你提供第二个参数来膨胀,那么你实际上会得到那个父组。考虑在这里传递 null 。
授予 http://developer .android.com/reference/android/view/LayoutInflater.html#inflate%28int,%20android.view.ViewGroup%29 了解详细信息
I haven't tested this but I believe that this line causes your problem
If you provide the second argument to inflate then you actually get that parent group back. Consider just passing in null here.
Confer http://developer.android.com/reference/android/view/LayoutInflater.html#inflate%28int,%20android.view.ViewGroup%29 for details
在解决类似的问题时,我刚刚用头撞了一两堵墙。
尝试调用
addMeasurementBuilder = new AlertDialog.Builder(this);
(而不是使用mContext
)并查看是否有效。Android 开发人员的对话框教程传递了
getApplicationContext()
的结果,但我测试了代码(复制和粘贴),它也崩溃了!非常糟糕的形式,谷歌。我在这个问题中找到了答案。
I've just banged my head against a wall or two while solving a similar problem.
Try calling
addMeasurementBuilder = new AlertDialog.Builder(this);
(instead of withmContext
) and see if that works.The Dialogs tutorial on android developers passes in the result of
getApplicationContext()
, but I've tested the code (copied and pasted) and it crashes too! Very bad form, Google.I found the answer in this question.