2014-07-25 65 views
0

我試圖創建一個android應用程序,當創建一個webview時,它可以與設備互動而無需打開本機網頁瀏覽器。我在這裏查看了其他問題,並且我在網上看到了一些其他建議後創建了此代碼。但是我的代碼不起作用。什麼似乎是錯誤的?Android錯誤Webview不工作

這裏是我的活動代碼:

import android.os.Bundle; 
import android.app.Activity; 
import android.content.Intent; 
import android.graphics.Color; 
import android.view.View; 
import android.webkit.WebSettings; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 
import android.widget.Button; 



public class SecondActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_second); 

     Button switchButton = (Button) findViewById(R.id.button1); 
     switchButton.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       Intent intent = new Intent(SecondActivity.this, MainActivity.class); 
       startActivity(intent); 
      } 

     }); 



     WebView myWebView = (WebView) findViewById(R.id.webView1); 
     myWebView.setWebViewClient(new WebViewClient(){   
      @Override 
      public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) { 
      view.loadUrl("https://www.google.com"); 
      return false; 
      } 
     }); 
    } 
} 

這裏是我的xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".SecondActivity" > 

    <WebView 
     android:id="@+id/webView1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_above="@+id/button1" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" /> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:text="Back" /> 

</RelativeLayout> 

這裏是我的編輯,還是有語法錯誤。我無法弄清楚我在語法方面做了什麼錯誤。

WebView myWebView = (WebView) findViewById(R.id.webView1); 
myWebView.setWebViewClient(new WebViewClient() { 
     public boolean shouldOverrideUrlLoading("WebView: view, String, https://www.google.com/") { 
     view.loadUrl("https://www.google.com/"); 
     return false; 
     } 
    });};} 
+0

您需要包含logcat才能看到錯誤 – Darrell

+0

Eclipse將不允許我運行該應用程序。 – user3831298

+0

那麼你的'Webview'中的字符串不包含在引號中''「' – Darrell

回答

3

正如我已經評論過的,你的代碼中有語法錯誤。我已經修復了它,但是你錯過了另一點。在重寫shouldOverrideUrlLoading(WebView view, String url)方法時,不需要指定url。

以下代碼片段提供了一個提示,說明如何覆蓋方法。

WebView myWebView = (WebView) findViewById(R.id.webView1); 
myWebView.setWebViewClient(new WebViewClient(){  
    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) { 
     view.loadUrl(url); 
     return false; 
    } 
}); 

你安裝後的WebViewClient你應該叫loadUrl(..)方法像以下。

myWebView.loadUrl("https://www.google.com"); 

順便說一下,如果你的WebViewClient嘗試裝入硬編碼的URL,這是行不通的罰款。假設您希望加載"https://www.facebook.com",它將再次加載"https://www.google.com"。因此,WebView必須每次加載作爲參數給定的url。

還有一件事,您應該在您的AndroidManifest.xml上指定權限,以便應用程序從Internet加載內容。

<uses-permission android:name="android.permission.INTERNET" /> 

不要忘記添加上述權限。

希望這可能有所幫助。

+0

我的eclipse仍然告訴我,代碼充滿了語法錯誤。可能會出現什麼問題?即使你給的第一部分給出了3個語法錯誤。 \t WebView myWebView =(WebView)findViewById(R.id.webView1); \t myWebView.setWebViewClient(新WebViewClient(){ \t @覆蓋 \t公共布爾shouldOverrideUrlLoading(web視圖視圖,字符串URL){ \t view.loadUrl(URL); \t返回假; \t} \t}); } – user3831298

-1

你想要這樣的東西。

shouldOverrideUrlLoading(WebView: view, String webPage) 

然後加入

WebPage = "https://www.google.com/"; 

並改變

view.loadUrl(webPage); 
return false; 

所以最終代碼會看起來像這樣

shouldOverrideUrlLoading(WebView: view, String webPage){ 
    WebPage = "https://www.google.com/"; 
    view.loadUrl(webPage); 
    return false; 
}