首页 > 解决方案 > 网络应用程序在安装和打开时出现黑屏?

问题描述

我是 android 的新手,正在尝试通过遵循教程制作 webapp。

但是,当安装一个签名的 apk 时,打开它会给出一个空白页面。不知道出了什么问题。

我编辑的页面是:

活动主:

<?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="fill_parent"
    android:layout_height="fill_parent"
    android:paddingBottom="1dp"
    android:paddingLeft="1dp"
    android:paddingRight="1dp"
    tools:context=".MainActivity">

    <ProgressBar
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/progressBar"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/swipe"
        android:layout_width="match_parent"
        android:layout_height="match_parent">



        <WebView

            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:id="@+id/webView"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentTop="true" />

    </android.support.v4.widget.SwipeRefreshLayout>

</android.support.constraint.ConstraintLayout>

安卓清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.iinfohub.www.iinfohub">

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/logo"
        android:label="IINFO HUB"
        android:roundIcon="@drawable/logo"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

MainActivity.java

    package com.iinfohub.www.iinfohub;

    import android.content.Intent;
    import android.net.Uri;
    import android.support.v4.widget.SwipeRefreshLayout;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.KeyEvent;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;

    public class MainActivity extends AppCompatActivity {

    WebView mWebView;

    SwipeRefreshLayout swipe;

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


        swipe = (SwipeRefreshLayout) findViewById(R.id.swipe);
        swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            public void onRefresh() {
                LoadWeb();
            }
        });

        LoadWeb();

    }

    public void LoadWeb() {
        mWebView = (WebView) findViewById(R.id.webView);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setAppCacheEnabled(true);
        mWebView.loadUrl("http://iinfohub.com/forapp/index.php");
        swipe.setRefreshing(true);
        mWebView.setWebViewClient(new WebViewClient() {

            public void onReveivedError(WebView view, int errorCode, String description, String failingUrl) {
                mWebView.loadUrl("file://android_asset/error.html");
            }

            public void onPageFinished(WebView view, String url) {
                //hide the swipe refreshlayout
                swipe.setRefreshing(false);
            }

        });

    }


    @Override
    public boolean onKeyDown(final int keyCode, final KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
            mWebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}

我所做的只是按照该教程进行操作,正如我所提到的,它提供了一个运行签名 APK 的空白页面。

非常感谢您对此的任何帮助。

标签: androidandroid-studio

解决方案


我认为你应该loadUrl毕竟。因为设置/配置必须在加载页面之前应用。


推荐阅读