2015-10-23 177 views
-2

我有一個機器人的應用程序,由多個嵌套片段的fragements,例如我有一組選項卡並點擊一個標籤加載片段插入到內容區。嵌套片段 - 不能保持嵌套在其它片段

我的一個標籤是表示其位置的地圖,我在郵政編碼找回座標爲它做一個地理查詢。

這是所有的工作,現在終於我需要確保當用戶點擊地圖選項卡,並啓動異步任務,確實地理查找到設備旋轉犯規阻止這一切。其上裝載被點擊的標籤時

我的地圖片段:

public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    View rootView = (View)inflater.inflate(R.layout.fragment_locations, container, false); 

    // Make sure user's device supports Google play services 
    try { 
     MapsInitializer.initialize(getActivity()); 
     mapView = (MapView) rootView.findViewById(R.id.mapView); 
     mapView.onCreate(savedInstanceState); 
     googleMap = mapView.getMap(); 
     Log.i(LOG_TAG, "Google play service is available."); 

     if(googleMap == null) { 
      Toast.makeText(getActivity().getApplicationContext(), "Problem creating Google map", Toast.LENGTH_LONG).show(); 
     } else { 
      googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
       googleMap.getUiSettings().setZoomControlsEnabled(true);      

      FragmentManager fm = getFragmentManager(); 
      mRetainedTaskFragment = (LocationNetworkOperationsRetainedFragment) fm.findFragmentByTag(TAG_RETAINED_TASK_FRAGMENT); 

      // If the Fragment is non-null, then it is currently being 
      // retained across a configuration change. 
      if (mRetainedTaskFragment == null) { 
       mRetainedTaskFragment = new LocationNetworkOperationsRetainedFragment(); 
       fm.beginTransaction().add(mRetainedTaskFragment, TAG_RETAINED_TASK_FRAGMENT).commit(); 
      } 
     } 
    } catch (Exception e) { 
     Log.e(LOG_TAG, "Google play service is not available."); 
     Toast.makeText(getActivity().getApplicationContext(), "Google play service is not available", Toast.LENGTH_LONG).show(); 
    } 

    return rootView; 
} 

我的異步任務:

public class LocationNetworkOperations extends AsyncTask<String, Void, LatLng> { 

    @Override 
    protected LatLng doInBackground(String... postcodes) { 
     Geocoder geocoder = new Geocoder(getActivity().getApplicationContext()); 
     LatLng coords= null; 

     try { 
      List<Address> addresses = geocoder.getFromLocationName(postcodes[0], 1); 

      if (addresses != null && !addresses.isEmpty()) { 
       Address address = addresses.get(0); 
       coords = new LatLng(address.getLatitude(), address.getLongitude()); 
      } else { 
       Toast.makeText(getActivity().getApplicationContext(), "Unable to geocode postcode" + postcodes[0], Toast.LENGTH_LONG).show(); 
      } 
     } catch(Exception ioe) { 
      ioe.printStackTrace(); 
     } 

     return coords; 
    } 

    @Override 
    protected void onPostExecute(LatLng coords) { 
     if(coords != null) { 
      Marker mapMarkerHome = googleMap.addMarker(new MarkerOptions().position(coords).title("Home") 
        .snippet("E10 6JQ").icon(BitmapDescriptorFactory 
          .fromResource(R.drawable.home_map_marker))); 
      CameraPosition cameraPosition = new CameraPosition.Builder().target(coords).zoom(15).build(); 
      googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); 
     } 
    } 

    @Override 
    protected void onPreExecute() {} 

    @Override 
    protected void onProgressUpdate(Void... values) {} 
} 

保留的片段:

public class LocationNetworkOperationsRetainedFragment extends Fragment { 

interface LocationNetworkOperationsTaskCallbacks { 
    void onPreExecute(); 
    void onProgressUpdate(int percent); 
    void onCancelled(); 
    void onPostExecute(); 
} 

private LocationNetworkOperationsTaskCallbacks mCallbacks; 
private LocationsFragment.LocationNetworkOperations mTask; 

@Override 
public void onAttach(Context context) { 
    super.onAttach(context); 
    mCallbacks = (LocationNetworkOperationsTaskCallbacks) context; 
} 

/** 
* This method will only be called once when the retained 
* Fragment is first created. 
*/ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    // Retain this fragment across configuration changes. 
    setRetainInstance(true); 

    // Create and execute the background task. 
    mTask = new LocationsFragment().new LocationNetworkOperations(); 
    mTask.execute("E106JQ"); 
} 

@Override 
public void onDetach() { 
    super.onDetach(); 
    mCallbacks = null; 
} 

}

所以我跟着一個教程,並寫了一保留片段和在揭開序幕aysnc的任務,但我遇到以下情況除外:

java.lang.IllegalStateException: Can't retain fragements that are nested in other fragments 
     at android.support.v4.app.Fragment.setRetainInstance(Fragment.java:820) 
     at com.example.android.LocationNetworkOperationsRetainedFragment.onCreate(LocationNetworkOperationsRetainedFragment.java:37) 
     at android.support.v4.app.Fragment.performCreate(Fragment.java:1939) 
     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:988) 
     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1207) 
     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738) 
     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1572) 
     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:493) 
     at android.os.Handler.handleCallback(Handler.java:739) 
     at android.os.Handler.dispatchMessage(Handler.java:95) 
     at android.os.Looper.loop(Looper.java:148) 
     at android.app.ActivityThread.main(ActivityThread.java:5417) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

爲什麼不可能的,如果它嵌套保留片段,是有這個解決方案?

+0

你能顯示代碼? –

+0

您是否嘗試使用Google搜索結果中的第一個解決方案? – Selvin

+0

[Android無法保留嵌套在其他片段中的片段]的可能重複(http://stackoverflow.com/questions/25520705/android-cant-retain-fragments-that-are-nested-in-other-fragments ) –

回答

-1

Can't retain fragements that are nested in other fragments

這是嵌套片段的限制。我猜測你的一個或多個子碎片在代碼中的某處設置了setRetainInstance(true)。您需要將其刪除以防止出現錯誤。

+0

[...我討厭偷...](http://stackoverflow.com/questions/25520705/android-cant-retain-fragments-that-are-nested-in-other-fragments#answer-25520822) – Selvin

+0

我只是想幫助他。顯然他不能谷歌。 –

+0

我沒有谷歌...很好的答案,我已經閱讀過,是的其中一個沒有...其rthe保留片段... – berimbolo