我对 fragment 中的 WebView 有疑问。当我执行应用程序时,webview 不显示 Html 页面。这是我的代码:
public class BingMapFragment 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";
private View main_view;
private static View v;
private WebView webMapView;
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
ListView lv;
TraceAdapter aAdpt;
private FragmentChangeActivity parent;
private OnFragmentInteractionListener mListener;
public BingMapFragment() {
// Required empty public constructor
setRetainInstance(true);
}
// TODO: Rename and change types and number of parameters
public static BingMapFragment newInstance(String param1, String param2) {
BingMapFragment fragment = new BingMapFragment();
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);
webMapView = (WebView)main_view.findViewById(R.id.bingMapView);
webMapView.setWebViewClient(new WebViewClient());
WebSettings ws = webMapView.getSettings();
ws.setJavaScriptEnabled(true);
webMapView.loadUrl("file:///android_asset/prova.html");
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
new DataSourceDeviceRealTime().getDeviceRealTime(this);
main_view = inflater.inflate(R.layout.fragment_bing_map,container,false);
v=main_view;
parent=(FragmentChangeActivity) getActivity();
parent.setupActionBar(Constants.SCREEN_MAP);
return inflater.inflate(R.layout.fragment_bing_map, container, false);
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
public void goBack() {
if(GlobalData.isSalesforcePolicyActive() || GlobalData.isMobileRoutePolicyActive()){
parent.switchContent(new MenuFragment(), true);
} else {
parent.logoutButtonClicked();
}
}
}
我不知道问题的根源,因为我已经在另一个应用程序中使用了 webview。谢谢你的回复。
最佳答案
您正在 onCreate 方法中调用 loadUrl(),但 webview 在 onCreateView 中膨胀,它在 onCreate 之后调用 - 因此这可能会导致一些异常。
在您的 onCreateView 方法中,您对布局进行了两次扩充 - 第一次对布局进行扩充并找到 WebView 实例,但您返回的是新扩充的布局。返回的布局应用于 fragment ...
将 return main_view
放在 onCreateView() 的末尾而不是 inflater.inflate...
webMapView = (WebView)main_view.findViewById(R.id.bingMapView);
也应该在 onCreateView
中调用
一些其他注意事项:
- 您应该在
onStart
或onPause
开始加载 webview 数据 - 要附加您应该使用
onAttach
方法的 Activity
关于android - fragment Android Studio 中的 Webview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44455800/