2013-10-24 91 views
1

我真的很難搞清楚這一點。當我看着在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地圖上這樣做,但是我不知道如何在web視圖上實現它。我有一個顯示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); 
    } 
} 

} 

GoogleMap的標籤

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); 
    } 
} 
} 

請幫助。謝謝!

+0

是片段類mChildFragmentManager變量存在? –

+0

我沒有明白你的意思。我也在我的地圖選項卡中使用了這種方法來處理方向更改 – Jeongbebs

+0

意思是說,您正在嘗試訪問名爲mChildFragmentManager的字段變量,這在Fragment類中不可用 –

回答

1

試試這個...

public void onDetach() { 
super.onDetach(); 
try { 
    Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager"); 
    childFragmentManager.setAccessible(true); 
    childFragmentManager.set(this, null); 
} catch (NoSuchFieldException i) { 
    } 
catch (IllegalAccessException i) { 
} 

}

+0

此錯誤正在Android開源中進行跟蹤問題跟蹤:https://code.google.com/p/android/issues/detail?id=42601 –

+0

@KristopherJohnson感謝您對此的高度關注...... –

相關問題