开发者

Android/Eclipse Question - "id cannot be resolved or is not a field" error

Hello I started trying to develop Android applications today, but I have a problem:

I decided to try out making a Web View using this tutorial - http://developer.android.com/resources/tutoria开发者_Python百科ls/views/hello-webview.html

But when I put the code in for the OnCreate() method I get an "id connot be resolved or is not a field" error. Here is my code:

main.xml:

<?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"
/>

HelloWebView.java:

package com.example.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");
    }
}

I have tried cleaning, CTRL+SHIFT+O, and just completely restarting the project. There error is in the statement: mWebView = (WebView) findViewById(R.id.webview); and Eclipse just says "id cannot be resolved or is not a field"

Also, I installed everything today using the guide on developer.android.com and followed all steps correctly. I have made Java programs on this computer before so I don't think there is a problem related to the JDK/JRE.


I managed to fix the problem by simply restarting my computer. I guess in order for all the development tools to work properly a computer restart is necessary? I can't believe I didn't think about doing this before though. Thanks for those who tried to help.


There are a couple of similar questions here. Take a look at some answers:

  • import of android.R : this response: R cannot be resolved - Android error

  • java tools not building R.java: R cannot be resolved - Android error

  • problem with default.properties file: R cannot be resolved - Android error

  • and finally, something reported by several: make sure the file is main.xml and not Main.xml


Found the solution that finally worked for me after researching hours. Turns out the problem is not your R.java, All you gotta do is 'Clean' the project one last time. Save your project. And open Eclipse again. The problem is GONE!


Look into the R File Generated and see whether you see a Public Static Class called Id if so then see whether it contains a public static final int webview ( that you have created )


I also get that from time to time.

This usually (or always) does it for me: Project - Clean - "your project"

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜