2015-11-28 50 views
-1

我是一个13岁的男孩,我在udacity中使用android studio进行android开发,并且在大写字母R中存在一个代码问题,我不知道为什么请帮助这个java文件R.java android studio

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> 

,并请一个简单的答案我不是专家:)

+0

线当你问的问题你也不得不说的就是你得到确切的错误错误。它也是编译时间或运行时错误。 –

+0

“@ + id/team_a_score”的'R.id.team_a_score' - 在'R'中有很多不同的东西,它们在不同的类别中。 'R.id','R.string',... – zapl

回答

0

R档误差会来,每当有错误在任何.xml文件中你的项目,然后R.java不会产生那么它会显示错误

检查一次是否有任何.xml文件

否则清洁和重建项目中的任何错误。

2

从我可以看到你没有在Activity中扩充布局文件。你必须这样做,你可以使用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); 
0

R.java是自动的Android从资源生成的文件,也就是说,从你的xml文件和drawables。如果其中一个xml文件有错误,则无法生成该文件,并且出现错误。如果生成的文件,你会看到它的进口在源代码:

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

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

,当然,Dalija说的话,你应该有setContentView(..)