I know this question might have been asked a thousand times on StackOverflow, but I have a precise question : When I have the problem of "R cannot be resolved to a variable", should I import the R of the package ? I know that eclipse suggests it and doing it solves the problem but is that correct to have the import of R in the class ?

有帮助吗?

解决方案

Yes. All of your resource ids are compiled into R.java. If you do not import com.mypackage.R then any references such as R.id.textview will not resolve.

其他提示

Usually, you don't need to import R.java. As @Vyger said, most of the time when Eclipse suggests you to import R.java there is something wrong with your R.java... Try to delete and recreate it (See Developing for Android in Eclipse: R.java not regenerating).

This will solve the problem most of the time... If this doesn't work, just import it. It won't harm your application.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top