2

bI have the following java code:

    Intent intent = getIntent();

    CO2 = intent.getIntExtra("CO2", 0);     

    TextView textCo2 = (TextView)findViewById(R.id.textRoadView1);
    textCo2.setText(String.valueOf("CO2 emissions: "+CO2+"g"));

And the XML file:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/textRoadView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".RoadDetails" >

    <TextView
    android:id="@+id/textRoadView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textView2"
    android:layout_marginTop="38dp"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" />
    </RelativeLayout>

So when I try to set some text in the "textCo2" I get this weird exception "Android widget relativelayout cannot be cast to android widget textview." I tried removing the "R.java" file and cleaning the project but the problem continued. I think the mistake is somewhere in my code but I can find it. Could you help me out?

1
  • never use an ID twice for different xml elements Commented Feb 10, 2014 at 8:22

1 Answer 1

8

Your id is duplicate, remove if from your RelativeLayout, like this:

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".RoadDetails" >

    <TextView
    android:id="@+id/textRoadView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/textView2"
    android:layout_marginTop="38dp"
    android:text="Medium Text"
    android:textAppearance="?android:attr/textAppearanceMedium" />
    </RelativeLayout>
Sign up to request clarification or add additional context in comments.

2 Comments

Dear GOD THANK YOU SO MUCH!!! I knew it was something small and stupid just I could not find it out! Thank you again!
@user3182266, Please accept the answer if it helped you. see the link that how to accept answer if you don't know how to accept it. meta.stackexchange.com/questions/5234/…

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.