如何使WebView通过wifi加载?

时间:2019-07-12 23:55:45

标签: java android-webview

我似乎无法加载我的TermName。我得到的是空白(白色)活动,而不是网页。

我尝试为WebViewACCESS_WIFI_STATE添加权限,但是这些不起作用? 我收到此错误消息:

ACCESS_NETWORK_STATE

这是代码: 公共类WebViewActivity扩展了AppCompatActivity {

07-13 04:02:18.570 18878-18878/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.packagename.phase1challenge, PID: 18878
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.packagename.phase1challenge/com.packagename.phase1challenge.WebViewActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2613)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
        at android.app.ActivityThread.-wrap11(ActivityThread.java)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1517)
        at android.os.Handler.dispatchMessage(Handler.java:111)
        at android.os.Looper.loop(Looper.java:227)
        at android.app.ActivityThread.main(ActivityThread.java:6102)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:859)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749)
     Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
        at android.view.ViewGroup.addViewInner(ViewGroup.java:4450)
        at android.view.ViewGroup.addView(ViewGroup.java:4286)
        at android.view.ViewGroup.addView(ViewGroup.java:4227)
        at android.view.ViewGroup.addView(ViewGroup.java:4200)
        at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:460)
        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:145)
        at com.packagename.phase1challenge.WebViewActivity.onCreate(WebViewActivity.java:20)
        at android.app.Activity.performCreate(Activity.java:6280)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1131)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2566)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
        at android.app.ActivityThread.-wrap11(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1517) 
        at android.os.Handler.dispatchMessage(Handler.java:111) 
        at android.os.Looper.loop(Looper.java:227) 
        at android.app.ActivityThread.main(ActivityThread.java:6102) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:859) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:749) 

布局资源文件

private WebView myWebView;

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


    WebView myWebView = (WebView) findViewById(R.id.web_view);
    myWebView.loadUrl("https://url.com/alc/");

    }

@Override
public void onBackPressed() {
    if (myWebView.canGoBack()) {
        myWebView.goBack();
    } else {
        super.onBackPressed();
    }

清单

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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"
    android:orientation="vertical"
    tools:context=".WebViewActivity">

    <WebView
        android:id="@+id/web_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

0 个答案:

没有答案