android - 片段中的 webview 场景问题
问题描述
安卓新手,如有错误请见谅
FiveFragment.java 的问题,其中mWebview变量用红色下划线,“无法解析符号 mWebview”
frament_five.xml:
<?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.ekatechhp.pkmapplication.FiveFragment">
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
片段五.java:
@SuppressLint("ValidFragment")
public class FiveFragment extends Fragment {
public FiveFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout. fragment_five, container, false);
mWebview = (WebView) view.findViewById(R.id.webview);
mWebview.loadUrl("https://google.com");
// Enable Javascript
WebSettings webSettings = mWebview.getSettings();
webSettings.setJavaScriptEnabled(true);
// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new WebViewClient());
return view;
}
}
解决方案
在你的程序中声明 WebView就像这个 WebView mWebview;
@SuppressLint("ValidFragment")
public class FiveFragment extends Fragment {
WebView mWebview ;//declare here
public FiveFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState) {
View view=inflater.inflate(R.layout. fragment_five, container, false);
mWebview = (WebView) view.findViewById(R.id.webview);
mWebview.loadUrl("https://google.com");
// Enable Javascript
WebSettings webSettings = mWebview.getSettings();
webSettings.setJavaScriptEnabled(true);
// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new WebViewClient());
return view;
}
}
并确保你导入这个
import android.webkit.WebView;
推荐阅读
- gtk - 我不能将 firejail keepasssc 与 Bionic Beaver 一起使用
- bash - 使用通配符提取部分字符串 - 替代解决方案
- javascript - 如何导航到选项卡导航内的单个屏幕。反应导航
- sql - 填补所有组的日期空白
- imagemagick - 更改图像中的背景(Imagemagick、GraphicsMagick、...)
- ios - 只想在 uitablelview 中返回 1 个单元格,但由于 json 响应,显示多个单元格
- r - 如何调试 R 脚本
- javascript - 反应上的比例模块d3问题
- android - 一部安卓手机可以连接多个蓝牙设备吗?
- javascript - 为什么从过滤方法返回的数组中为特定索引分配值 - 将直接修改数组