【问题标题】:WebView crashing runtimeWebView 崩溃运行时
【发布时间】:2017-11-07 23:50:57
【问题描述】:

我是新手。只想通过 WebView 显示一个网页,但是它 启动此特定活动时不断崩溃。请帮忙。 这是我的代码

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebView;
import android.widget.Button;

public class vistaweb extends AppCompatActivity implements View.OnClickListener {

    String url= "https://myschoolserver.com/apps_login.php";


    WebView vWeb = (WebView) findViewById(R.id.MyWeb);
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        setContentView(R.layout.activity_vistaweb);




        vWeb.clearCache(true);
        vWeb.clearHistory();
        vWeb.getSettings().setJavaScriptEnabled(true);
       // vWeb.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        vWeb.loadUrl(url);

        Button back = (Button)findViewById(R.id.back);
        Button forward = (Button)findViewById(R.id.forward);
        Button reload = (Button)findViewById(R.id.relode);

        back.setOnClickListener(this);
        forward.setOnClickListener(this);
        reload.setOnClickListener(this);


    }
    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.back:
                vWeb.canGoBack();

                break;

            case R.id.forward:
                vWeb.canGoForward();
                break;

            case R.id.relode:
                vWeb.reload();
                break;
        }
    }
}

这是我的 xml 文件

<?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.myschoolserver.myschoolserver_beta.vistaweb">

    <Button
        android:id="@+id/back"
        style="@style/Widget.AppCompat.Button.Small"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="3dp"
        android:text="BACK"
        app:layout_constraintLeft_toLeftOf="parent"
        tools:layout_editor_absoluteY="461dp"
        android:layout_marginStart="3dp" />

    <Button
        android:id="@+id/relode"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="RELOAD"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        tools:layout_editor_absoluteY="462dp" />

    <Button
        android:id="@+id/forward"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Forward"
        tools:layout_editor_absoluteY="462dp"
        android:layout_marginRight="8dp"
        app:layout_constraintRight_toRightOf="parent" />

    <WebView
        android:id="@+id/MyWeb"
        android:layout_width="0dp"
        android:layout_height="445dp"
        android:layout_marginLeft="0dp"
        android:layout_marginTop="16dp"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

在启动时,我注意到报告错误在第 16 行显示错误,即 WebView vWeb = (WebView) findViewById(R.id.MyWeb);

请帮忙。提前致谢。

【问题讨论】:

    标签: android webview


    【解决方案1】:

    1.你应该在onCreate方法中调用findViewById

    2.因为你在onClick方法中使用了vWeb,所以你应该将vWeb设置为全局变量。

    3.不要忘记添加权限。

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

    4.在你的代码中设置webView.setWebViewClientwebview.setWebChromeClient

    private WebView vWeb;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getSupportActionBar().hide();
        setContentView(R.layout.activity_vistaweb);
        vWeb = (WebView) findViewById(R.id.MyWeb);
        ...
    }
    

    【讨论】:

    • 是的,您帮助我准确理解我想要的方式。谢谢你,先生。真的很感激。
    猜你喜欢
    • 2016-11-02
    • 2018-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多