Java 无法将Android widget relativelayout强制转换为Android widget textview
bI具有以下java代码:Java 无法将Android widget relativelayout强制转换为Android widget textview,java,android,android-layout,textview,Java,Android,Android Layout,Textview,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")); 和XML文件: <Relative
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 widget textview”。我尝试删除“R.java”文件并清理项目,但问题仍然存在。我想错误在我的代码中的某个地方,但我能找到它。你能帮我吗 您的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>
您的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>
绝不能对不同的xml元素使用两次ID绝不能对不同的xml元素使用两次ID尊敬的上帝,非常感谢!!!我知道这是一件很小很愚蠢的事情,只是我找不到它!再次感谢你@用户3182266,如果答案对您有帮助,请接受。如果您不知道如何接受答案,请查看如何接受答案的链接。亲爱的上帝,非常感谢你!!!我知道这是一件很小很愚蠢的事情,只是我找不到它!再次感谢你@用户3182266,如果答案对您有帮助,请接受。如果您不知道如何接受答案,请查看如何接受答案的链接。