2012-12-10 20 views
0

我一直在一個Android項目,但一個非常惱人的錯誤是殺了我:「R無法解決或不是一個領域」。那麼,有很多問題誰得到了答案,但我現在嘗試的一切都不起作用:乾淨的項目,銷燬R.java &乾淨的項目,導入x.y.z.R;並銷燬任何導入的android.R;但它仍然存在。你能幫助我嗎?Android項目 - R無法解析或不是字段

這裏來發生錯誤代碼(ListArretsActivity.java):

package com.example.onetantan; 

import java.util.ArrayList; 

import com.example.onetantan.rest.Arret; 
import com.example.onetantan.rest.ArretRestMethod; 
import com.example.onetantan.rest.ListArret; 
import com.example.onetantan.R; 

import android.app.ListActivity; 
import android.content.Context; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Menu; 

public class ListArretsActivity extends ListActivity { 
    ... // Code that doesn't seemed revelant, for me, for the error given 
     class MyAsyncTask extends AsyncTask<Void, Void, Void> { 
      @Override 
      protected Void doInBackground(Void... paramArrayOfParams) { 
       ArretRestMethod arm = new ArretRestMethod(ListArretsActivity.this.R); 
       return null; 
      } 
     } 
    ... // Code that doesn't seemed revelant, for me, for the error given 
} 

這裏說到那裏是沒有問題的代碼...(ArretAdapter.java):

package com.example.onetantan; 

import java.util.List; 

import com.example.onetantan.rest.Arret; 
import com.example.onetantan.R; 

import android.content.Context; 
import android.util.Log; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.TextView; 

public class ArretAdapter extends BaseAdapter { 
    private static String LOG_TAG="ArretAdapter"; 
    private Context mContext; 
    private List<Arret> mListArret; 
    private LayoutInflater inflater; 

    public ArretAdapter(Context p_oContext, List<Arret> p_oListArret){ 
     super(); 
     mContext = p_oContext; 
     mListArret = p_oListArret; 
     this.inflater = LayoutInflater.from(mContext); 
    } 
    ... // Code that doesn't seemed revelant, for me, for the error given 
    @Override 
    public View getView(int p_iPosition, View p_oConvertView, ViewGroup p_oParentView) { 
     if (p_oConvertView == null) { 
      p_oConvertView = inflater.inflate(R.layout.arret_item, null); 
     } 
     ((TextView)(p_oConvertView.findViewById(R.id.name))).setText("Heure"); 
     ((TextView)(p_oConvertView.findViewById(R.id.dist))).setText("Heure"); 
     return p_oConvertView; 
    } 
    ... // Code that doesn't seemed revelant, for me, for the error given 
} 

而且完成後,我的程序包資源管理器的圖片爲src & gen文件夾。想要發佈圖片,但沒有足夠的聲譽,因爲我是一個新用戶,所以這裏有一個鏈接:http://img217.imageshack.us/img217/5511/tosend.png

這已經過去了一個星期,因爲我沒有能夠做一件事情...也許是太明顯,我沒有看到,或者是一個嚴重的問題,但我真的需要你的幫助。

在此先感謝您提供給我的任何答案(即使它是我嘗試過三次的其他問題的答案,如果需要,我會再次嘗試)。

編輯:你的答案之一是檢查我的XML文件的任何錯誤。但是Eclipse不會發回任何錯誤,我也沒有看到任何錯誤(我根本沒有嘗試過......)。 在我的資源>佈局文件夾:arret_item.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:id="@+id/name" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="TextView" /> 

    <TextView 
     android:id="@+id/dist" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="TextView" /> 

</LinearLayout> 

在我的資源>佈局文件夾:arret_item.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" 
    tools:context=".ListArretsActivity" > 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:text="@string/hello_world" /> 

</RelativeLayout> 

這些人是我res.layout僅有的兩個.xml文件我可能會被編輯。

EDIT 2:運行Android的細絨,它只返回我警告:

不針對Android系統的最新版本;適用性模式適用。 .gif注意位圖格式不鼓勵(64項) 實測值位圖繪製RES /抽拉/ l_1.gif在densityless文件夾(68項) 的的ressource R.drawable.l_1似乎是未使用的(70項)

由於我正在使用API​​ 17(Android 4.2),並且我的清單是16,所以我立即糾正了第一個錯誤。但它不能糾正我的錯誤。

對於另外3個警告,這是正常的,因爲我暫時不使用它們,因爲我給這個項目帶來了這個項目。

好,這纔是我的AndroidManifest.xml因爲我編輯它:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.onetantan" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="17" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.example.onetantan.ListArretsActivity" 
      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> 

答:對不起大家,還有我沒有給一個代碼,它是ArretRestMethod.java。如果我已經給你這個,你可能會明白:

package com.example.onetantan.rest; 

import ... 

public class ArretRestMethod { 
     private static String LOG_TAG="ArretRestMethod"; 
     Context mContext=null; 

     public ArretRestMethod(Context context) { 
      mContext = context.getApplicationContext(); 
     } 
     ... 
} 

我的一個朋友指出我在ListArretsActivity中的一行。java的

ArretRestMethod arm = new ArretRestMethod(ListArretsActivity.this.R); 

必須這樣寫:

ArretRestMethod arm = new ArretRestMethod(ListArretsActivity.this); 

免費鞭子將分配給任何人誰願意給我的懲罰...:X(我很慚愧,我沒有看到這個......對不起大家,但是我通過詢問得知有至少一些事情。)

+0

你不應該被導入進口com.example.onetantan.R; – Jameo

+0

在arret_item.xml的頂部是否是<?xml version =「1.0」encoding =「utf-8」?>?這是必要的,如果t不是,可能會打破你的_R_引用。 – Kirk

+0

嘗試將您的** API **更改爲另一個級別後,再轉回原始級別。並檢查** ErrorLog **或**問題**查看有一些錯誤。 –

回答

1

烏斯清理項目....

in eclipse goto -->projects --> select clean --> select your project (shows in popup) 
+0

對不起,但它不起作用。 這是我見過最多的答案,而且我已經完成了超過20次,但它並沒有使它更好。無論如何,再次嘗試但不是,我的項目仍然固執。不管怎麼說,還是要謝謝你。 –

+0

嘗試清理並重新啓動Eclipse。有時需要重新啓動Eclipse。 – Kirk

+0

只是檢查你的錯誤日誌,是否有任何錯誤 –

1
  • 檢查您最近編輯的xml文件是否有錯誤。 Eclipse有時會錯過xml文件中的錯誤,因此您可能必須打開最近編輯的文件。
  • 檢查res文件夾中是否有 資源在文件名中有非法字符。
+0

Eclipse不發回任何錯誤...我沒有看到任何錯誤。我將編輯我的帖子,以便提供我的xml代碼。再次感謝。 –

0

檢查<?xml version="1.0" encoding="utf-8"?>是arret_item.xml的頂部。它從您發佈的代碼中缺少,但我只是一個複製粘貼的遺漏。這是必要的,如果它不在那裏,可能會破壞你的R引用。

此外,請嘗試在Eclipse中運行Android Lint。這將有助於指出可能實際上是未報告的錯誤的警告。

林特細節:http://tools.android.com/tips/lint#TOC-Eclipse-Usage

enter image description here

+0

感謝您的建議,不瞭解Android Lint。但它只是向我發出警告。否則,arret_item.xml中缺少的第一行是複製粘貼省略。 –

+0

如果可以,請嘗試修復警告。他們可能會造成這個問題。 – Kirk

0
ArretRestMethod arm = new ArretRestMethod(ListArretsActivity.this.R); 

爲什麼你需要的R上面的行?我相信它需要一個上下文實例。

如果是這樣,
使場 - Context context;
中的onCreate - context= this;
使用上下文 - ArretRestMethod arm = new ArretRestMethod(context);

如果其他一些參數是必需的傳球被代替。

0

IF ALL ELSE FAILS ... 創建一個新項目並手動複製所有文件。

我最近正在執行代碼的更新,這些代碼在15個月內沒有被觸及,並且無法通過該代碼。這可能是我錯過的一個配置設置或其他一些微不足道的部分,但我希望在3小時前完成這個配置。

0

我有同樣的問題。在經過Clean,Restarts等第一手嘗試之後,我刪除了:

import android.R;

,並與我的包的名字

進口com.josh.microqr取而代之

似乎愚蠢的,但許多清潔後,小錯誤喜歡上了這一個阻力。

0

如果您確定xml和其他設置沒有問題,請先備份您的項目,然後在「gen」文件夾中刪除您的R文件現在再次構建項目,這將重新生成您的R文件(這將僅適用於如果您的項目沒有錯誤,那麼「R無法解析或不是字段」)。

0

我認爲,任何的下面應該可以幫助你解決這個問題:

  • 檢查你的XML文件中的錯誤。
  • 檢查您的res文件夾中是否有文件名中含有無效字符的文件
  • 請執行project-> clean project。它將重新生成R.java文件。

RK
http://ctrlaltsolve.blogspot.in