【发布时间】:2014-03-31 05:40:38
【问题描述】:
我真的需要帮助。我是android编程的初学者,我想做一个简单的数独游戏。但是如果没有解决这个问题,我将无法继续。我的 R.java 文件没有被生成。这里是代码 -
package org.example.sudoku;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class Sudokugame extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sudokugame);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.sudokugame, menu);
return true;
}
}
它说 R 不能被解析为变量。并且 gen 文件夹中缺少 R.java 文件。 P.S - 我的文件在 res 文件夹中都没有以大写字母开头,所以这不是错误。
这是我的 xml 代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/main_title"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/continue_label"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/new_game_label"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/about_label"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/exit_label"/>
</LinearLayout>
这是我的 strings.xml 代码:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Sudoku</string>
<string name="main_title">Android Sudoku</string>
<string name="continue_label">Continue</string>
<string name="new_game_label">New Game</string>
<string name="about_label">About</string>
<string name="exit_label">Exit</string>
</resources>
这是我的 androidmanifest.xml 代码:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.example.sudoku"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="1"
android:targetSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="org.example.sudoku.Sudokugame"
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>
【问题讨论】:
-
你需要清除项目。
-
一旦清理项目,然后尝试执行。
-
删除对 R.java 类的任何导入。将光标移动到对任何按钮/布局/字符串/来自 R 的任何内容的引用,删除最后一个字符,然后按 ctrl+空格。 Eclipse 将尝试对您进行代码提示,自动导入正确的 R.java。只是为了保持凉爽、清洁和重建。
-
这是因为您会在 Manifest 文件或 XML 文件中出现错误。请确保您在以下内容中没有任何错误。完成后,清理并导入