Android.widget,textView 不能投射到 android.widget,button

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12465551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:44:27  来源:igfitidea点击:

Android.widget,textView cannot be cast to android.widget,button

androidandroid-layoutandroid-intentandroid-widget

提问by Adariel Lzinski

I keep getting a runtime error when launching my activity and it says android.widget.textview cannot be cast to android.widget.button?

启动我的活动时,我不断收到运行时错误,它说无法将 android.widget.textview 转换为 android.widget.button?

XML:

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:ignore="HardcodedText" >

    <TextView
        android:id="@+id/textViewName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="false"
        android:layout_alignParentTop="false"
        android:text="Name:"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textViewLevel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/textViewName"
        android:text="Level:"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textViewClass"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="false"
        android:layout_below="@+id/textViewLevel"
        android:text="Class:"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textViewTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textViewClass"
        android:layout_alignBottom="@+id/textViewClass"
        android:layout_centerHorizontal="true"
        android:text="Title:"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textViewXP"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textViewTitle"
        android:layout_alignLeft="@+id/textViewTitle"
        android:text="XP:"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textViewGold"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textViewXP"
        android:layout_alignParentTop="true"
        android:text="Gold:"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textViewLineBreak"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textViewClass"
        android:text="________________________________________"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:visibility="invisible" />

    <TextView
        android:id="@+id/textViewGoldValue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/textViewGold"
        android:text="0"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@+id/textViewTitle"
        android:text="Stats" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button3"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="20dp"
        android:layout_toLeftOf="@+id/textViewTitle"
        android:text="Equipment" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/button2"
        android:layout_alignParentLeft="true"
        android:layout_alignRight="@+id/button2"
        android:layout_marginBottom="22dp"
        android:text="Inventory" />

    <Button
        android:id="@+id/Button01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button1"
        android:layout_alignBottom="@+id/button1"
        android:layout_alignLeft="@+id/button4"
        android:layout_alignParentRight="true"
        android:text="Shop" />

    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/button2"
        android:layout_toRightOf="@+id/textViewXP"
        android:text="Class Training" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button5"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/button1"
        android:text="Crafting" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textViewLineBreak"
        android:text="HP:"
        android:textAppearance="?android:attr/textAppearanceMedium" />


    <TextView
        android:id="@+id/textViewStrTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textViewLineBreak"
        android:layout_alignLeft="@+id/textViewGoldValue"
        android:text="N/A"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/textViewXPValue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textViewGoldValue"
        android:layout_below="@+id/textViewGoldValue"
        android:text="0"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textViewLevelValue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textViewClass"
        android:layout_toRightOf="@+id/textViewName"
        android:text="1"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/TextViewStrClass"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textViewLineBreak"
        android:layout_alignLeft="@+id/textViewLevelValue"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/textViewRace"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textViewClass"
        android:text="Race:"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView4"
        android:layout_alignBottom="@+id/textView4"
        android:layout_toLeftOf="@+id/TextViewStrClass"
        android:text="10"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Spinner
        android:id="@+id/spinnerMonsters"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView4"
        android:layout_marginTop="50dp"
        android:layout_toLeftOf="@+id/btnBattle"
        android:entries="@array/Monsters"
        android:prompt="@string/MonsterPrompt" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/spinnerMonsters"
        android:layout_alignRight="@+id/textViewXPValue"
        android:text="10"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Button
        android:id="@+id/btnBattle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/spinnerMonsters"
        android:text="Battle!" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textViewStrTitle"
        android:layout_alignRight="@+id/spinnerMonsters"
        android:layout_marginRight="14dp"
        android:text="/"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textViewXpNextLevel"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textViewLevel"
        android:layout_alignBottom="@+id/textViewLevel"
        android:layout_alignLeft="@+id/btnBattle"
        android:text="100"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textViewBattleResults"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/spinnerMonsters"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/textViewStrRace"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView1"
        android:layout_toRightOf="@+id/textViewClass"
        android:text="l"
        android:textAppearance="?android:attr/textAppearanceSmall" />


    <TextView
        android:id="@+id/textViewStrAlliance"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView2"
        android:layout_alignParentRight="true"
        android:layout_toRightOf="@+id/textViewStrTitle"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/textViewAlliance"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textViewTitle"
        android:layout_toRightOf="@+id/button1"
        android:text="Alliance:"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/textViewAlliance"
        android:layout_below="@+id/textViewLineBreak"
        android:text="MP:"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

What is happening here? I've gone through it a few times and can't find anything

这里发生了什么?我已经通过它几次,找不到任何东西

Edit:

编辑:

09-17 18:28:51.485: E/AndroidRuntime(724): FATAL EXCEPTION: main
09-17 18:28:51.485: E/AndroidRuntime(724): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.intent_buttontests/com.example.intent_buttontests.PlayScreen}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button
09-17 18:28:51.485: E/AndroidRuntime(724):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
09-17 18:28:51.485: E/AndroidRuntime(724):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
09-17 18:28:51.485: E/AndroidRuntime(724):  at android.app.ActivityThread.access0(ActivityThread.java:123)
09-17 18:28:51.485: E/AndroidRuntime(724):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
09-17 18:28:51.485: E/AndroidRuntime(724):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-17 18:28:51.485: E/AndroidRuntime(724):  at android.os.Looper.loop(Looper.java:137)
09-17 18:28:51.485: E/AndroidRuntime(724):  at android.app.ActivityThread.main(ActivityThread.java:4424)
09-17 18:28:51.485: E/AndroidRuntime(724):  at java.lang.reflect.Method.invokeNative(Native Method)
09-17 18:28:51.485: E/AndroidRuntime(724):  at java.lang.reflect.Method.invoke(Method.java:511)
09-17 18:28:51.485: E/AndroidRuntime(724):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-17 18:28:51.485: E/AndroidRuntime(724):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-17 18:28:51.485: E/AndroidRuntime(724):  at dalvik.system.NativeStart.main(Native Method)
09-17 18:28:51.485: E/AndroidRuntime(724): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button
09-17 18:28:51.485: E/AndroidRuntime(724):  at com.example.intent_buttontests.PlayScreen.onCreate(PlayScreen.java:110)
09-17 18:28:51.485: E/AndroidRuntime(724):  at android.app.Activity.performCreate(Activity.java:4465)
09-17 18:28:51.485: E/AndroidRuntime(724):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
09-17 18:28:51.485: E/AndroidRuntime(724):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
09-17 18:28:51.485: E/AndroidRuntime(724):  ... 11 more

回答by Sam

I added the logcat. But i have no idea what is happening

我添加了 logcat。但我不知道发生了什么

This is the important information in your LogCat file:

这是 LogCat 文件中的重要信息:

Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button
  at com.example.intent_buttontests.PlayScreen.onCreate(PlayScreen.java:110)

You read the error correctly, it is a ClassCastException. The lines below Caused by...tell you where the error was thrown, which is in PlayScreen.onCreate()on line 110. As best I can tell line 110 is:

您正确阅读了错误,它是一个ClassCastException. 下面的Caused by...几行告诉你错误是在哪里抛出的,PlayScreen.onCreate()在第 110 行。我能告诉你的第 110 行是:

Button btnBattle = (Button) findViewById(R.id.btnBattle);

But this line is fine and the XML for btnBattlelooks fine too...

但是这条线很好,而且 XML forbtnBattle看起来也很好......

I ran your Activity with your layout myself and didn't get any errors. Have you cleaned your project? Often this will remove these "phantom" errors. (In Eclipse, Project-> Clean...)

我自己用你的布局运行了你的 Activity 并且没有出现任何错误。你清理过你的项目吗?通常这会消除这些“幻影”错误。(在 Eclipse 中,项目->清理...



I do have one suggestion, you have a lot of Buttons that do similar tasks. You can do the same actions with much less code if you use the XML onClickattribute. First create a method (call it launchClick()) in your Activity like so:

我确实有一个建议,您有很多执行类似任务的按钮。如果您使用 XMLonClick属性,您可以用更少的代码执行相同的操作。首先launchClick()在您的 Activity 中创建一个方法(调用它),如下所示:

public void launchClick(View v) {
    Intent intent;

    switch(v.getId()) {
    case R.id.button1:
        intent = new Intent(PlayScreen.this, Inventory.class);
        break;

    case R.id.button2:
        intent = new Intent(PlayScreen.this, Equipment.class);
        break;

    // etc, etc
    }
    startActivityForResult(intent, 0);
};

And add the attribute android:onClickto every Button that you should have this behavior in play_screen.xml:

并将属性添加android:onClick到您应该在其中具有此行为的每个按钮play_screen.xml

<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_toLeftOf="@+id/textViewTitle"
    android:onClick="launchClick"
    android:text="Stats" />

Hope that helps!

希望有帮助!

回答by aviomaksim

Project -> Cleancan help you.

项目 -> 清洁可以帮助你。

回答by sanjay

you are trying to cast TextViewto Button.

您正在尝试投射TextViewButton.

for TextViewdo this :

TextView做到这一点:

TextView tv = (TextView)findviewById(R.id.your textviewid present in xml layout file);

for Button:

对于Button

Button btn1 = (Button)findviewById(R.id.your buttonid present in xml layout file);

回答by Leonardo Sapuy

In Eclipse go to Project > Clean, choose your project, and it's all... Your app will run normally...

在 Eclipse 中,转到 Project > Clean,选择您的项目,就这样了……您的应用程序将正常运行……

回答by Anil Chahal

First make sure all TextViewaddressing the right TextViewin your .java file.. Like this..

首先确保您的 .java 文件中的所有TextView地址都正确TextView.. 像这样..

TextView textview1 = (TextView) findViewById(R.id.textview1);

Error like Android.widget,textView cannot be cast to android.widget,button raised because some time we referred Buttoninstead of TextView.

像 Android.widget,textView 这样的错误无法转换为 android.widget,button 引发,因为有一段时间我们引用了Button而不是TextView.

If everything is ok then Clean your project and refresh.. It worked for me..

如果一切正常,那么清理你的项目并刷新..它对我有用..

回答by bob

I am using a RelativeLayout with 3 textViews and 2 buttons. I get the same bizarre error when I move the placement of the textViews using the graphical interface editor. If I set them back to where they originally were placed, the error goes away.

我正在使用带有 3 个文本视图和 2 个按钮的 RelativeLayout。当我使用图形界面编辑器移动 textViews 的位置时,我得到了同样的奇怪错误。如果我将它们放回原来放置的位置,错误就会消失。

I can get rid of the error by:

我可以通过以下方式摆脱错误:

  1. Cut all the data from the offending XML file to the clipboard.
  2. Save the project.
  3. Clean the project (see error in the XML file)
  4. Paste back the XML code and save
  5. Clean
  6. Run
  1. 将有问题的 XML 文件中的所有数据剪切到剪贴板。
  2. 保存项目。
  3. 清理项目(查看 XML 文件中的错误)
  4. 粘贴回 XML 代码并保存
  5. 干净的

This appears to be a bug since the XML code is legit, but does not get built at run time properly.

这似乎是一个错误,因为 XML 代码是合法的,但没有在运行时正确构建。

Hope it works for you. Eclipse Java EE IDE for Web Developers.

希望对你有效。面向 Web 开发人员的 Eclipse Java EE IDE。

Version: Indigo Service Release 2 Build id: 20120216-1857

版本:Indigo Service Release 2 版本号:20120216-1857

回答by Amir Bax

  1. Project > Clean
  1. 项目 > 清洁

Or

或者

  1. change itemes Id's
  1. 更改项目 ID

it help :D

它有帮助:D

回答by Kar Shiang Tey

I just encountered the same problem. However, i decided to use only "View" without casting. It works perfectly. "setOnClickListener" works the same for our case.

我刚刚遇到了同样的问题。但是,我决定只使用“查看”而不进行投射。它完美地工作。“setOnClickListener” 对我们的情况也一样。

e.g. View button = v.findViewById(button_id); button.setOnClickListener(...)

例如 View button = v.findViewById(button_id); button.setOnClickListener(...)

回答by David Parés

In case the project clean does not work. The cause might be related to use te same IDon more than one item.

如果项目清理不起作用。原因可能与在多个项目上使用相同的 ID相关。

Make sure when assigning ID′s via layout or programatically, that are not duplicated.

确保在通过布局或以编程方式分配 ID 时,不要重复。

I faced this issue while assinging low numbers as ID (probably not a best practice), as the ID's where colliding with RadioButton checked ID.

我在将低数字分配为 ID 时遇到了这个问题(可能不是最佳实践),因为 ID 与 RadioButton 检查的 ID 发生冲突。

Cheers

干杯

回答by Belema

I've had similar issue, mine was with ImageView instead of TextView, and when I went back to check my activity, I discovered that I had already declared a variable as Button, and then I assigned the variable to findViewById(R.id.ivrest), where ivRest refers to an ImageView, so just make sure if you declare a variable with a particular type, when reassigning the variable it's should be of the same type

我有类似的问题,我的是 ImageView 而不是 TextView,当我回去检查我的活动时,我发现我已经将一个变量声明为 Button,然后我将该变量分配给 findViewById(R.id. ivrest),其中 ivRest 指的是 ImageView,所以只要确保你声明了一个具有特定类型的变量,当重新分配变量时,它应该是相同的类型