2014-02-27 16 views
0

錯誤在於,R是加下劃線的紅色!和com.example.guessthenumber.R也強調紅色! 當盤旋它說來創建包com.example.guessthenumber.R一個R級我發現我的R.java沒有包含任何內容,有人能告訴我在R.java中寫什麼

xml文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="@drawable/Guess" 
    android:gravity="start" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity" > 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:text="Done!" /> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/imageView1" 
     android:layout_centerHorizontal="true" 
     android:text="Enter your guessed number below" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:textColor="#191970" 
     android:textStyle="bold" /> 

    <EditText 
     android:id="@+id/editText1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/textView1" 
     android:layout_centerVertical="true" 
     android:text="0" 
     android:inputType="number" /> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/button1" 
     android:layout_below="@+id/editText1" 
     android:layout_marginTop="29dp" 
     android:fontFamily="sans-serif-condensed" 
     android:text="@string/guess" 
     android:textColor="#000000" 
     android:textSize="22sp" 
     android:textStyle="bold" /> 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentTop="true" 
     android:src="@drawable/game" /> 

</RelativeLayout> 

下面一個是java文件:

package com.example.guessthenumber; 

import android.os.Bundle; 
import com.example.guessthenumber.R 
import android.app.Activity; 
import android.view.Menu; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     final TextView t1 = (TextView) findViewById(R.id.textView1); 
     final TextView guessText = (TextView) findViewById(R.id.textView2); 
     final EditText userGuess = (EditText) findViewById(R.id.editText1); 

     Button b1 = (Button) findViewById(R.id.button1); 

     final String[] myNames={"Shashank","Sarthak","Gowda","$hankar"}; 


     b1.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       String randText=""; 
       // TODO Auto-generated method stub 

       int rando = (int) (Math.random()* 6); 
       int userNumber = Integer.parseInt(userGuess.getText().toString()); 

       if(userNumber == rando){ 
        guessText.setText("You got it right!"); 
       }else{ 
        guessText.setText("Guess again! :("); 
       } 

       randText=Integer.toString(rando); 
       t1.setText(randText); 

       userGuess.setText("0"); 
      } 
     }); 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

} 
+1

嘗試重建您的項目 – RMachnik

+1

您的佈局文件存在問題。找到它,修復它,並最終做一個項目 - >清潔 – gunar

+1

張貼您的'TextView' XML –

回答

1

替換:
import android.R; 來自

import com.example.guessthenumber.R 

刪除import.android.R並使用ctrl + shift + O,如果使用eclipse,則組織導入。

清潔,建立你的項目。

另外,請閱讀更多關於R:
Android: What is R? Why is it so Cryptic?

檢查進口 - 在您的活動的上方,下方的包名。並確保它有這樣的一行:

import your_application_package_name.R; 

同意上述Gúnár酒店的評論,

在XML中,「ID」沒有正確定義,爲您的TextView。請發佈XML代碼,也可能是活動的。

<TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 
+0

現在我已經編輯爲你說的,@ + id/textView1我會發布xml和java下面 – user3351630

+0

請發佈您的活動代碼 – user2450263

+0
0

檢查您從包導入R類,而不是Android的一個:在TextView的「ID」,XML

正確方法。還請檢查您的resources是否包含錯誤,否則將不會生成R類。

+0

先生,該怎麼做? – user3351630

+0

我發現我的R.java沒有包含任何東西! – user3351630

相關問題