gpt4 book ai didi

java - 如何在 fragment 中使用Android Webview

转载 作者:行者123 更新时间:2023-12-02 09:17:56 25 4
gpt4 key购买 nike

HomeFragment.java

package com.example.starcitizen.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import androidx.fragment.app.Fragment;
import com.example.starcitizen.R;

public class HomeFragment extends Fragment {
public HomeFragment(){ }

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState){
View rootView = inflater.inflate(R.layout.fragment_home, container, false);

String url = "http://google.com";
WebView view = (WebView) rootView.findViewById(R.id.home_webview);
view.getSettings().setJavaScriptEnabled(true);
view.loadUrl(url);

return rootView ;
}
}


fragment_home

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context="com.example.starcitizen.fragment.HomeFragment">

<WebView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/home_webview"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true" />

</RelativeLayout>


我看了其他问题。但我的应用程序无法正常工作。
所以我想知道是否需要修复我的主要活动。因此,我附加了主要活动。

MainActivity.java

package com.example.starcitizen;

import android.os.Bundle;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import com.google.android.material.navigation.NavigationView;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.view.Menu;

public class MainActivity extends AppCompatActivity {

private AppBarConfiguration mAppBarConfiguration;

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

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_guide, R.id.nav_maps,
R.id.nav_ships, R.id.nav_weapons, R.id.nav_mission)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
}


对不起,我英语不好。因此,在翻译的帮助下...
我第一次问stackoverflow。



我正在为韩国人开发应用程序。但是我在片段方面有一些问题。

如果输入了Webview,则App将关闭而不运行。

请帮我。

添加logcat!



11-15 20:00:31.465 3666-3666 / com.example.starcitizen E / Android运行时:致命异常:主
    流程:com.example.starcitizen,PID:3666
    java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.starcitizen / com.example.starcitizen.MainActivity}:android.view.InflateException:二进制XML文件第8行:膨胀类android.webkit.WebView的错误
        在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
        在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
        在android.app.ActivityThread.access $ 800(ActivityThread.java:151)
        在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1303)
        在android.os.Handler.dispatchMessage(Handler.java:102)
        在android.os.Looper.loop(Looper.java:135)
        在android.app.ActivityThread.main(ActivityThread.java:5254)
        在java.lang.reflect.Method.invoke(本机方法)
        在java.lang.reflect.Method.invoke(Method.java:372)
        在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:905)
        在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
     原因:android.view.InflateException:二进制XML文件第8行:错误膨胀了类android.webkit.WebView
        在android.view.LayoutInflater.createView(LayoutInflater.java:633)
        在com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
        在android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)
        在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
        在android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
        在android.view.LayoutInflater.inflate(LayoutInflater.java:504)
        在android.view.LayoutInflater.inflate(LayoutInflater.java:414)
        在com.example.starcitizen.fragment.HomeFragment.onCreateView(HomeFragment.java:18)
        在androidx.fragment.app.Fragment.performCreateView(Fragment.java:2600)
        在androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:881)
        在androidx.fragment.app.FragmentManagerImpl.addAddedFragments(FragmentManagerImpl.java:2100)
        在androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1874)
        在androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1830)
        在androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727)
        在androidx.fragment.app.FragmentManagerImpl.dispatchStateChange(FragmentManagerImpl.java:2663)
        在androidx.fragment.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManagerImpl.java:2613)
        在androidx.fragment.app.Fragment.performActivityCreated(Fragment.java:2624)
        在androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:904)
        在androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
        在androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303)
        在androidx.fragment.app.FragmentManagerImpl.dispatchStateChange(FragmentManagerImpl.java:2659)
        在androidx.fragment.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManagerImpl.java:2613)
        在androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:246)
        在androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:542)
        在androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:201)
        在android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1236)
        在android.app.Activity.performStart(Activity.java:6006)
        在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288)
        在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
        在android.app.ActivityThread.access $ 800(ActivityThread.java:151)
        在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1303)
        在android.os.Handler.dispatchMessage(Handler.java:102)
        在android.os.Looper.loop(Looper.java:135)
        在android.app.ActivityThread.main(ActivityThread.java:5254)
        在java.lang.reflect.Method.invoke(本机方法)
        在java.lang.reflect.Method.invoke(Method.java:372)
        在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:905)
        在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
     引起原因:java.lang.reflect.InvocationTargetException
        在java.lang.reflect.Constructor.newInstance(本地方法)
        在java.lang.reflect.Constructor.newInstance(Constructor.java:288)
        在android.view.LayoutInflater.createView(LayoutInflater.java:607)
        在com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:55)
        在android.view.LayoutInflater.onCreateView(LayoutInflater.java:682)
        在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
        在android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
        在android.view.LayoutInflater.inflate(LayoutInflater.java:504)
        在android.view.LayoutInflater.inflate(LayoutInflater.java:414)
        在com.example.starcitizen.fragment.HomeFragment.onCreateView(HomeFragment.java:18)
        在androidx.fragment.app.Fragment.performCreateView(Fragment.java:2600)
        在androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:881)
        在androidx.fragment.app.FragmentManagerImpl.addAddedFragments(FragmentManagerImpl.java:2100)
        在androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1874)
        在androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1830)
        在androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1727)
        在androidx.fragment.app.FragmentManagerImpl.dispatchStateChange(FragmentManagerImpl.java:2663)
        在androidx.fragment.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManagerImpl.java:2613)
        在androidx.fragment.app.Fragment.performActivityCreated(Fragment.java:2624)
        在androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:904)
        在androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1238)
        在androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1303)
        在androidx.fragment.app.FragmentManagerImpl.dispatchStateChange(FragmentManagerImpl.java:2659)
        在androidx.fragment.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManagerImpl.java:2613)
        在androidx.fragment.app.FragmentController.dispatchActivityCreated(FragmentController.java:246)
        在androidx.fragment.app.FragmentActivity.onStart(FragmentActivity.java:542)
        在androidx.appcompat.app.AppCompatActivity.onStart(AppCompatActivity.java:201)
        在android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1236)
        在android.app.Activity.performStart(Activity.java:6006)
        在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288)
        在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
        在android.app.ActivityThread.access $ 800(ActivityThread.java:151)
        在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1303)
        在android.os.Handler.dispatchMessage(Handler.java:102)
        在android.os.Looper.loop(Looper.java:135)
        在android.app.ActivityThread.main(ActivityThread.java:5254)
        在java.lang.reflect.Method.invoke(本机方法)
        在java.lang.reflect.Method.invoke(Method.java:372)
        在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:905)
        在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
     造成原因:android.content.res.Resources $ NotFoundException:字符串资源ID#0x2040003
        在android.content.res.Resources.getText(Resources.java:299)
        在android.content.res.Resources.getString(Resources.java:385)
        在com.android.org.chromium.content.browser.ContentViewCore



试图了解但失败了。

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />

<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />

</androidx.drawerlayout.widget.DrawerLayout>

最佳答案

WebViewActivity中使用Fragment没有什么区别。


我无法解决您的问题,但建议您检查以下内容。


检查清单中的权限


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



检查 RuntimeonAttach()中是否存在 HomeFragment异常。
如果要在 WebView中看到 Fragment,则应声明以下代码。


    view.setWebViewClient(new WebViewClient());



  更新资料


    final String source = "http://google.com";
final WebView webView = (WebView) rootView.findViewById(R.id.home_webview);
//webView.loadUrl(url);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
//Do some like progress bar loading here.

}

@Override
public void onPageFinished(WebView view, String url) {
webView.loadUrl(source);

}
});

关于java - 如何在 fragment 中使用Android Webview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58873369/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com