有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java 安卓。支持v7。小装置。AppCompatTextView无法强制转换为安卓。小装置。相对布局

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

安卓.support.v7.widget.AppCompatTextView cannot be cast to 安卓.widget.RelativeLayout 

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

DisplayMessageActivity.java

    package com.example.flover.hellloworld;

    import 安卓.content.Intent;
    import 安卓.os.Bundle;
    import 安卓.support.v7.app.AppCompatActivity;
    import 安卓.view.Menu;
    import 安卓.view.MenuItem; 
    import 安卓.widget.RelativeLayout;
    import 安卓.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:安卓="http://schemas.安卓.com/apk/res/安卓"
    xmlns:tools="http://schemas.安卓.com/tools"
    安卓:layout_width="match_parent"
    安卓:layout_height="match_parent"
    tools:context="com.example.flover.hellloworld.DisplayMessageActivity">

    <TextView
        安卓:id="@+id/content"
        安卓:layout_width="wrap_content"
        安卓:layout_height="wrap_content"
        ></TextView>

</RelativeLayout>

共 (1) 个答案

  1. # 1 楼答案

    你所需要做的就是将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);
    }
    
    
    }