2014-02-22 38 views
3

我正在從一個月的Android項目工作。我在64位Windows 8 PC上運行sdk。我一直遇到錯誤,指出「R無法解析爲變量」。我已經嘗試了幾種在網上發佈的解決方案,其中一些是:R無法解析爲變量[無法解決與論壇中給出的解決方案]

  • 清理項目並重新構建。

  • 刪除從XML文件中的錯誤(在我的項目沒有觀察到這樣的錯誤)包名稱(這是不是在所有在我的情況下完成)

  • 進口android.R的

  • 變化(這不是在我的項目進口)

  • 在SDK安裝最新的更新(所有已安裝)

這個錯誤大多發生在我清理項目或構建它時。我一直在做的項目一直運行到昨天,直到我清理了項目。建設該項目也沒有解決問題。在我測試從android到php傳遞值的代碼中存儲數據到mysql中的意思。這是我從網站上獲得的測試代碼。此代碼也顯示相同的錯誤。

我的項目的到期日期即將到期,我不知道如何解決此錯誤。

代碼:

MainActivity.java

package com.example.sdvd; 
import java.io.BufferedReader; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.util.ArrayList; 
import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.NameValuePair; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.message.BasicNameValuePair; 
import org.json.JSONObject; 
import android.os.Bundle; 
import android.app.Activity; 
import android.util.Log; 
import android.view.Menu; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

public class MainActivity extends Activity { 

    String name; 
    String id; 
    InputStream is=null; 
    String result=null; 
    String line=null; 
    int code; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     //final EditText eid=(EditText) findViewById(R.id.e1); 
     final EditText e_id=(EditText) findViewById(R.id.e1); 
     final EditText e_name=(EditText) findViewById(R.id.editText2); 

     Button insert=(Button) findViewById(R.id.button1); 
     Toast.makeText(this, "Yo", Toast.LENGTH_SHORT); 
     insert.setOnClickListener(new View.OnClickListener() { 

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

       id = e_id.getText().toString(); 
       name = e_name.getText().toString(); 

       insert(); 
      } 
     }); 
    } 

    public void insert() 
    { 
     ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 

     nameValuePairs.add(new BasicNameValuePair("id",id)); 
     nameValuePairs.add(new BasicNameValuePair("name",name)); 

     try 
     { 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost("http://10.0.2.2/insert.php"); 
      httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
      HttpResponse response = httpclient.execute(httppost); 
      HttpEntity entity = response.getEntity(); 
      is = entity.getContent(); 
      Log.e("pass 1", "connection success "); 
     } 
     catch(Exception e) 
     { 
      Log.e("Fail 1", e.toString()); 
      Toast.makeText(getApplicationContext(), "Invalid IP Address", 
      Toast.LENGTH_LONG).show(); 
     } 

     try 
     { 
      BufferedReader reader = new BufferedReader 
      (new InputStreamReader(is,"iso-8859-1"),8); 
      StringBuilder sb = new StringBuilder(); 
      while ((line = reader.readLine()) != null) 
      { 
       sb.append(line + "\n"); 
      } 
      is.close(); 
      result = sb.toString(); 
      Log.e("pass 2", "connection success "); 
     } 
     catch(Exception e) 
     { 
      Log.e("Fail 2", e.toString()); 
     } 

     try 
     { 
      JSONObject json_data = new JSONObject(result); 
      code=(json_data.getInt("code")); 

      if(code==1) 
      { 
       Toast.makeText(getBaseContext(), "Inserted Successfully", 
       Toast.LENGTH_SHORT).show(); 
      } 
      else 
      { 
       Toast.makeText(getBaseContext(), "Sorry, Try Again", 
       Toast.LENGTH_LONG).show(); 
      } 
     } 
     catch(Exception e) 
     { 
      Log.e("Fail 3", e.toString()); 
     } 
    } 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

} 

activity_main.xml中

<?xml version="1.0" encoding="UTF-8"?> 
<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"> 
    <EditText 
    android:id="@+id/e1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:hint="Id" 
    android:padding="11dp" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="39dp" 
    android:ems="10" 
    android:inputType="number"> 
     <requestFocus /> 
    </EditText> 
    <Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:padding="11dp" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" 
    android:text="Insert" /> 
    <EditText 
    android:id="@+id/editText2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/e1" 
    android:layout_below="@+id/e1" 
    android:layout_marginTop="34dp" 
    android:ems="10" 
    android:hint="Name" 
    android:inputType="textPersonName" 
    android:padding="11dp" /> 
</RelativeLayout> 

艙單

<?xml version="1.0" encoding="UTF-8"?> 
<manifest 
xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.sdvd" 
android:versionCode="1" 
android:versionName="1.0"> 
    <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme"> 
     <activity 
     android:name="com.example.sdvd.MainActivity" 
     android:label="@string/app_name"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 
</manifest> 
+2

清潔項目往往揭示隱藏在XML文件中的錯誤。仔細檢查他們所有,包括字符串,維度,drawables,菜單,首選項,...不僅佈局。通常有一個小的錯誤,不允許R編譯。 –

+1

您是否已經檢查/ res文件夾中的所有文件名? – donfuxx

+1

@ mr.boyfox:**「您需要導入com.example.sdvd.R」** - 不需要導入您自己項目的R類 - 這是隱式完成的。 – Squonk

回答

2

在activity_main.xml中從第一EditText刪除以下兩行...

<requestFocus /> 
</EditText> 

然後改變它的最後一行有結束/如下...

android:inputType="number" /> 

...然後清理/重建您的項目。

編輯: OK,等待...的R.java文件,你在你的答案顯示了包的名稱如下...

package com.example.test; 

...但你Activity的包名是...

package com.example.sdvd; 

...你的Activity是永遠不會找到R.java文件,因爲這兩個文件必須位於相同的包中,或者您必須在Activity中明確導入com.example.test.R

對你的軟件包名稱進行分類並使它們相互配合,一切都應該工作。

順便說一句 - 在這裏參考stackoverflow不要發佈一個答案,你自己的問題,實際上並沒有回答這個問題 - 它混淆了局勢,讓人們難以遵循你問 - 只是編輯您的原始問題是否需要添加額外信息。

+0

按照您的建議進行了更改,但在清理/重建後仍然出現相同的錯誤。 – ganapathy

+0

@ganapathy:看我的編輯。 – Squonk

+0

對不起!我是堆棧溢出新手。我將在下一次遵守協議。 其實,正如我在我的回答中提到的,我正在處理package com.example.sdvd。但是我創建了一個新項目,並將整個項目從前一個項目複製到新項目。新的包名爲com.example.test; 當我更改了項目後,我不知道如何,但R.java文件正在生成......現在我面臨的問題是'e1無法解析或不是字段'。所以我檢查了新項目的R文件editText沒有自動添加在ID部分 – ganapathy

0

該方法解決了R文件問題。我只是將代碼複製到一個新項目中。我沒有改變任何東西。在構建項目後,'R無法解析爲變量'錯誤神奇地消失了。這種方法已經爲我工作了很多次。但是,你們不認爲每次發生這種錯誤都會把一切都複製到一個新的項目中(如果項目真的很大),這是浪費時間。不應該有適當的解決方案。代碼如何從一個文件複製到另一個項目中去除這樣的錯誤。

儘管R文件錯誤已被刪除。顯示新錯誤,即'e1無法解析或不是字段',其中e1是存在於XML文件中的edittext字段。

<EditText 
    android:id="@+id/e1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:hint="Id" 
    android:padding="11dp" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="39dp" 
    android:ems="10" 
    android:inputType="number"/> 

我使用下面的代碼找到E1

final EditText e_id=(EditText) findViewById(R.id.e1); 

我的R檔

/* AUTO-GENERATED FILE. DO NOT MODIFY. 
* 
* This class was automatically generated by the 
* aapt tool from the resource data it found. It 
* should not be modified by hand. 
*/ 

package com.example.test; 

public final class R { 
    public static final class attr { 
    } 
    public static final class dimen { 
     /** Default screen margins, per the Android Design guidelines. 

     Customize dimensions originally defined in res/values/dimens.xml (such as 
     screen margins) for sw720dp devices (e.g. 10" tablets) in landscape here. 

     */ 
     public static final int activity_horizontal_margin=0x7f040000; 
     public static final int activity_vertical_margin=0x7f040001; 
    } 
    public static final class drawable { 
     public static final int ic_launcher=0x7f020000; 
    } 
    public static final class id { 
     public static final int action_settings=0x7f080001; 
     public static final int button1=0x7f080000; 
    } 
    public static final class layout { 
     public static final int activity_main=0x7f030000; 
    } 
    public static final class menu { 
     public static final int main=0x7f070000; 
    } 
    public static final class string { 
     public static final int action_settings=0x7f050001; 
     public static final int app_name=0x7f050000; 
     public static final int hello_world=0x7f050002; 
    } 
    public static final class style { 
     /** 
     Base application theme, dependent on API level. This theme is replaced 
     by AppBaseTheme from res/values-vXX/styles.xml on newer devices. 


      Theme customizations available in newer API levels can go in 
      res/values-vXX/styles.xml, while customizations related to 
      backward-compatibility can go here. 


     Base application theme for API 11+. This theme completely replaces 
     AppBaseTheme from res/values/styles.xml on API 11+ devices. 

API 11 theme customizations can go here. 

     Base application theme for API 14+. This theme completely replaces 
     AppBaseTheme from BOTH res/values/styles.xml and 
     res/values-v11/styles.xml on API 14+ devices. 

API 14 theme customizations can go here. 
     */ 
     public static final int AppBaseTheme=0x7f060000; 
     /** Application theme. 
All customizations that are NOT specific to a particular API-level can go here. 
     */ 
     public static final int AppTheme=0x7f060001; 
    } 
} 

上次發生同樣的錯誤,我清理項目。從那一刻起,顯示了R文件錯誤。有沒有適當的方法來調試這個錯誤?

只注意到我的編輯文本是沒有得到自動R檔的ID部分

+0

我能夠訪問xml文件中的按鈕。但我無法訪問編輯文本字段。 – ganapathy

+1

在佈局文件中嘗試更改'android:layout_alignLeft'和'android:layout_below'屬性以使用'@ id/e1'而不是'@ + id/e1' – Squonk

+0

@Squonk我做出了您所建議的更改。仍然顯示相同的錯誤。 – ganapathy

0

我覺得這是在XML文件結尾的EditText問題補充。

嘗試了這一點..

<?xml version="1.0" encoding="UTF-8"?> 
<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"> 
    <EditText 
    android:id="@+id/e1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:hint="Id" 
    android:padding="11dp" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="39dp" 
    android:ems="10" 
    android:inputType="number"/> 

    <Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:padding="11dp" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" 
    android:text="Insert" /> 
    <EditText 
    android:id="@+id/editText2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/e1" 
    android:layout_below="@+id/e1" 
    android:layout_marginTop="34dp" 
    android:ems="10" 
    android:hint="Name" 
    android:inputType="textPersonName" 
    android:padding="11dp" /> 
</RelativeLayout> 
+0

進行了您所建議的更改,但仍未解決錯誤。 – ganapathy

+0

你可以改變最終的EditText e_id到EditText e_id嗎?從你的java文件中刪除最終? – thestar

+0

刪除了final關鍵字。還是一樣的錯誤。編號: EditText eid1 = findViewById(R.id.e1); \t \t \t id = eid1.getText()。toString(); – ganapathy