Android 片段中的 Studio Webview 不显示任何内容且为空
Android Studio Webview in a Fragment shows nothing and empty
我希望在 fragment_home_screen 中实现一个不覆盖 BottomNavigationView 的 webview。但是,即使我通过在 Fragment_Home.java onCreateView() 方法中设置 webview 来遵循在线教程中的所有内容。什么都没有出现。我尝试在 fragment_home_screen.xml 中添加一个按钮以确保片段 DID 出现。它最终只显示了按钮。
几个小时以来,我一直在尝试解决这个问题,但仍然无法弄清楚我的代码出了什么问题。有什么想法吗?
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android.exercise.myApplication">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<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.AppCompat.Light.NoActionBar">
<activity
android:name=".Activity_Map"
android:exported="false"
android:label="@string/title_activity_map" />
<activity
android:name=".Activity_Login"
android:exported="false" />
<activity
android:name=".Activity_SignUp"
android:exported="false" />
<activity
android:name=".Activity_Home"
android:exported="false"
android:label="Main" />
<activity
android:name=".SplashScreen"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Activity_Home.java
package android.exercise.myApplication;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;
public class Activity_Home extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
BottomNavigationView bottomNav = findViewById(R.id.bottomNavigationView);
bottomNav.setOnNavigationItemSelectedListener(navListener);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new HomeFragment()).commit();
}
private BottomNavigationView.OnNavigationItemSelectedListener navListener =
new BottomNavigationView.OnNavigationItemSelectedListener(){
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment selectFragment = null;
switch(item.getItemId()){
case R.id.homeScreen:
selectFragment = new HomeFragment();
break;
case R.id.userScreen:
selectFragment = new Fragment_User();
break;
case R.id.driverScreen:
selectFragment = new Fragment_Driver();
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, selectFragment).commit();
return true;
}
};
}
Fragment_Home.java
package android.exercise.myApplication;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
/**
* A simple {@link Fragment} subclass.
* Use the {@link Fragment_Home#newInstance} factory method to
* create an instance of this fragment.
*/
public class Fragment_Home extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
public Fragment_Home() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment Fragment_Home.
*/
// TODO: Rename and change types and number of parameters
public static Fragment_Home newInstance(String param1, String param2) {
Fragment_Home fragment = new Fragment_Home();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String url = "https://pastebin.com/";
View v = inflater.inflate(R.layout.fragment_home_screen, container, false);
WebView myWebview = (WebView) v.findViewById(R.id.webview);
myWebview.getSettings().setJavaScriptEnabled(true);
myWebview.getSettings().setDomStorageEnabled(true);
myWebview.setWebViewClient(new WebViewClient());
myWebview.loadUrl("https://en.wikipedia.org/");
myWebview.setWebViewClient(
new SSLWebViewClient()
);
myWebview.clearView();
myWebview.measure(100, 100);
myWebview.getSettings().setUseWideViewPort(true);
myWebview.getSettings().setLoadWithOverviewMode(true);
return v;
}
}
activity_main_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".Activity_Home" >
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/bottom_menu"/>
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/fragment_container"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
fragment_home_screen.xml
<?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"
tools:context=".Fragment_Home">
<WebView
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
</WebView>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintStart_toStartOf="@+id/webview"
app:layout_constraintTop_toTopOf="@+id/webview" />
</RelativeLayout>
至于logcat,经过警告过滤只有一行错误:
022-01-19 21:45:53.557 7053-7053/android.exercise.myApplication E/chromium: [ERROR:gl_surface_egl.cc(549)] eglChooseConfig failed with error EGL_SUCCESS
我试过你的代码,它工作正常你只需要注释掉 Fragment_Home.java 中的一些代码,如下所示:
@Override
public View onCreateView(....){
.....
..........
//remove or comment this part.
// myWebView.setWebViewClient(new SSLWebViewClient());
.....
}
我希望在 fragment_home_screen 中实现一个不覆盖 BottomNavigationView 的 webview。但是,即使我通过在 Fragment_Home.java onCreateView() 方法中设置 webview 来遵循在线教程中的所有内容。什么都没有出现。我尝试在 fragment_home_screen.xml 中添加一个按钮以确保片段 DID 出现。它最终只显示了按钮。 几个小时以来,我一直在尝试解决这个问题,但仍然无法弄清楚我的代码出了什么问题。有什么想法吗?
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android.exercise.myApplication">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<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.AppCompat.Light.NoActionBar">
<activity
android:name=".Activity_Map"
android:exported="false"
android:label="@string/title_activity_map" />
<activity
android:name=".Activity_Login"
android:exported="false" />
<activity
android:name=".Activity_SignUp"
android:exported="false" />
<activity
android:name=".Activity_Home"
android:exported="false"
android:label="Main" />
<activity
android:name=".SplashScreen"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Activity_Home.java
package android.exercise.myApplication;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;
public class Activity_Home extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
BottomNavigationView bottomNav = findViewById(R.id.bottomNavigationView);
bottomNav.setOnNavigationItemSelectedListener(navListener);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new HomeFragment()).commit();
}
private BottomNavigationView.OnNavigationItemSelectedListener navListener =
new BottomNavigationView.OnNavigationItemSelectedListener(){
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
Fragment selectFragment = null;
switch(item.getItemId()){
case R.id.homeScreen:
selectFragment = new HomeFragment();
break;
case R.id.userScreen:
selectFragment = new Fragment_User();
break;
case R.id.driverScreen:
selectFragment = new Fragment_Driver();
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, selectFragment).commit();
return true;
}
};
}
Fragment_Home.java
package android.exercise.myApplication;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
/**
* A simple {@link Fragment} subclass.
* Use the {@link Fragment_Home#newInstance} factory method to
* create an instance of this fragment.
*/
public class Fragment_Home extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
public Fragment_Home() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment Fragment_Home.
*/
// TODO: Rename and change types and number of parameters
public static Fragment_Home newInstance(String param1, String param2) {
Fragment_Home fragment = new Fragment_Home();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String url = "https://pastebin.com/";
View v = inflater.inflate(R.layout.fragment_home_screen, container, false);
WebView myWebview = (WebView) v.findViewById(R.id.webview);
myWebview.getSettings().setJavaScriptEnabled(true);
myWebview.getSettings().setDomStorageEnabled(true);
myWebview.setWebViewClient(new WebViewClient());
myWebview.loadUrl("https://en.wikipedia.org/");
myWebview.setWebViewClient(
new SSLWebViewClient()
);
myWebview.clearView();
myWebview.measure(100, 100);
myWebview.getSettings().setUseWideViewPort(true);
myWebview.getSettings().setLoadWithOverviewMode(true);
return v;
}
}
activity_main_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".Activity_Home" >
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/bottom_menu"/>
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/fragment_container"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
fragment_home_screen.xml
<?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"
tools:context=".Fragment_Home">
<WebView
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
</WebView>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintStart_toStartOf="@+id/webview"
app:layout_constraintTop_toTopOf="@+id/webview" />
</RelativeLayout>
至于logcat,经过警告过滤只有一行错误:
022-01-19 21:45:53.557 7053-7053/android.exercise.myApplication E/chromium: [ERROR:gl_surface_egl.cc(549)] eglChooseConfig failed with error EGL_SUCCESS
我试过你的代码,它工作正常你只需要注释掉 Fragment_Home.java 中的一些代码,如下所示:
@Override
public View onCreateView(....){
.....
..........
//remove or comment this part.
// myWebView.setWebViewClient(new SSLWebViewClient());
.....
}