问题描述
我试图通过教程和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_horizontal_margin
android:paddingRight =@ dimen / activity_horizontal_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'com.android.support:appcompat-v7:23.4.0'
}
不知道是否还有人需要这个答案,但我想通了像这样:
我在教程中看到了这个:
因此,在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:
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!