2013-02-15 81 views
2

嘗試爲手機創建ListFragment - > MapFragment活動。 MapFragment代碼如下:MapFragment錯誤 - CameraUpdateFactory未初始化

public class MapFragment extends SupportMapFragment { 
    private GoogleMap mMap; 

    @Override 
    public void onCreate(Bundle bundle){ 
     super.onCreate(bundle); 

     try { 
      MapsInitializer.initialize(this.getActivity()); 
     } catch (GooglePlayServicesNotAvailableException e){ 
      e.printStackTrace(); 
     } 

     setRetainInstance(true); 
    } 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState){ 
     super.onActivityCreated(savedInstanceState); 
     if(getMap() != null){ 
      Log.v(TAG, "Map ready for use!"); 
      mMap = getMap(); 
     } 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     View root = super.onCreateView(inflater, container, savedInstanceState); 
     return root; 
    } 

    public void animateTo(double lat, double lng){ 
      Log.d(TAG, "mMap animating..."); 
      LatLng mCurrentPosition = new LatLng(lat, lng); 
      mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(mCurrentPosition, 16)); 
    } 
} 

,但我得到了下面的錯誤---

02-15 17:12:53.232: E/AndroidRuntime(6913): FATAL EXCEPTION: main 
02-15 17:12:53.232: E/AndroidRuntime(6913): java.lang.NullPointerException: CameraUpdateFactory is not initialized 
02-15 17:12:53.232: E/AndroidRuntime(6913):  at com.google.android.gms.internal.at.a(Unknown Source) 
02-15 17:12:53.232: E/AndroidRuntime(6913):  at com.google.android.gms.maps.CameraUpdateFactory.J(Unknown Source) 
02-15 17:12:53.232: E/AndroidRuntime(6913):  at com.google.android.gms.maps.CameraUpdateFactory.newLatLngZoom(Unknown Source) 
02-15 17:12:53.232: E/AndroidRuntime(6913):  at com.test.app.MapFragment.animateTo(MapFragment.java:113) 
02-15 17:12:53.232: E/AndroidRuntime(6913):  at com.test.app.MapActivity.onListItemClick(MapActivity.java:270) 
02-15 17:12:53.232: E/AndroidRuntime(6913):  at com.test.app.SimpleListFragment.onListItemClick(SimpleListFragment.java:65) 
02-15 17:12:53.232: E/AndroidRuntime(6913):  at android.support.v4.app.ListFragment$2.onItemClick(ListFragment.java:58) 
02-15 17:12:53.232: E/AndroidRuntime(6913):  at android.widget.AdapterView.performItemClick(AdapterView.java:298) 
02-15 17:12:53.232: E/AndroidRuntime(6913):  at android.widget.AbsListView.performItemClick(AbsListView.java:1100) 
02-15 17:12:53.232: E/AndroidRuntime(6913):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749) 
02-15 17:12:53.232: E/AndroidRuntime(6913):  at android.widget.AbsListView$1.run(AbsListView.java:3423) 
02-15 17:12:53.232: E/AndroidRuntime(6913):  at android.os.Handler.handleCallback(Handler.java:725) 
02-15 17:12:53.232: E/AndroidRuntime(6913):  at android.os.Handler.dispatchMessage(Handler.java:92) 
02-15 17:12:53.232: E/AndroidRuntime(6913):  at android.os.Looper.loop(Looper.java:137) 
02-15 17:12:53.232: E/AndroidRuntime(6913):  at android.app.ActivityThread.main(ActivityThread.java:5039) 
02-15 17:12:53.232: E/AndroidRuntime(6913):  at java.lang.reflect.Method.invokeNative(Native Method) 
02-15 17:12:53.232: E/AndroidRuntime(6913):  at java.lang.reflect.Method.invoke(Method.java:511) 
02-15 17:12:53.232: E/AndroidRuntime(6913):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
02-15 17:12:53.232: E/AndroidRuntime(6913):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
02-15 17:12:53.232: E/AndroidRuntime(6913):  at dalvik.system.NativeStart.main(Native Method) 

我想調用MapInitializer將修復它,但似乎並沒有這樣的伎倆。這是否與調用getMap()方法的時間有關?

+0

確定'mMap' ISN在這一點上是否爲null?對'CameraUpdateFactory.newLatLngZoom'的調用將首先進行,因此您不會在'mMap'上看到NPE - 但這會解釋看到此錯誤。 – 2013-02-15 22:58:30

+0

在我的主要活動中,我稱'MapFragment mFragment = new MapFragment()',我認爲這意味着mMap不會是空的。也許這是一個不正確的假設?在調用之後,我使用'FragmentTransaction'來嘗試替換我的'FrameLayout',其id爲'content_frame',但它一直給我NPE。 – codedawg82 2013-02-15 23:07:57

+0

那麼,你有一個日誌調用在那裏 - 「地圖準備好使用!」在異常之前的任何地方出現在日誌中? – 2013-02-15 23:18:21

回答

1

這裏改變了實施。使用了一個新的佈局,該佈局既具有ListFragment的片段類,又具有FrameLayout中的MapFragment。然後通過它們的FragmentIds找到這些內容,然後使用show/hide結合添加到後端堆棧以使地圖/列表顯示給用戶。我不知道什麼開銷這招使用兩套片段被顯示/隱藏,但似乎在Nexus4 :)

代碼可以在這裏很好地工作: https://github.com/codedawg82/AndroidFragments/

+0

上面的鏈接被打破了 – Chaitu 2013-10-22 09:56:58

+0

對不起,更新了這個爲雅。 – codedawg82 2013-10-28 20:05:55