2013-05-01 114 views
0

Eclipse在setContentView(R.layout.main);行中顯示main is not a field or cannot be resolved錯誤。我試圖添加一個導入,import android.R;,但它所做的是導致更多的錯誤。這些錯誤似乎圍繞着包含「R」的線條。有什麼建議麼?Webview後退按鈕導致錯誤

這裏是我的xml文件

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

    <TextView 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     android:text="This is the demo of WebView Client" 
     android:textSize="20sp" 
     android:gravity="center_horizontal">  
    </TextView> 

    <ProgressBar 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:layout_gravity="center" 
     android:id="@+id/progressBar1"/>  

    <WebView 
     android:id="@+id/webview01" 
     android:layout_height="wrap_content" 
     android:layout_width="fill_parent" 
     android:layout_weight="1"> 
    </WebView> 



</LinearLayout> 

下面是活動文件

package com.example.xxx; 

import android.app.Activity; 
import android.graphics.Bitmap; 
import android.os.Bundle; 
import android.view.KeyEvent; 
import android.view.View; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
import android.widget.ProgressBar; 



public class MainActivity extends Activity { 


    WebView web; 
    ProgressBar progressBar; 

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

     web = (WebView) findViewById(R.id.webview01); 
     progressBar = (ProgressBar) findViewById(R.id.progressBar1); 

     web.setWebViewClient(new myWebClient()); 
     web.getSettings().setJavaScriptEnabled(true); 
     web.loadUrl("http://www.technotalkative.com"); 
    } 

    public class myWebClient extends WebViewClient 
    { 
     @Override 
     public void onPageStarted(WebView view, String url, Bitmap favicon) { 
      // TODO Auto-generated method stub 
      super.onPageStarted(view, url, favicon); 
     } 

     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) { 
      // TODO Auto-generated method stub 

      view.loadUrl(url); 
      return true; 

     } 

     @Override 
     public void onPageFinished(WebView view, String url) { 
      // TODO Auto-generated method stub 
      super.onPageFinished(view, url); 

      progressBar.setVisibility(View.GONE); 
     } 
    } 


    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
     if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) { 
      web.goBack(); 
      return true; 
     } 
     return super.onKeyDown(keyCode, event); 
    } 

} 
+0

希望你已經嘗試清理項目並檢查佈局文件夾中是否存在xml文件。它不建議你自己加入.R。讓它自動生成。 – 2013-05-01 02:56:05

回答

1

你不應該import android.R,但import you.package.name.R代替,機器人,因爲你用R.layout.main,請確保您的佈局文件名爲主.xml res/layout下。

+0

我試圖添加它,它的工作,但任何建議,爲什麼我的進度條只顯示進度欄,但不去網頁? @ A.man @ EricHua23? – 2013-05-01 03:14:18

+0

您是否在您的AndroidManifest.xml中添加Internet權限?' – EricHua23 2013-05-01 03:19:13

+0

是的,我有那個 – 2013-05-01 03:47:28