Android小部件relativelayout无法强制转换为android小部件textview

时间:2014-02-10 08:16:59

标签: java android android-layout textview

b我有以下java代码:

    Intent intent = getIntent();

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

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

XML文件:

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

因此,当我尝试在“textCo2”中设置一些文本时,我得到了这个奇怪的异常“Android widget relativelayout无法转换为android小部件textview。”我尝试删除“R.java”文件并清理项目,但问题仍在继续。我认为错误在我的代码中的某处,但我可以找到它。你能救我吗?

1 个答案:

答案 0 :(得分:6)

您的ID是重复的,请从RelativeLayout删除,如下所示:

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