Мне действительно трудно понять это. Когда я смотрел, где LogCat указывает ошибку, он указывает на этой линии:runtimeexception: невозможно уничтожить активность и RuntimeException: .nosuchfieldexception: mChildfragmentManager
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);
}
}
Это происходит, когда я изменил альбомной ориентации. Я знаю, что представление воссоздается после смены ориентации, я смог сделать это на своих Google-картах и в этом приложении, но я не знаю, как его реализовать на веб-просмотре. У меня есть веб-просмотр, который показывает facebook sharer.php, и когда я меняю его на ландшафтную ориентацию, он останавливается.
Это мой код для этой вкладки:
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);
}
}
}
GoogleMap Tab
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);
}
}
}
Пожалуйста, помогите. Благодаря!
переменная mChildFragmentManager существует в классе Фрагмент? –
Я не понял, что вы имеете в виду. Я также использовал этот метод на моей вкладке карты для обработки изменений ориентации. – Jeongbebs
означает, что вы пытаетесь получить доступ к переменной поля с именем mChildFragmentManager, которая недоступна в классе фрагментов –