Java android.support.v7.widget.AppCompatTextView无法强制转换为android.widget.RelativeLayout
我正在学习Android,但当我到达显示消息部分时,第6点我有一个错误Java android.support.v7.widget.AppCompatTextView无法强制转换为android.widget.RelativeLayout,java,android,android-layout,casting,Java,Android,Android Layout,Casting,我正在学习Android,但当我到达显示消息部分时,第6点我有一个错误 android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.RelativeLayout 我一步一步地做每件事,就像在教程中一样。那为什么它不起作用呢 DisplayMessageActivity.java package com.example.flover.hellloworld; import andr
android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.RelativeLayout
我一步一步地做每件事,就像在教程中一样。那为什么它不起作用呢
DisplayMessageActivity.java
package com.example.flover.hellloworld;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class DisplayMessageActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
Intent intent = getIntent();
String message = intent.getStringExtra(MyActivity.EXTRA_MESSAGE);
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.content);
layout.addView(textView);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_display_message, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
活动显示消息.xml
<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"
tools:context="com.example.flover.hellloworld.DisplayMessageActivity">
<TextView
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
></TextView>
</RelativeLayout>
导致问题的原因。因为您正在定义id为TextView的RelativeLayout
分配
android:id="@+id/content"
至
RelativeLayout
。不要将布局更改为XML中的TextView,id内容应该转到相对视图,而不是TextView
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.flover.hellloworld.DisplayMessageActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
您需要做的就是将Id分配给它应该是什么样的相对布局
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rl_Container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"></TextView>
</RelativeLayout>
对我来说,将android:id添加到根布局容器修复了我的问题。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rl_Container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"></TextView>
</RelativeLayout>
public class DisplayMessageActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
Intent intent = getIntent();
String message = intent.getStringExtra(MyActivity.EXTRA_MESSAGE);
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.rl_Container);
layout.addView(textView);
}
}