4

I have a really really really strange issue here.

I have this line of code:

SeekBar mProgressBar =  (SeekBar) findViewById(R.id.SeekBar);

And it throws a ClassCastException: android.widget.Button as the title indicates. The XML file looks like this:

...
<SeekBar
android:id="@+id/SeekBar"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
/>
...

If I change the code to

Button mProgressBar =  (Button) findViewById(R.id.SeekBar);

It "works" (e.i that single line works. The programs then fails for other reasons because it's now a button). I have the exact same line in another file without problems. Any idea what could be wrong?

1
  • Do you have the correct R imported? Commented May 20, 2011 at 18:59

3 Answers 3

19

Must be an outdated auto-generated R class. Sometimes it gets messed up, i had it just moments ago. Try to clean the project and rebuild again. Or change something in the layout XML and save, Eclipse should refresh the R class (that is, if you're using eclipse).

Sign up to request clarification or add additional context in comments.

2 Comments

One guy reported that even rebuilding did not help and needed to re-add the project to eclipse. See here. I'm sure there is a way to force complete rebuild of R.java also from command line, but i never looked for that.
This had happened to me a few times. Changing a simple thing in an xml layout and saving solved the issue. Thanks.
1

If it's an outdated R class and your Eclipse is not updating it, then just create a new XML layout file with the same contents as the previous one. Then delete the previous layout file. Your R class should then be updated. I have this problem frequently on Linux, and this is the solution that works for me.

Choosing Project -> Clean does NOT solve my problem on Linux because the R class doesn't get recreated...

Comments

0

Have you checked you got the right Button class from the right library imported. Sometimes other libraries can provide their own implementation and Eclipse will choose them over android.widget.Button. Obviously this will cause a crash as your layout will be referring to the button in the android library.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.