java Android:无法从视图投射到按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2805983/
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-10-29 22:56:03  来源:igfitidea点击:

Android : Cannot cast from View to Button

javaandroidbuttonviewcasting

提问by Mike

I just started working with the Android, but seem to have come across a problem that I simply can't find the answer to. I get the error "Cannot cast from View to Button" on this line :

我刚开始使用 Android,但似乎遇到了一个我根本找不到答案的问题。我在这一行收到错误“无法从视图投射到按钮”:

Button myButton = (Button)findViewById(R.id.my_button);

I've tried many different things to get it going, and I've searched for the answer, but for some reason it just refuses to work right. If anybody could point me in the right direction it'd be most appreciated.

我尝试了很多不同的方法来让它继续运行,并且我已经寻找了答案,但由于某种原因它只是拒绝正常工作。如果有人能指出我正确的方向,我将不胜感激。

Thank in advance.

预先感谢。

回答by sanjay

try this,

试试这个,

android.widget.Button myButton = (android.widget.Button)findViewById(R.id.my_button); 

回答by CaseyB

Is the exception a ClassCastException? If so then the view you are finding with the id my_buttonisn't a button. If it's a NullPointerException then there is no view with the id my_button. This could be cause by not calling setContent() before you try to find the views.

异常是 ClassCastException 吗?如果是这样,那么您找到的带有 id 的视图my_button不是按钮。如果它是 NullPointerException,则没有 ID 为 my_button 的视图。这可能是由于在尝试查找视图之前未调用 setContent() 造成的。

回答by Bewczardski

I too have encountered this error and was unable to find a reason why it was happening. Like Joe Plante points out if it isn't working then something is wrong....

我也遇到过这个错误,但找不到它发生的原因。就像乔·普兰特 (Joe Plante) 指出的那样,如果它不起作用,那么就会出现问题......

In my scenario I happened to give the Relative Layout an id (ie clicking in a blank space in the graphical layout) the same ID as my button. This was causing the wrong View to be returned by FindViewById(R.id.my_button);

在我的场景中,我碰巧给了相对布局一个 id(即在图形布局中的空白处单击)与我的按钮相同的 ID。这导致 FindViewById(R.id.my_button); 返回错误的视图;

To check this in your xml see if the

要在您的 xml 中检查这一点,请查看

<RelativeLayout xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <Button
        android:id="@+id/btnRecipe"

回答by Chris

My guess is that your XML view (my_button) is not a button. Otherwise, are you sure that the id for your button is correct? It should read like "android:id="@+id/my_button"" as well as having a width and a height. Please post your XML file.

我的猜测是您的 XML 视图 (my_button) 不是按钮。否则,您确定按钮的 id 正确吗?它应该读作“ android:id="@+id/my_button"”以及具有宽度和高度。请发布您的 XML 文件。

回答by Chris

In addition toeverything else already mentioned, I would also suggest you take a look at your import statements at the top of the document. I've found this one in particular to be very annoying:

除了已经提到的所有其他内容之外,我还建议您查看文档顶部的导入语句。我发现这个特别烦人:

import android.R;

Doing this means every time you reference R.id.my_button in the rest of your program, it looks through Android's default resources instead of your own.

这样做意味着每次您在程序的其余部分引用 R.id.my_button 时,它都会查看 Android 的默认资源而不是您自己的资源。

回答by Joe Plante

I actually encountered this in code I was doing. Basically it was the "If it looks like a duck and if it sounds like a duck, then why the bloody insert expletive herearen't you seeing it as a duck?" situation. I changed the equivalent of android:id="@+id/someid" by changing it to equivalent of android:id="@+id/someid_x" (don't forget to do this in Java as well) and everything worked like clockwork again.

我实际上在我正在做的代码中遇到了这个。基本上它是“如果它看起来像一只鸭子,如果它听起来像一只鸭子,那么为什么这里的血腥插入咒语不是你把它看作一只鸭子?” 情况。我改变了 android:id="@+id/someid" 的等价物,将其更改为 android:id="@+id/someid_x" 等价物(不要忘记在 Java 中也这样做),一切都像再次发条。

So, in my situation, I believe that there might have been an ID out there referencing another object in the R table, and it was getting the wrong or unintended item. The strangest thing is that it started to happen when put a set of views into a RelativeLayout

所以,在我的情况下,我相信可能有一个 ID 引用了 R 表中的另一个对象,并且它得到了错误或意外的项目。最奇怪的是,当把一组视图放入一个RelativeLayout时,它开始发生

回答by Jefri singh

this class cast exception occurred while calling the widget type mismatch like you having one Image View image1, but you are calling that in java code by using ImageButton ib=(ImageButto) findViewById(R.id.image1); that time you get the error message like that

此类转换异常发生在调用小部件类型不匹配时,就像您有一个 Image View image1,但您在 Java 代码中使用 ImageButton ib=(ImageButto) findViewById(R.id.image1); 那个时候你会收到这样的错误信息

回答by Ajay Vijayasarathy

Check if you have missed this line

检查您是否错过了这一行

import android.view.View;

import android.view.View;

回答by sonichy

I met this in Android Studio 3.5, the IDE has not discover library by intelligence.
I solved this by manually insert the library:

我在 Android Studio 3.5 中遇到过这个,IDE 没有智能发现库。
我通过手动插入库解决了这个问题:

import android.widget.Button;

回答by Cyr1lfiggus

In my case it was the failure to import the android.widget.Button. Once this was complete the errors resolved.

就我而言,是导入 android.widget.Button 失败。一旦完成,错误就解决了。

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.TextView;
import android.widget.Button;