Java 在 Android Studio 教程中找不到符号错误?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33028675/
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
Cannot find symbol errors in Android Studio Tutorial?
提问by MrPickles7
https://developer.android.com/training/basics/firstapp/building-ui.html#Weight
https://developer.android.com/training/basics/firstapp/building-ui.html#Weight
I am doing the tutorial above and to get it to build I had to comment out these lines inside MainActivity.java inside src folder (This code is in the MainActivity class inside OnCreate()).
我正在执行上面的教程并要构建它,我必须在 src 文件夹内的 MainActivity.java 中注释掉这些行(此代码位于 OnCreate() 内的 MainActivity 类中)。
// Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
// setSupportActionBar(toolbar);
//
// FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
// fab.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View view) {
// Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
// .setAction("Action", null).show();
// }
// });
If I do not comment out these lines, I get 'cannot find' errors and can't build, example:
如果我不注释掉这些行,我会收到“找不到”错误并且无法构建,例如:
error: cannot find symbol variable toolbar
错误:找不到符号变量工具栏
Can someone explain in plain english why this is happening and how I can fix it? I have tried various import.R fixes that people found to combat Eclipse randomly adding that, but they don't work. My imports:
有人可以用简单的英语解释为什么会发生这种情况以及我如何解决它?我尝试了各种 import.R 修复,人们发现这些修复可以与 Eclipse 随机添加,但它们不起作用。我的进口:
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
I am using Android Studio like the tutorial says and get the same error on a clean install. Is it my automatically generated imports? Is the tutorial wrong or incompatible with the latest Android Studio? Are my build settings wrong?
我正在像教程所说的那样使用 Android Studio,并在全新安装时遇到相同的错误。是我自动生成的导入吗?教程是否错误或与最新的 Android Studio 不兼容?我的构建设置有误吗?
采纳答案by jyanks
The R
class is auto-generated based on what is in your *.xml
files, whether they are strings, dimensions, colors, ids within layouts, etc.
该R
班是自动生成基于什么是你*.xml
的文件,无论是字符串,尺寸,颜色,布局内的ID等
When you are calling code like this:
当您像这样调用代码时:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
You are saying, "Set the toolbar variable to the widget with an id of toolbar
".
您是说,“将工具栏变量设置为 ID 为toolbar
“的小部件。
In an xml, say you had three textviews like this:
在 xml 中,假设您有三个这样的文本视图:
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text_view_1" />
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text_view_2" />
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/text_view_3" />
When I build the project, my R.java
file will then be updated to have references in java code to those ids that I defined in xml, like this:
当我构建项目时,我的R.java
文件将被更新以在 java 代码中引用我在 xml 中定义的那些 id,如下所示:
public static final int text_view_1=0x7f0c0066;
public static final int text_view_2=0x7f0c0067;
public static final int text_view_3=0x7f0c0068;
To explain it at a very basic level, the R.java
file is java code that is generated to reference xml elements within actual java code.
为了从一个非常基本的层面解释它,该R.java
文件是生成的 java 代码,用于在实际 java 代码中引用 xml 元素。
I am guessing that when you created a project in Android Studio, it came with the MainActivity.java
class. That's where that code is coming from that you posted above. The part right above that in the default MainActivity.java
class is
我猜当你在 Android Studio 中创建一个项目时,它附带了MainActivity.java
类。那就是您在上面发布的代码的来源。默认MainActivity.java
类中的正上方部分是
setContentView(R.layout.activity_main);
This is setting the activity's view to the xml layout file defined in activity_main.xml. Inside of this layout is the following
这是将活动的视图设置为activity_main.xml 中定义的xml 布局文件。在这个布局里面是以下内容
activity_main.xml
活动_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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" android:fitsSystemWindows="true"
tools:context=".MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="@style/AppTheme.AppBarOverlay"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_main" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
My guess is that perhaps your activity_main.xml does not contain this definition for the Toolbar
and the FloatingActionButton
widget.
我的猜测是,您的 activity_main.xml 可能不包含Toolbar
和FloatingActionButton
小部件的此定义。
If you want to really start from scratch so that you can follow the tutorial you linked, you should delete the MainActivity.java
and just go create your own.
如果您想真正从头开始,以便可以按照链接的教程进行操作,则应该删除MainActivity.java
并创建自己的。
回答by SW_IR
Run>Clean and Rerun'app' or ctrl+F5 on Android Studio
在 Android Studio 上运行 > Clean and Rerun'app' 或 ctrl+F5
回答by Jake TheSnake Roberts
I know this is old, but I'm going to answer because this tutorial is still the best for android studios, and it poorly explains this part.
我知道这是旧的,但我要回答,因为本教程仍然是 android 工作室的最佳选择,并且它对这部分的解释很差。
OnCreate() creates the elements you define in XML in Java, by creating the default activity, you still have a
OnCreate() 创建你在 Java 中用 XML 定义的元素,通过创建默认活动,你仍然有一个
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
and a
和一个
fab.setOnClickListener(new View.OnClickListener()
defined in OnCreate, android studios as not created these resources in android.R because you deleted there definitions in XML as part of the tutorial!
在 OnCreate 中定义,android studios 没有在 android.R 中创建这些资源,因为您在教程中删除了 XML 中的定义!