I keep getting a runtime error when launching my activity and it says android.widget.textview cannot be cast to android.widget.button?
XML:
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
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" />
Hope that helps!
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:
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
In Eclipse go to Project > Clean, choose your project, and it's all... Your app will run normally...
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..
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