2

I'm trying to get the instance of a ImageView defined in an XML layout file:

<RelativeLayout
    android:id="@+id/visual_compass_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/output_container" >

    <ImageView
        android:id="@+id/visual_compass"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:contentDescription="Compass"
        android:scaleType="fitStart"
        android:src="@drawable/compass_rose" />

</RelativeLayout>

But I always get that runtime error:

... java.lang.ClassCastException: android.widget.RelativeLayout cannot be
cast to android.widget.ImageView ... at net.example.MainActivity.onCreate
(MainActivity.java:57)

Line "57" is that one:

compassView = (ImageView) findViewById(R.id.visual_compass);

I can't figure out why that error ist thrown; I don't see the problem with that piece of code. Does anybody elso know?

Thank you, Felix D.

6
  • 4
    have you tried to clean and rebuild your project? Commented Jul 16, 2013 at 23:14
  • What do you mean exactly? I'm working with Eclipse: so should i create a new peoject and copy all modified files from the old ont to it? Commented Jul 16, 2013 at 23:17
  • 1
    no, there is an option in Eclipse - clean, it would delete all generated files and then you have to build again, which will regenerate them. (Project -> Clean, if you didn't know) Commented Jul 16, 2013 at 23:18
  • 1
    Are you sure, that you didn't accidentally write RelativeLayout compassView;[...]? If not, then cleaning the project will probably help like others already suggested. Commented Jul 16, 2013 at 23:23
  • Ok, thank you for your quick comments. I'll try cleaning the project. And post my results here. @Ahmad And yes, I'm sure. Commented Jul 16, 2013 at 23:43

1 Answer 1

6

Make sure in your code the compassView is ImageView variable only and try to access it as

  ImageView compassView = (ImageView) findViewById(R.id.visual_compass); 

And if everything is correct then try to clean and Build your project again from the menu Project-> clean. and Proejct-> Build.

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

1 Comment

OK! that helped. Tahnk you. But I think the Project is rebuild automatically after being cleaned. And Eclipse automatically checks wether the cast ist correct...

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.