Android.widget,textView cannot be cast to android.widget,button
Asked Answered
B

11

28

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

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.access$600(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
Besetting answered 17/9, 2012 at 19:1 Comment(8)
You are probably doing (Button) findViewById(R.id.actuallyATextView) somewhere.Somersomers
Need to see your Activity code that creates the views, but suspect you are attempting to cast a TextView to a Button, for example: TextView textViewName = (Button) findViewById(R.id.textViewName); would cause such an error.Capriola
The code you posted looks fine... Please post the LogCat errors. Perhaps the error is in a different file?Prismatic
I added the logcat. But i have no idea what is happeningBesetting
Hi this error is easy but can't understaningIsodiametric
Type Casting like button textview ,imageIsodiametric
Button b=findviewbyid(R.id.btn);Isodiametric
textview to include in xml fileIsodiametric
P
90

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

This is the important information in your LogCat file:

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:

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

But this line is fine and the XML for btnBattle looks fine too...

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


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 onClick attribute. First create a method (call it launchClick()) in your Activity like so:

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:onClick to every Button that you should have this behavior in 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" />
Prismatic answered 17/9, 2012 at 21:20 Comment(5)
I'm glad cleaning worked, but notice that I updated my answer. (I removed implements OnClickListener because that is not necessary.)Prismatic
Always forget to clean the project :/!!! I searched and you saved me from asking a question!!! Thanks. (UPVOTED)Interruption
Thanks! Cleaning did the trick ! I would have never guessed it! Co can we say its a bug in adt?Autumnautumnal
Cleaning the project especially after modifying stuff in your "res" folder seems to be a more valuable trick then I'd expect.Niphablepsia
well said sam! it works after cleaning of the project. thanksLeonardaleonardi
R
28

Project -> Clean can help you.

Roark answered 7/7, 2013 at 12:3 Comment(0)
F
4

you are trying to cast TextView to Button.

for TextView do this :

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

for Button :

Button btn1 = (Button)findviewById(R.id.your buttonid present in xml layout file);
Fattal answered 17/9, 2012 at 19:18 Comment(2)
or do like this:-android.widget.Button myButton = (android.widget.Button)findViewById(R.id.my_button);Fattal
This did the trick too! by the way, if you get environment error, I suggest you restart the IDE and try cleaning again afterHuygens
M
1

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

Megrims answered 15/4, 2013 at 19:58 Comment(0)
S
1

First make sure all TextView addressing the right TextView in your .java file.. Like this..

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 Button instead of TextView.

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

Selfexplanatory answered 6/7, 2013 at 7:31 Comment(0)
F
1

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.

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

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

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

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

Filature answered 10/9, 2013 at 21:33 Comment(0)
P
0

Please attach your java code to find out where the problem is occuring. This problem comes when you are trying to convert the textview to button.

Peraea answered 17/9, 2012 at 19:6 Comment(0)
S
0
  1. Project > Clean

Or

  1. change itemes Id's

it help :D

Substance answered 18/10, 2014 at 21:13 Comment(0)
K
0

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.

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

Kenton answered 19/10, 2016 at 4:16 Comment(0)
F
0

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

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

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.

Cheers

Forcefeed answered 23/6, 2017 at 15:19 Comment(0)
C
0

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

Crabby answered 24/4, 2020 at 17:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.