http://developer.android.com/training/basics/firstapp/building-ui.html安卓教程錯誤:R 1不能被解析爲一個變量
我一直在下面這個教程,但我有兩個錯誤,無論是「R不能解析爲一個變量」。過去我製作了Android應用程序(簡單的應用程序),並且我記得通過檢查是否有導入R語句(我不這樣做)來解決這個問題,以及項目是否在構建之前已被清理再次(我清理它,我仍然得到錯誤)。我不知道該怎麼做。謝謝!
*我也想提一下,我看到170個匹配的同一個標題的線程,「刪除導入R語句」的解決方案不適用於我的問題(我不認爲) 再次感謝
所有的代碼都是直接從上面的鏈接,但在這裏是爲了方便
activity_my_first.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<EditText android:id="@+id/edit_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="@string/edit_message" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send" />
</LinearLayout>
MyFirstActivity.java
package com.example.myfirstapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;
public class MyFirstActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_first);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_my_first, menu);
return true;
}
}
的strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">My First App</string>
<string name="edit_message">Enter a message</string>
<string name="button_send">Send</string>
<string name="menu_settings">Menu Settings</string>
</resources>
編輯:當我清理項目,我得到在控制檯此消息:
[2012-06-29 11:12:38 - MyFirstApp] W/ResourceType(6140): Bad XML block: header size 91 or total size 0 is larger than data size 0 [2012-06-29 11:12:38 - MyFirstApp] C:\Users\zhong\workspace\eclipse\MyFirstApp\res\menu\activity_my_first.xml:2: error: Error: No resource found that matches the given name (at 'title' with value '@string/menu_settings').
編輯:添加了一行佈局xml文件<string name="menu_settings">Menu Settings</string>
固定清理項目錯誤,但我仍然無法從兩個R錯誤中運行該項目。
你有沒有清理的項目? – 2012-06-29 02:16:45
是的。我只是注意到當我清理時出現錯誤。我編輯了原帖 – SwimBikeRun