简体   繁体   English

为什么我可以在一个应用程序中看到我的网站,而在另一个应用程序中却看不到?

[英]Why can I see my website in one app but not in another?

I wanted to build a simple Android app which just includes a WebView to display my website. 我想构建一个简单的Android应用,其中仅包含一个WebView来显示我的网站。 I used the offical android tutoiral to do so. 我使用了官方的Android tutoiral来做到这一点。

For my first app I used the fullscreen-template. 对于我的第一个应用程序,我使用了全屏模板。 After adding the WebView my xml -file looks like this: 添加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>

And I added this code to hte java -file: 并将此代码添加到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");

And I added the internet permission to the manifest as direct child of the <manifest> -block: 我将Internet许可作为<manifest> -block的直接子级添加到清单中:

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

This works fine. 这很好。 But since I was not completely pleased with the resuling app I created a new project based on the 'empty activity'-template. 但是,由于我对这个重新发布的应用程序不完全满意,因此我基于“空活动”模板创建了一个新项目。 This lead to this much shorter xml -file: 这导致这个更短的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>

And I copy pasted the same code in the java -file: 然后将相同的代码复制粘贴到java -file中:

WebView myWebView = (WebView) findViewById(R.id.webView);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.loadUrl(this._url);

And I again added the internet permission: 然后我再次添加了互联网许可:

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

This app does not work. 这个应用程式无法运作。 However loading data with 但是用

myWebView.loadData("<h1>Test</h1>", "text/html; charset=UTF-8", null);

displays Test . 显示Test

Can someone tell me why the first version work but not the second? 谁能告诉我为什么第一个版本有效,而第二个版本无效?

只要确保您在myWebView.loadUrl("YOUR URL");使用了正确的URL myWebView.loadUrl("YOUR URL");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM