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

use*_*266 0 java android textview android-layout

bI有以下java代码:

    Intent intent = getIntent();

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

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

和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>
Run Code Online (Sandbox Code Playgroud)

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

Dam*_* R. 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>
Run Code Online (Sandbox Code Playgroud)

  • @ user3182266,如果有帮助,请接受答案。如果您不知道如何接受答案,请参见如何接受答案的链接。http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work (2认同)