开发者

R.id cannot be resolved [duplicate]

This question already has answers here: 开发者_开发问答 R cannot be resolved - Android error (108 answers) Closed 9 years ago.

So I copied this tutorial example thing right from Google's android site and I ma getting an error that R.id cannot be resolved.

Here is my Java file

package com.TestApp.HelloWebView;

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;

public class HelloWebView extends Activity {
    WebView mWebView;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mWebView = (WebView) findViewById(R.id.webview);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.loadUrl("http://www.google.com");
    }

}

Here is my res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>

<WebView android:id="@+id/webview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>


You have to import your R class

import com.TestApp.HelloWebView.R;

Also as Demand posted you have to use a namespace for your layout.

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


I had the same problem when I updated my Android SDK. Suddenly new projects could not generate the R.java file.

  • importing my R.java didn't help because it wasn't generated
  • cleaning and building the project didn't help either -- the /gen folder was still empty.

I found that after the upgrade, the adb server didn't start up properly. So a manually started it:

$ cd $ANDROID_SDK/platform-tools
$ adb start-server

Then I restarted Eclipse and did a clean/build on the project, which generated the R.java file.


If using Eclipse, run a clean build (Project/Clean...); this can often clear issues with the resources not being compiled correctly.


Did you try any of the solutions listed here? R cannot be resolved - Android error

Also, look for your R.java file. Are there any errors with it?


I had this problem too. I can't say a solution in general, but you can make a new workplace and copy the hardcode. In the future never click on clean or build, instead save or start eclipse again.


Go in your main.xml file, delete a char and rewrite it. That will generate the R file again.


On trying an Android beginner's tute (http://mobile.tutsplus.com/tutorials/android/android-sdk-development/) the "R.id cannot be resolved" error had me stumped too. In my case it was an android:onClick in /res/layout/main.xml. It seems onClick is not supported in AVD platform 1.5 API level 3.

A change to a new AVD with platform 1.6 API, level 6 removed the error - also started the project from scratch with the new api and level settings. For absolute Eclipse newbies (like myself) create a new AVD with:

Window -> Android SDK and AVD Manager -> New. Select platform, level number from "Target" drop down list.


Also try deleting the ~.android\debug.keystore (Happened to me, thought it was due to R.id issue). Real problem was debug.keystore was older than a year!


I ran into this problem when I tried to follow Lars Vogel's Android development tutorial...it turns out my problem is simply because I didn't save the layout file. Once I save the file, it works fine.


make sure you do not have any xml files with capitals in the name.

I had the same problem.

all the file names in res (and all its sub folders including layout) must be lower case only. This includes image files, xml files, etc (even if they aren't referenced anywhere within the project.)


if you posted full your main.xml file, then it's not right. You must write a namespace of xml:

xmlns:android="http://schemas.android.com/apk/res/android"

for example:

<?xml version="1.0" encoding="utf-8"?>

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/label"
    android:layout_width="78dip"
    android:layout_height="65dip"
    android:paddingTop="4dip"
    android:textSize="12dip"
    android:singleLine="true"
    android:ellipsize="end"
    android:textColor="@color/bright_text_dark_focused"
    android:gravity="center_horizontal|center_vertical" />


Yeah another fix I started the emulator. By running a helloandroid program, that did compile. And got out of eclispe and got back in don't know which one fixed the problem..

Cleaning the project etc didn't fix it - can i have 2 hrs of my life back!


import com.TestApp.R; isn't accepted once R is lost.

My Eclipse now auto creates a new "res" folder everytime I "clean". Yes, I have two folders named "res" in parallel directly under my project folder. The auto created one is empty. String values are still found. No other errors than "R cannot be resolved" are reported by Eclipse.


Eureka! I've managed to make my first android project import to Eclipse!

Earlier I always got "Unable to find system library" under the project's Properties - Java build path - Libraries tab and it was impossible to correct it once it occured. The consequence was that no import statements could be resolved by anything imported.

In this case, I changed the JDK compliance level from 1.6 to 1.5 BEFORE I imported (afterwards its too late and cannot be mended). It must be done from Window - Preferences - Java - Compiler (earlier I thought this option was available only by right-clicking on already imported project). Then right-click on each project and set Properties - Android to a suitable Target (with the errors above, eclipse ignores any selection here). Then, I could even change back to JDK compliance level 1.6.

So, in general, changing the JDK compliance level before importing, might help.

And when deleting a project ALWAYS check the "Delete project content on disk (cannot be undone)" checkbox in the "Are you sure" dialog. Otherwise it is impossible to re-import the project, o if you've imported it with the wrong JDK compliance level the first time, it is impossible to correct the mistake, other than to try in another workspace.

These are the experiences of a very frustrated Eclipse user.


Make sure there is not a hidden error in one of your .xml files.
Sometimes you can delete a statement in the strings.xml file that is important.

For example, in the Vogella example, I got the error:

Error: No resource found that matches the given name (at 'title' with value '@string/menu_settings').

I had deleted the line:

<string name="menu_settings">Settings</string>

Putting this back in or deleting res/menu/main.xml would solve the error.


I had the same problem but got fixed

i dont know exactly how this problem gonna fix

Just try, It might be helpful

add this code at the top of ur code

import com.MyFirstApp.activities.R;

here com.MyFirstApp.activties is my component name replace it with your component name

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜