[英]R cannot be resolved to a variable - Android
我正在學習Android開發,並且正在使用Reto Meier寫的《 Professional Android 4 Application Development》一書。 我將Eclipse與ADT插件配合使用,以遵循本書中的示例。 我已經閱讀了一些有關此問題的現有文章,並嘗試了幾種方法來解決它。 我的代碼與書中的代碼完全相同。 請幫助我,我急於繼續學習本書,但是我不想跳過過去的事情,但是我已經遇到了這個愚蠢的錯誤,超過了三個小時。 這是一個簡單的待辦事項列表應用程序。 這是我的主要活動課。
package com.example.todoolist;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
public class ToDoListActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Inflate your View
第19行 setContentView(R.layout.activity_to_do_list);
// Get references to UI widgets
第22行 ListView myListView =(ListView)findViewById(R.id.myListView);
第23行最終的EditText myEditText =(EditText)findViewById(R.id.myEditText);
// Create the Array List of to do items
final ArrayList<String> todoItems = new ArrayList<String>();
// Create the Array Adapter to bind the array to the List View
final ArrayAdapter<String> aa;
aa = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
todoItems);
// Bind the Array Adapter to the List View
myListView.setAdapter(aa);
myEditText.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN)
if ((keyCode == KeyEvent.KEYCODE_DPAD_CENTER) ||
(keyCode == KeyEvent.KEYCODE_ENTER)) {
todoItems.add(0, myEditText.getText().toString());
aa.notifyDataSetChanged();
myEditText.setText("");
return true;
}
return false;
}
});
}
}
這是我的XML文件...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/myEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/addItemHint"
android:contentDescription="@string/addItemContentDescription"
/>
<ListView
android:id="@+id/myListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
還有我的strings.xml ...
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">ToDoList</string>
<string name="addItemHint">New To Do Item</string>
<string name="addItemContentDescription">New To Do Item</string>
</resources>
我已經進入我的Android SDK Manager,並安裝了所有未安裝的構建工具,然后重新啟動了SDK Manager,並多次清理了我的項目,然后重新啟動了Eclipse,甚至重新啟動了我的計算機。 請幫助,在這一點上,我只想繼續進行操作,並解決此愚蠢的錯誤。 我還添加了import語句,並按照其他職位上的人員的建議刪除了import語句。 它沒有解決問題。 Eclipse在我的Activity類中(第19、22和23行)三次給出“ R無法解析為變量”錯誤。
我也發生了同樣的事情,我花了一天的時間解決這個問題。 確實這是一個愚蠢的錯誤。 我只是在總結我的經驗,也許對您有幫助。 鍵入此setContentView(R.layout.activity_to_do_list);
時,請先仔細查看setContentView(R.layout.activity_to_do_list);
聽到您的起點Rl
然后日食會為您打開一個建議,看起來像這樣
您導入第二個不是layout-android.R
。 第二個在您的項目中創建,聽到com.example.gridtest
是項目包名稱。 然后將導入的部分集中在代碼中
好吧,看看這個import com.example.gridtest.R;
它的重要 。 如果您已經導入了此android.R
則將其刪除。 謝謝,希望它能奏效。 (您不必總是這樣做,但是如果您遇到這種問題,請這樣做)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.