[英]Why can I see my website in one app but not in another?
我想構建一個簡單的Android應用,其中僅包含一個WebView來顯示我的網站。 我使用了官方的Android tutoiral來做到這一點。
對於我的第一個應用程序,我使用了全屏模板。 添加WebView之后,我的xml
-file如下所示:
<FrameLayout 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:background="#0099cc"
tools:context="com.example.janwagner.webviewdemo.FullscreenActivity">
<!-- The primary full-screen view. This can be replaced with whatever view
is needed to present your content, e.g. VideoView, SurfaceView,
TextureView, etc. -->
<TextView
android:id="@+id/fullscreen_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:keepScreenOn="true"
android:text="@string/dummy_content"
android:textColor="#33b5e5"
android:textSize="50sp"
android:textStyle="bold" />
<!-- This FrameLayout insets its children based on system windows using
android:fitsSystemWindows. -->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:id="@+id/fullscreen_content_controls"
style="?metaButtonBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:background="@color/black_overlay"
android:orientation="horizontal"
tools:ignore="UselessParent">
<Button
android:id="@+id/dummy_button"
style="?metaButtonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/dummy_button" />
</LinearLayout>
</FrameLayout>
</FrameLayout>
並將此代碼添加到hte java
-file:
WebView myWebView = (WebView) findViewById(R.id.webView);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl("http://192.168.100.166:8080");
我將Internet許可作為<manifest>
-block的直接子級添加到清單中:
<uses-permission android:name="android.permission.INTERNET" />
這很好。 但是,由於我對這個重新發布的應用程序不完全滿意,因此我基於“空活動”模板創建了一個新項目。 這導致這個更短的xml
-file:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.janwagner.trainerapp.MainActivity">
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</android.support.constraint.ConstraintLayout>
然后將相同的代碼復制粘貼到java
-file中:
WebView myWebView = (WebView) findViewById(R.id.webView);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl(this._url);
然后我再次添加了互聯網許可:
<uses-permission android:name="android.permission.INTERNET" />
這個應用程式無法運作。 但是用
myWebView.loadData("<h1>Test</h1>", "text/html; charset=UTF-8", null);
顯示Test 。
誰能告訴我為什么第一個版本有效,而第二個版本無效?
只要確保您在myWebView.loadUrl("YOUR URL");
使用了正確的URL myWebView.loadUrl("YOUR URL");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.