简体   繁体   中英

R.java android studio

i am a 13 years boy i am in a course for android development in udacity and im using android studio and there is a problem with this code in the capital R and i dont know why please help this is the java file

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);
}

}

and this is the 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>

and please make a simple answer im not an expert :)

From what I can see you are not inflating layout file in Activity. You have to do so, before you can use findViewById

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.name_of_layout_xml);    
}

Also you have to use R.id.

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

The R file error will come whenever there is any error in any .xml file of your project then R.java will not be generated then it will show error

Check once is there any error in any .xml files

otherwise clean and rebuild your project.

R.java is a file that is automatically generated by Android from your resources, that is, from your xml files and drawables. If there's an error in one of the xml files, the file can't be generated and you get an error. If the file is generated, you'll see an import for it in your source code:

import com.example.android.courtcounter.R

(or whatever the package name of your app)

Go check your xml files one by one, the error should be indicated in the file editor, in text mode. If not, mabye there's an image that you use that's not in the proper directory.

And, of course, what Dalija says, you should have a line with setContentView(..)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM