我正在尝试在我的网页浏览中打开此website,但它是空的,以下是我的代码,
view = (WebView) this.findViewById(R.id.webView);
view.getSettings().setJavaScriptEnabled(true);
view.setWebViewClient(new MyBrowser());
view.loadUrl("https://www.google.com/maps/dir/Mumbra,+Thane,+Maharashtra/Mazgaon,+Mumbai,+Maharashtra/@19.0890476,72.804611,11z/data=!3m1!4b1!4m14!4m13!1m5!1m1!1s0x3be7bf117349510b:0xc8148b8a43075d71!2m2!1d73.0228173!2d19.1736147!1m5!1m1!1s0x3be7ce5ab292b941:0x19ab115c5e7876ac!2m2!1d72.8436024!2d18.9718388!3e3?hl=en"); //try js alert
view.setWebChromeClient(new WebChromeClient()); // adding js alert support
我在这里做错了什么? 谢谢你的建议。
完整代码 的 MainActivity
public class MainActivity extends Activity {
private WebView view; //membuat variabel view agar bisa akses method onKeyDown
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
view = (WebView) this.findViewById(R.id.webView);// adding js alert support
WebSettings settings = view.getSettings();
settings.setJavaScriptEnabled(true);
settings.setAllowContentAccess(true);
settings.setDomStorageEnabled(true);
view.setWebViewClient(new MyBrowser());
view.setWebChromeClient(new WebChromeClient());
view.loadUrl("https://moovit.com/?from=Mumbra&to=Dadar&fll=19.218316_73.087245&tll=18.976622_72.832794&metroId=3732&lang=en");
}
private class MyBrowser extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
//ketika disentuh tombol back
if ((keyCode == KeyEvent.KEYCODE_BACK) && view.canGoBack()) {
view.goBack(); //method goback() dieksekusi untuk kembali pada halaman sebelumnya
return true;
}
// Jika tidak ada history (Halaman yang sebelumnya dibuka)
// maka akan keluar dari activity
return super.onKeyDown(keyCode, event);
}
}
布局
<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" tools:context=".MainActivity">
<WebView
android:id="@+id/webView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true" />
以下是我已经获得互联网许可的清单文件,我还添加了硬件加速的proprerty并将其值设置为true 的清单
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:hardwareAccelerated="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
答案 0 :(得分:1)
因此,您需要更改webview的高度和宽度,并且它可以正常工作