我真的很难弄清楚这一点。当我查看 logcat 指向错误的位置时,它指向这一行:
throw new RuntimeException(i);
在这里面
public void onDetach() {
super.onDetach();
try {
Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
childFragmentManager.setAccessible(true);
childFragmentManager.set(this, null);
} catch (NoSuchFieldException i) {
throw new RuntimeException(i);
} catch (IllegalAccessException i) {
throw new RuntimeException(i);
}
}
当我更改为横向时会发生这种情况。我知道在改变方向后正在重新创建视图,我也可以在这个应用程序中的谷歌地图上执行此操作,但我不知道如何在 webview 上实现它。我有一个显示 facebook sharer.php 的 webview,当我将其更改为横向时,它会强制停止。
这是此选项卡的代码:
public class TabFour extends Fragment {
static WebView webView;
String myBlogAddr = "http://192.168.1.75/Treasuria-v2/";
String myUrl;
Button map;
View rootView;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.activity_tab_four, container, false);
displayWebview();
return rootView;
}
protected void displayWebview(){
webView= (WebView)rootView.findViewById(R.id.webView);
webView.setWebViewClient(new MyWebViewClient());
String title = "Quezon City Guide";
String desc = "Your Number One City Guide!";
String image = "http://s22.postimg.org/5t4qidqpt/launcher114x114.png";
myBlogAddr ="http://www.facebook.com/sharer.php?m2w&s=100&p[title]="+title+"&p[summary]="+desc+"&p[url]=http://www.sample.com&p[images][0]="+image;
if(myUrl == null){
myUrl = myBlogAddr;
}
webView.loadUrl(myUrl);
Log.i("URL", myBlogAddr);
map = (Button) rootView.findViewById(R.id.buttonMap);
map.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
getActivity().getActionBar().setSelectedNavigationItem(2);
}
});
}
private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
myUrl = url;
view.loadUrl(url);
return true;
}
}
public void onBackPressed() {
if(webView.canGoBack())
webView.goBack();
}
public void OnCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
getActivity().getActionBar().setSelectedNavigationItem(1);
map = (Button) rootView.findViewById(R.id.buttonMap);
map.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
getActivity().getActionBar().setSelectedNavigationItem(2);
}
});
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
}
public void onDetach() {
super.onDetach();
try {
Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
childFragmentManager.setAccessible(true);
childFragmentManager.set(this, null);
} catch (NoSuchFieldException i) {
throw new RuntimeException(i);
} catch (IllegalAccessException i) {
throw new RuntimeException(i);
}
}
}
谷歌地图标签
public class TabTwo extends Fragment {
private static View view;
private static GoogleMap map;
private static Double latitude, longitude;
private static LatLngBounds QC = new LatLngBounds(
new LatLng(14.656669, 120.998598), new LatLng(14.666965, 121.098934));
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.activity_tab_two, null);
// Passing hard coded values for latitude & longitude. Please change as per your need. This is just used to drop a Marker on the Map
latitude = 14.6353475;
longitude = 121.0327501;
map = ((SupportMapFragment) MainActivity.fragmentManager
.findFragmentById(R.id.map)).getMap();
setUpMapIfNeeded(); // For setting up the MapFragment
return view;
}
/***** Sets up the map if it is possible to do so *****/
public static void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the map.
if (map == null) {
// Try to obtain the map from the SupportMapFragment.
map = ((SupportMapFragment) MainActivity.fragmentManager
.findFragmentById(R.id.map)).getMap();
// Check if we were successful in obtaining the map.
if (map != null)
setUpMap();
}
}
/**
* This is where we can add markers or lines, add listeners or move the
* camera.
* <p>
* This should only be called once and when we are sure that {@link #mMap}
* is not null.
*/
private static void setUpMap() {
// For showing a move to my location button
map = ((SupportMapFragment) MainActivity.fragmentManager
.findFragmentById(R.id.map)).getMap();
map.setMyLocationEnabled(true);
map.getUiSettings().setCompassEnabled(true);
map.getUiSettings().setRotateGesturesEnabled(true);
// For dropping a marker at a point on the Map
map.addMarker(new MarkerOptions().position(new LatLng(latitude, longitude)).title("Estuar Building").snippet("Work Place"));
// For zooming automatically to the Dropped PIN Location
map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude,
longitude), 12.0f));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(QC.getCenter(), 12));
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// TODO Auto-generated method stub
if (map != null)
setUpMap();
if (map == null) {
// Try to obtain the map from the SupportMapFragment.
map = ((SupportMapFragment) MainActivity.fragmentManager
.findFragmentById(R.id.map)).getMap();
// Check if we were successful in obtaining the map.
if (map != null)
setUpMap();
}
}
/**** The mapfragment's id must be removed from the FragmentManager
**** or else if the same it is passed on the next time then
**** application will crash ****/
@Override
public void onDestroyView() {
super.onDestroyView();
try {
Fragment fragment = (getFragmentManager().findFragmentById(R.id.map));
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.remove(fragment);
ft.commit();
setUpMap();
} catch (Exception e) {
e.printStackTrace();
}
}
public void onDetach() {
super.onDetach();
try {
Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
childFragmentManager.setAccessible(true);
childFragmentManager.set(this, null);
} catch (NoSuchFieldException i) {
throw new RuntimeException(i);
} catch (IllegalAccessException i) {
throw new RuntimeException(i);
}
}
}
请帮忙。谢谢!