开发者

eclipse error with android: id cannot be resolved or is not a field

I just started playing around with android development, and already with just an atte开发者_如何学Cmpt at making a button, I have encountered a problem. The error I'm given in the following code is right on "R.id.button1". It says id cannot be resolved or is not a field. Do I need to manually reference every single object I make in the layout xml file? I found that this did work, but it does seem to be a bit much for every button I want to make...

package com.example.helloandroid;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class HelloAndroid extends Activity {
    /** Called when the activity is first created. */

 private Button button1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        button1 = (Button)findViewById(R.id.button1);
        button1.setOnClickListener(new OnClickListener()
        {
         public void onClick(View v)
         {
          finish();          
         }        
        });
    }
}


I've been wasting a lot of time (two weeks) because of the same problem until I discovered the problem wasn't mine but Eclipse's. I guess there's a lot of people with the same problem.

Just try this: Save your project, close Eclipse and then open it again. So simple.


Do I need to manually reference every single object I make in the layout xml file

Yes, otherwise you won't be able to do anything with those views. It's not that bad actually. So, each time you create a view in your XML, and you want to reference it, put an ID:

<View
    android:id="@+id/the_id"/>

And then, from your code you can reference it using the R class. You can type, in the example, R.id.the_id and then Ctrl+Shift+O to make Eclipse auto import the needed files.

You can speed up your productivity by using frameworks like Roboguice; I think it's for lazy people, though.


This answer is not applicable to this question (looking at code you have provided). Just adding it if someone else stumbles here and above mentioned answers do not help.

If cleaning (Project --> clean) doesn't helps or saving and restarting eclipse doesn't help either, check for the following incorrect import.

import android.R;

Which Eclipse sometimes add by mistake on auto-import (Ctrl+Shift+O). Remove that line (import) and it's done :D


Following this EXCELLENT tutorial , I encountered the same problem. After reading Carmello's answer (Sept 17, 2011. 07:23) I simply clicked File->Save All, and voila, 'button0' was automagically defined, and even syntax highlighted.


If "R.id.button1" is not defined, then you'll get a compile error, just as you saw. If you don't define this in the layout, then it won't be defined.

You don't have to specify every object you create in the layout, but you do if you try to reference it from "R.*". You can manually create buttons and other objects that are not specified in the layout.


I ran through the same issues for time being. Plz, do not forget to define as follows:

<View
android:id="@+id/button1" />

if you are using the id in your .java class.

Button b =(Button) findViewById(R.id.button1);

Being said that, the id defined in xml file must match with the id in findViewById().


Go to the 'R.java' file under the 'gen' folder and check whether your 'button1' is present under the class 'id'.If not,then this could be the reason you got that error.When you use the statement " R.id. " make sure that the is present under the appropriate class,in this case under the 'id' class.


R.id is a generated object that assigns int numbers to resources. Try this go to your gen/mypackage/R.java and delete the file. As you can see it is re-generated. This file provides static references where as the context is more of the dynamic state of your app. If you have syntax errors that will prevent automatic re-generation of that R.java file so you will get lots or R. errors. As everyone else has said you can click save all icon or ctl+shift+s on windows. You can clean the project project/clean and that will clean up 95% of those exceptions. Yes eclipse is buggy that way but netbeans does not support android that well. this link may help

Good luck


Do these things,anyone of this will help you

  1. Project -> Clean, Right click -> Fix Project Properties
  2. Restart Eclipse
  3. make some fake modification in manifest and save
  4. check your console for any error message
  5. check your drawable folder, check the image names satisfy the rules
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜