草庐IT

java - 为自定义对话框扩充 View 时发生 ClassCastException

coder 2024-07-03 原文

我正在尝试将自定义对话框类型添加到 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 作为 View 组......但我想不出我还能放什么在充气机调用中。

编辑:以及 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);
    }
}

最佳答案

您是否尝试过删除 XML 中的 onClick 属性?

尝试绑定(bind)它们时可能会出现问题,因为它们不存在

(另外,您使用应用程序上下文的任何特殊原因?)

关于java - 为自定义对话框扩充 View 时发生 ClassCastException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6246492/

有关java - 为自定义对话框扩充 View 时发生 ClassCastException的更多相关文章

  1. ruby - Facter::Util::Uptime:Module 的未定义方法 get_uptime (NoMethodError) - 2

    我正在尝试设置一个puppet节点,但ruby​​gems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由ruby​​gems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby

  2. ruby-on-rails - Rails - 一个 View 中的多个模型 - 2

    我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何

  3. ruby-on-rails - 渲染另一个 Controller 的 View - 2

    我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>

  4. ruby-on-rails - Rails 3.2.1 中 ActionMailer 中的未定义方法 'default_content_type=' - 2

    我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer

  5. ruby-on-rails - form_for 中不在模型中的自定义字段 - 2

    我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢

  6. ruby - 主要 :Object when running build from sublime 的未定义方法 `require_relative' - 2

    我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby​​1.9+ 关于ruby-主要:Objectwhenrun

  7. java - 等价于 Java 中的 Ruby Hash - 2

    我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/

  8. ruby-on-rails - 如何在我的 Rails 应用程序 View 中打印 ruby​​ 变量的内容? - 2

    我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby​​中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R

  9. ruby-on-rails - 如何在 Rails View 上显示错误消息? - 2

    我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c

  10. ruby - 在 Ruby 中有条件地定义函数 - 2

    我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin

随机推荐