java - 教程上的android错误找不到符号变量activity_display_message

标签 java android xml gradle

我正在尝试通过教程和 Android Studio 学习 Android 应用程序构建。一些关于 xml 和导入的评论很有帮助。我只犯了一个错误。我收到此错误 Error:(22, 57) error: cannot find symbol variable activity_display_message

关于导入的错误我已经通过对堆栈流的一些搜索进行了修复。我错过了一些东西

显示消息 Activity

package com.example.rpinto.myfirstapp;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ViewGroup;
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(MainActivity.EXTRA_MESSAGE);
        TextView textView = new TextView(this);
        textView.setTextSize(40);
        textView.setText(message);

        ViewGroup layout = (ViewGroup) findViewById(R.id.activity_display_message);
        layout.addView(textView);
    }
}

activity_display_message.xml

<?xml version="1.0" encoding="utf-8"?>
<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="com.example.rpinto.myfirstapp.DisplayMessageActivity"
    tools:showIn="@layout/activity_display_message"
    android:id="@+id/content">
</RelativeLayout>

构建.gradle

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "24.0.1"

    defaultConfig {
        applicationId "com.example.rpinto.myfirstapp"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.4.0'
}

最佳答案

我遇到了同样的问题。在 Android Studio 说明的“Build an Intent”部分下,很容易错过这一行:

public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";

确保它包含在 MainActivity.java 中的@Override 之上,并且一切就绪:)

关于java - 教程上的android错误找不到符号变量activity_display_message,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38758391/

相关文章:

xml - XML over Sockets 是好的还是坏的做法?

java - 如何通过快速排序算法实现避免堆栈溢出错误

android - 通过 urllib 或 urllib2 在 Python 中 curl 请求

Java + ResultSet.getObject()

android - Cordova/Phonegap Android 目标 : not installed [Android Studio Installed]

android - 尝试将 fragment 覆盖在现有布局上

ruby /生成器 API : create XML without using blocks

java - 清除 Swing GUI 中的 ImageIcon 和相关操作

java - 多边形旋转时会缩小

java - ActionListener 在 Java 中不工作