R.javaandroid工作室

R.javaandroid工作室,java,android,Java,Android,我是一个13岁的男孩,我正在udacity学习android开发课程,我正在使用android studio,这段代码在大写字母R中有一个问题,我不知道为什么请帮忙这是java文件 package com.example.android.courtcounter; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.TextView; class Mai

我是一个13岁的男孩,我正在udacity学习android开发课程,我正在使用android studio,这段代码在大写字母R中有一个问题,我不知道为什么请帮忙这是java文件

package com.example.android.courtcounter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

class MainActivity extends AppCompatActivity {
int score = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

}
/**
 * Displays the given score for Team A.
 */
public void displayForTeamA(int score) {
    TextView scoreView;
    scoreView = (TextView) findViewById(R.team_a_score);this is the problem
    scoreView.setText(String.valueOf(score));
}
public void threeScore (int score) {
    displayForTeamA(score + 3);
}
public void twoScore (int score) {
    displayForTeamA(score + 2);
}

public void freeThrow (int score) {
    displayForTeamA(score + 1);
}

}
这是xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Team A"
    android:textSize="16sp"
    android:gravity="center_horizontal"
    android:padding="4dp"/>

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="0"
    android:id="@+id/team_a_score"
    android:textSize="16sp"
    android:gravity="center_horizontal"
    android:padding="4dp"/>
<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="+3 Points"
    android:layout_margin="8dp"/>
<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="+2 Points"
    android:layout_margin="8dp"/>
<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Free Throw"
    android:layout_margin="8dp"/>
 </LinearLayout>


请简单回答我不是专家:)

只要项目的任何.xml文件中有任何错误,R.java将不会生成,然后它将显示错误

检查一下.xml文件中是否有任何错误


否则,请清理并重建您的项目。

从我看到的情况来看,您并没有在活动中膨胀布局文件。在使用
findViewById

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.name_of_layout_xml);    
}
您还必须使用
R.id.

scoreView = (TextView) findViewById(R.id.team_a_score);

java是由Android从您的资源(即xml文件和绘图表)自动生成的文件。如果其中一个xml文件中存在错误,则无法生成该文件,您将得到一个错误。如果生成了该文件,您将在源代码中看到该文件的导入:

import com.example.android.courtcounter.R

(或应用程序包的任何名称)

逐个检查xml文件,错误应该在文件编辑器中以文本模式指示。如果没有,mabye您使用的图像不在正确的目录中


当然,正如Dalija所说,当你问关于错误的问题时,你应该用
setContentView(..)

来说明你得到的确切错误是什么。这也是编译时或运行时错误。
@+id/team\u a\u score
R.id.team\u a\u score
-在
R
中有许多不同的东西,它们是分类的
R.id
R.string
。。。