R cannot be resolved to a variable - Hello World

2019-03-01 05:57发布

问题:

I know many people have had this problem and I have searched Google/Stack Overflow, but I somehow still can't figure out what I'm doing wrong.

package HelloWorld;
import android.app.Activity;
import android.os.Bundle;    
import android.widget.TextView;

public class HelloWorldActivity extends Activity {
    /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
 //   TextView tv = new TextView(this);
 //   String hello = "Hello World!";
 //   tv.setText(hello);
 //   setContentView(tv);
}
}

I tried searching for unnecessary import android.R; statements, as well as building/cleaning/refreshing/etc, but I just can't seem to figure it out. Maybe it's a problem with the way I've installed my SDKs? I don't think there is, but I'm very new to this so it's likely I made a blunder somewhere.

Also, I am getting this error:

[2011-12-13 18:18:46 - com.android.ide.eclipse.adt.internal.project.AndroidManifestHelper] Parser exception for C:\Users\MYNAME\Android Development\Hello World\Hello World\AndroidManifest.xml: The processing instruction target matching "[xX][mM][lL]" is not allowed.

Can anyone help me? Thanks!

EDIT: My XML file:

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

    <uses-sdk android:minSdkVersion="10" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".HelloWorldActivity"
            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>

回答1:

See this line in your manifest (at the opening of the <manifest /> tag)

package="HelloWorld Mylove"

That's not a valid package name and has to change. Usually a package name consists out of lowercase words, seperated by dots, e.g.:

com.hello.world

See Naming a Package. Change this to a valid name.

Since this is overall a bit of work, because you have to change it in various places¹ (and sometimes failed for me for some reason), I'd recommend just creating a new project via the new android project wizard in eclipse. This will ask you for a package name in step 4. Make sure to follow the naming conventions mentioned above. You should end up with pretty much the same code. Alternatively you can try to right-click on your project in the project explorer and select Android Tools -> Rename Application Package.

¹ The manifest itself, the folders where your *.java files are stored, the package statement within the source files



回答2:

The error-message that The processing instruction target matching "[xX][mM][lL]" is not allowed. implies that your XML declaration (<?xml version="1.0" encoding="utf-8"?>) is being misinterpreted as a processing instruction. That means that your XML declaration isn't the first thing in your file. Note that it needs to be the very first thing in your file — not even any whitespace is allowed to precede it. If it already is the very first thing in the file, one thing to try is, save it as ASCII/ANSI rather than UTF-8, to eliminate a possible BOM. (Most XML parsers these days are O.K. with UTF-8 that has a BOM, but originally the XML spec didn't allow that, so it seems conceivable that this parser might not be happy with it.)