R.id Cannot be resolved

2019-04-10 12:17发布

问题:

I guess something should be done with the Android-Manifest.xml file or something in the layout folder. I am new to Android so I don't know what I should write.

public class AndroidPong extends Activity implements  OnClickListener {
    static String tag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super .onCreate(savedInstanceState);

        tag = getResources().getString(R.string.app_name);
        fullScreen();
        splashScreen();
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        // TODO Auto-generated method stub
        return super .onCreateDialog(id);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // TODO Auto-generated method stub
        return super .onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        return super .onOptionsItemSelected(item);
    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super .onStop();
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super .onDestroy();
    }

    void startGame() {
        setContentView(R.layout.game);// error
        this .findViewById(R.id.Button01).setOnClickListener(this );//error
        this .findViewById(R.id.Button02).setOnClickListener(this );//Error
    }

    void splashScreen() {
        setContentView(R.layout.splash);// error
        this .findViewById(R.id.pongtable).setOnClickListener(this );//error
    }

    void gameMenu() {

    }

    void fullScreen() {
        this .requestWindowFeature(Window.FEATURE_NO_TITLE);
        this .getWindow().setFlags(
                WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

    public static void debug(String debug) {
        Log.d(tag, debug);
    }


<?xml version="1.0" encoding="utf-8"?>
<absoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent">

<Button android:id="@+id/Button01"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"

<Button android:id="+id/Button02"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
 </absoluteLayout>

回答1:

If you are using eclipse, this question has already been asked

R cannot be resolved - Android error



回答2:

It looks like you're having two different types of errors. I'll address the one you refer to in your question title first. This has to do with the resource id.

R.id Cannot be resolved

That error happens when the compiler cannot find the resource that you are referencing for some resource (like a string, layout, menu, etc...). This usually is the result of a few things that you can try.

First, make sure that you're using the correct names to reference your resource id. For example, you have R.id.Button01. If you're using this to reference the res id of a button, then make sure that your button in XML has the attribute android:id="@+id/Button01"

Secondly, make sure there are no errors in your XML layout file. If there are other errors, than often Eclipse (I'm assuming you're using Eclipse) won't recognize that the resource id for that item was created.

If you can't see any obvious error like this, then try to clean up your build. Do this by going to Project -> Clean...

The other errors have to do with your OnClickListeners

this .findViewById(R.id.Button01).setOnClickListener(this );//error
this .findViewById(R.id.Button02).setOnClickListener(this );//Error

and

this .findViewById(R.id.pongtable).setOnClickListener(this );//error

This error is more straightforward. You are setting up your click listeners incorrectly. The setOnClickListener method takes an OnClickListener as an argument. If you want to set up a new one you'll need to pass in a new OnClickListener that you build. You can do it like this...

this.someButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
             // whatever the button will do             
        }
    });


回答3:

I ran into this problem today as well. I checked all of the above answers, but nothing worked. It appeared to me that the R.java file was not being regenerated properly (even after a Project/Clean command). There was no "R.id" class being created for my project.

I finally managed to solve the problem by using the Graphical Layout view on my main.xml layout file (click the Graphical Layout tab at the bottom left of the editor window for main.xml).

I dragged a dummy field into the layout (which included an id) and saved it. Bam! Now Eclipse is generating the R.id class as expected. I removed the dummy field from main.xml and everything is as it should be.



回答4:

I had two images that were named with upper and lower case. It was working fine, and I was using them in my project. I built it a few times and it never cared. All of a sudden for whatever reason it didn't like them anymore.

It showed this in the console:

res\drawable-hdpi\Image.png: Invalid file name: must contain only [a-z0-9_.]

Just take out the uppercase characters and it's fixed. I'm guessing other resource errors will show up in the console too.

Note: You will also see in the "Problems" window an error like this:

Unparsed aapt error(s)! Check the console for output.



回答5:

Some tricks:

You were able to use R.id before, then clean project and build it again. I don't recommend build automatically.

In project properties you can order your /gen folder before your /src folder. This will cause gen folder to be compiled first, so R will be first generated and your changes could be reflect.

Ensure that you haven't got any other kind of compilation errors.