教程中的android错误找不到符号变量activity_display_message [英] android error on tutorial cannot find symbol variable activity_display_message

查看:1781
本文介绍了教程中的android错误找不到符号变量activity_display_message的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过教程和Android Studio学习android应用程序构建。有关xml和导入的一些评论是有帮助的。我陷入了一个错误。我得到这个错误错误:(22,57)错误:无法找到符号变量activity_display_message



有关导入的错误我已经修复了一些关于堆栈流的搜索。我错过了什么



DisplayMessageActivity

  package com.example.rpinto .myfirstapp; 

导入android.content.Intent;
导入android.support.v7.app.AppCompatActivity;
导入android.os.Bundle;
导入android.view.ViewGroup;
导入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.0encoding =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_horizo​​ntal_margin
android:paddingRight =@ dimen / activity_horizo​​ntal_margin
android:paddingTop =@ dimen / activity_vertical_margin
tools:context =com.example.rpinto.myfirstapp.DisplayMessageActivity
工具:showIn =@ layout / activity_display_message
android:id =@ + id / content>
< / RelativeLayout>

build.gradle

  apply plugin:'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion24.0.1

defaultConfig {
applicationIdcom.example.rpinto.myfirstapp
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName1.0
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'
}
}
$ b $ dependency {
compile fileTree(dir:'libs',include:['* .jar'])
testCompile'junit:junit:4.12'
compile'c​​om.android.support:appcompat-v7:23.4.0'
}


解决方案

不知道是否还有人需要这个答案,但我想通了像这样:

我在教程中看到了这个:


注意:以前版本的Android Studio生成的XML布局可能不包含android:id属性。调用findViewById()将失败,如果布局没有android:id属性。如果是这种情况,请打开activity_display_message.xml并将属性android:id =@ + id / activity_display_message添加到布局元素。




因此,在Res - > layout - > activity_display_message.xml下,我输入了行:

  android:id =@ + id / activity_display_message

RelativeLayout标签内的任何位置。在我的例子中,我随机推它在android:paddingTop和tools:context字段之间

希望这有助于:D


I am trying to learn android app building through tutorials and Android Studio. Some of the comments regarding xml and imports were helpfult. I am down to one error. I get this error Error:(22, 57) error: cannot find symbol variable activity_display_message

The errors regarding imports I have fixed with some searching on stack flow. I am missing something

DisplayMessageActivity

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>

build.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'
}

解决方案

Don't know if anyone still need the answer to this, but I figured it out like this:

I saw this in the tutorial:

Note: The XML layout generated by previous versions of Android Studio might not include the android:id attribute. The call findViewById() will fail if the layout does not have the android:id attribute. If this is the case, open activity_display_message.xml and add the attribute android:id="@+id/activity_display_message" to the layout element.

So, under Res -> layout -> activity_display_message.xml, I entered the line

android:id="@+id/activity_display_message"

Anywhere inside the RelativeLayout tags. In my case specifically, I randomly shoved it in-between the android:paddingTop and tools:context fields

Hope this helps :D

这篇关于教程中的android错误找不到符号变量activity_display_message的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆