【问题标题】:Android Studio app is closed forcedly when I use WebView loadUrl function当我使用 WebView loadUrl 函数时,Android Studio 应用程序被强制关闭
【发布时间】:2021-11-14 04:08:54
【问题描述】:

当我添加WebView 时,应用程序被强制停止。实际上,我不知道这有什么问题。构建成功完成。但是,当我执行构建的应用程序时,该应用程序被强制关闭。我该如何解决这个问题?我将添加我的代码。

AndroidManifest.xml

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

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Intro"
        android:usesCleartextTraffic="true">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/Theme.Intro.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

主活动

    private AppBarConfiguration mAppBarConfiguration;
    private WebView webView;
    private Intent web = new Intent(Intent.ACTION_VIEW);

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

        webView = (WebView) findViewById(R.id.webview);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl("https://google.com");
    }

fragment_intro.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".ui.intro.IntroductionFragment">

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

【问题讨论】:

  • 你能发布 logcat 错误
  • 'Webview' 存在于 'fragment_intro.xml' 中,而在 'MainActivity' 中你是从 'activity_main.xml' 中获取的
  • 您应该将 Webview 粘贴到您的 activity_main.xml 中,如 @Nitish 所述

标签: java android xml webview


【解决方案1】:

MainActivity.java

private AppBarConfiguration mAppBarConfiguration;
    private WebView webView;
    private Intent web = new Intent(Intent.ACTION_VIEW);

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

        webView = (WebView) findViewById(R.id.webview);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl("https://google.com");
    }

如果它不起作用,请在您的代码中添加 IntroductionFragment.java,因为您的布局正在使用它:
as tools:context=".ui.intro.IntroductionFragment

【讨论】:

    猜你喜欢
    • 2014-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-12
    相关资源
    最近更新 更多