java 教程上的android错误找不到符号变量activity_display_message
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38758391/
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
android error on tutorial cannot find symbol variable activity_display_message
提问by ryan.pinto
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
我正在尝试通过教程和 Android Studio 学习 android 应用程序构建。一些关于 xml 和导入的评论很有帮助。我犯了一个错误。我收到此错误 Error:(22, 57) 错误:找不到符号变量 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
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
构建.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'
}
回答by Joey
I had this same problem. Under the "Build an Intent" section in the Android Studio instructions, it's easy to miss this line:
我有同样的问题。在 Android Studio 说明中的“Build an Intent”部分下,很容易错过这一行:
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
Make sure this is included above the @Override in MainActivity.java and you're all set :)
确保它包含在 MainActivity.java 中的 @Override 之上,并且你已经准备好了:)
回答by AlgoRythm
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.
注意:以前版本的 Android Studio 生成的 XML 布局可能不包含 android:id 属性。如果布局没有 android:id 属性,则调用 findViewById() 将失败。如果是这种情况,请打开 activity_display_message.xml 并将属性 android:id="@+id/activity_display_message" 添加到布局元素。
So, under Res -> layout -> activity_display_message.xml, I entered the line
所以,在 Res -> layout -> activity_display_message.xml 下,我输入了这一行
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
RelativeLayout 标签内的任何位置。特别是在我的情况下,我随机将它推到了 android:paddingTop 和 tools:context 字段之间
Hope this helps :D
希望这会有所帮助:D
回答by MrVietnamese
I think:
我认为:
ViewGroup layout = (ViewGroup) findViewById(R.id.activity_display_message);
Should be:
应该:
RelativeLayout layout = (RelativeLayout ) findViewById(R.id.content);
回答by Vinay Kumar P
Here is the updated solution for this.
这是更新的解决方案。
Intent is a subclass of Main Activity so there's no need to specify explicitly. That means we can call EXTRA_MESSAGE
, instead of MainActivity.
Intent 是 Main Activity 的子类,因此无需明确指定。这意味着我们可以调用EXTRA_MESSAGE
, 而不是MainActivity.
EXTRA_MESSAGE
EXTRA_MESSAGE
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
Intent intent = getIntent();
String message = intent.getStringExtra(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);
回答by akinoreiki
I've had this problem so many times trying to do this tutorial, and to add to the only answer here that helped me, which was deleting the MainActivity.
from the MainActivity.EXTRA_MESSAGE
portion...
我在尝试完成本教程时遇到了这个问题很多次,并在此处添加了对我有帮助的唯一答案,即MainActivity.
从MainActivity.EXTRA_MESSAGE
部分中删除了...
I also had to import the following in DisplayMessageActivity.java
:
我还必须导入以下内容DisplayMessageActivity.java
:
import static android.provider.AlarmClock.EXTRA_MESSAGE;
import static android.provider.AlarmClock.EXTRA_MESSAGE;
This completely solved all of my errors and allowed me to continue the tutorial.
这完全解决了我的所有错误,并允许我继续本教程。
回答by OneCricketeer
"@layout/activity_display_message"
It is a layout. Not an ID.
它是一种布局。不是身。
You correctly used it here as R.layout
你在这里正确地使用它作为 R.layout
setContentView(R.layout.activity_display_message);
But this is wrong as R.id
但这是错误的 R.id
ViewGroup layout = (ViewGroup) findViewById(R.id.activity_display_message);
layout.addView(textView);
The corrected method should ideally look like this, but note that you might want some LayoutParams on the TextView, otherwise it might not display on the screen.
更正后的方法最好看起来像这样,但请注意,您可能需要在 TextView 上使用一些 LayoutParams,否则它可能不会显示在屏幕上。
@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.content);
layout.addView(textView);
}
回答by Divakar Joshi
I have solved the same issue by clean project and then Build the gradle, please try once.
我已经通过干净的项目解决了同样的问题,然后构建了 gradle,请尝试一次。
回答by Josh
The error comes from the MainActivity.java file.
错误来自 MainActivity.java 文件。
TextView tv = (TextView) findViewById(R.id.editText);
Where I have (R.id.editText);.....the system put (R.id.edit_message);
我在哪里 (R.id.editText);..... 系统放置 (R.id.edit_message);
edit_message is the function added to the text box but it is under the (EditText) "block" of code. New to Java, not sure what the block of code is called and not sure why the program put that in the public class. Hope this helps.
edit_message 是添加到文本框的函数,但它位于 (EditText) 代码块下。Java 新手,不确定代码块是什么,也不知道为什么程序把它放在公共类中。希望这可以帮助。
package com.example.josh.myfirstapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Example of a call to a native method
TextView tv = (TextView) findViewById(R.id.editText);
tv.setText(stringFromJNI());
}
/**
* A native method that is implemented by the 'native-lib' native library,
* which is packaged with this application.
*/
public native String stringFromJNI();
// Used to load the 'native-lib' library on application startup.
static {
System.loadLibrary("native-lib");
}
}
回答by Galo Jerez Sandoval
I had the same error message: cannot find symbol variable activity_display_message
我有同样的错误信息: 找不到符号变量 activity_display_message
I solved it by adding android:idin activity_display_message.xml
我通过在 activity_display_message.xml 中添加android:id解决了它
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.galo.myapplication.DisplayMessageActivity"
android:id="@+id/activity_display_message">
</android.support.constraint.ConstraintLayout>
回答by Ira Zakharchenko
Check if you have code like this in MainActivity.java
:
检查您是否有这样的代码MainActivity.java
:
public class MainActivity extends AppCompatActivity {
public static final String EXTRA_MESSAGE = "com.example.MyApplicationstart.MESSAGE";
"com.example.MyApplicationstart.MESSAGE", where "MyApplicationstart" is a name of your project.
“com.example.MyApplicationstart.MESSAGE”,其中“MyApplicationstart”是您的项目名称。