开发者

Android : Cannot cast from View to Button

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 lin开发者_如何学编程e :

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.


try this,

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


Is the exception a ClassCastException? If so then the view you are finding with the id my_button isn'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.


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....

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);

To check this in your xml see if the

<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"


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.


In addition to everything 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.


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 here aren'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.

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


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


Check if you have missed this line

import android.view.View;


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

import android.widget.Button;


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

import androidx.appcompat.app.AppCompatActivity;

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


In my case, I had created an activity, named 'Button'. This contradict with button widget. When I change my activity name with "ButtonActivity", then the problem has gone.


In my case, I missed that I'm using an ImageButton.

Check it too

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜