2013-07-04 145 views
1

我是Android新手。我正在開發一個簡單的註冊登錄系統。在每個類中,每個R發生的錯誤都很多。錯誤是R無法解析爲變量。其中一個類代碼在這裏R無法解析到變量

這裏是我的代碼

package com.UserAccounts; 

import android.app.Activity; 

import android.os.Bundle; 

import android.view.View; 

import android.widget.Button; 

import android.widget.EditText; 

import android.widget.Toast; 

public class SignUPActivity extends Activity 
{ 
    EditText editTextUserName,editTextPassword,editTextConfirmPassword; 
    Button btnCreateAccount; 

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

     // get Instance of Database Adapter 
     loginDataBaseAdapter=new LoginDataBaseAdapter(this); 
     loginDataBaseAdapter=loginDataBaseAdapter.open(); 

     // Get Refferences of Views 
     editTextUserName=(EditText)findViewById(R.id.editTextUserName); 
     editTextPassword=(EditText)findViewById(R.id.editTextPassword); 
     editTextConfirmPassword=(EditText)findViewById(R.id.editTextConfirmPassword); 

     btnCreateAccount=(Button)findViewById(R.id.buttonCreateAccount); 
     btnCreateAccount.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 

      String userName=editTextUserName.getText().toString(); 
      String password=editTextPassword.getText().toString(); 
      String confirmPassword=editTextConfirmPassword.getText().toString(); 

      // check if any of the fields are vaccant 
      if(userName.equals("")||password.equals("")||confirmPassword.equals("")) 
      { 
        Toast.makeText(getApplicationContext(), "Field Vaccant", Toast.LENGTH_LONG).show(); 
        return; 
      } 
      // check if both password matches 
      if(!password.equals(confirmPassword)) 
      { 
       Toast.makeText(getApplicationContext(), "Password does not match", Toast.LENGTH_LONG).show(); 
       return; 
      } 
      else 
      { 
       // Save the Data in Database 
       loginDataBaseAdapter.insertEntry(userName, password); 
       Toast.makeText(getApplicationContext(), "Account Successfully Created ", Toast.LENGTH_LONG).show(); 
      } 
     } 
    }); 
} 
    @Override 
    protected void onDestroy() { 
     // TODO Auto-generated method stub 
     super.onDestroy(); 

     loginDataBaseAdapter.close(); 
    } 
} 
+0

可能重複的[R無法解析 - Android錯誤](http://stackoverflow.com/questions/885009/r-cannot-be-resolved-android-error) – Simon

回答

2

如果您的課程與聲明的應用程序包不在同一個包中清單,你需要導入你的應用程序的類R。該形式應該是

import your_app_package_name_as_defined_in_the_manifest.R; 

請注意,如果你在任何項目的資源文件有錯誤,也不會產生R.java文件和R類將不存在。如果是這種情況,請解決資源文件錯誤,並且未定義的類錯誤將會消失。

+0

謝謝你的友好解釋 – user2502561

0

您需要導入yourpackage.R則錯誤得到解決......我認爲它會像進口com.UserAccounts一些事情。 R

1

檢查你沒有在你的res文件夾的錯誤,然後進行清理你的項目(項目 - >清潔)

0

第一我是「進口your_app_package_name_as_defined_in_the_manifest.R;」之後我按下Project - > Clean,然後完成。謝謝!

0

將我自己的資源文件導入到我自己的包中是我想要做的最後一件事。你不應該 訴諸這個國際海事組織! (但是,我已經訴諸於此!Hypocrite!)

我有這個問題無數次,它是由我的XML文件相關的不同事情引起的。

檢查: *您是否在任何onCreatView()方法中調用了Inflator.inflate ... R.id.my_resource.xml中的拼寫錯誤。

*請看佈局編輯器中的'設計'選項卡。檢查他們都指向相同的Android版本。像23等。

*在佈局編輯器中檢查您正在使用的主題。

*有時,您可能在xml中沒有指向包含其他xml文件的Include標籤。 *請繼續關注xml和充氣器,有時給定的佈局似乎沒有 問題,用紅線表示。

*如果仍然無法擺脫問題,請在其他資源文件中查找拼寫錯誤和錯誤。

*您可能還必須在每次更改時運行「清潔」。

*休息一下,稍後再回來。