Java android.support.v7.widget.AppCompatTextView 无法转换为 android.widget.RelativeLayout

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34436886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 15:39:50  来源:igfitidea点击:

android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.RelativeLayout

javaandroidandroid-layoutcasting

提问by user2443414

I am learning Android with http://developer.android.com/index.html, but when I reach http://developer.android.com/training/basics/firstapp/starting-activity.htmlDisplay the Message section, point 6 I have an error

我正在使用http://developer.android.com/index.html学习 Android ,但是当我到达http://developer.android.com/training/basics/firstapp/starting-activity.html显示消息部分时,点6 我有错误

android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.RelativeLayout 

I did everything step by step like it was in tutorial. So why it doesn't work?

我一步一步地做所有事情,就像在教程中一样。那么为什么它不起作用呢?

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);
    }
}

activity_display_message.xml

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

采纳答案by Umer Kiani

All you need to do is Assign Id to Relative Layout it should be like

您需要做的就是将 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>

then just make a slight modification to your activity

然后对您的活动稍作修改

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);
}


}

回答by Bhargav Thanki

RelativeLayout layout = (RelativeLayout) findViewById(R.id.content);

causing the problem.Because You are defining RelativeLayout with id of TextView.

导致问题。因为您正在定义具有 TextView id 的 RelativeLayout。

Assign

分配

android:id="@+id/content"

to RelativeLayout. Not to TextViewin XML

RelativeLayout。不在TextViewXML 中

回答by Malek Hijazi

Change your layout to this, the id content should go to the RelativeLayout and not the TextView.

将您的布​​局更改为此,id 内容应该转到RelativeLayout 而不是 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>