簡體   English   中英

R.java Android Studio

[英]R.java android studio

我是一個13歲的男孩,我正在學習使用udacity和im使用android studio進行android開發的課程,並且該代碼在大寫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>

請給一個簡單的答案,我不是專家:)

從我可以看到,您不是在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);

每當您的項目的任何.xml文件中存在任何錯誤時,R文件錯​​誤都會出現,然后不會生成R.java,然后它將顯示錯誤

檢查一次.xml文件中是否有錯誤

否則請清理並重建您的項目。

R.java是Android會根據您的資源(即xml文件和可繪制對象)自動生成的文件。 如果其中一個xml文件存在錯誤,則無法生成該文件,並且會出現錯誤。 如果生成了文件,您將在源代碼中看到該文件的導入:

import com.example.android.courtcounter.R

(或您應用的任何軟件包名稱)

逐一檢查您的xml文件,該錯誤應在文本模式下的文件編輯器中指出。 如果不是,請說,您使用的圖像不在正確的目錄中。

而且,當然,如Dalija所說的那樣,您應該使用setContentView(..)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM