我試圖創建一個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;
}
});};}
您需要包含logcat才能看到錯誤 – Darrell
Eclipse將不允許我運行該應用程序。 – user3831298
那麼你的'Webview'中的字符串不包含在引號中''「' – Darrell