0

I have a Samsung Galaxy S3 running android 4.1.2 touchwiz + 4.2.2 cm10.1 in dual boot. Now, the problem is that my app works perfectly on cm10.1 but crashes and gives NPE on touchwiz rom as well as on any AVD I run. Its a long code so I don't want to put up everything. Here is the logcat and the line of code where its gives this exception. Thanks for the help.

public class Fragment2 extends Fragment {

static final LatLng abcd = new LatLng(xx, xx);
String addresss;
private GoogleMap map;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    View rootView = inflater.inflate(R.layout.fragment_fragment2,
            container, false);
    map = ((SupportMapFragment) getFragmentManager().findFragmentById(
            R.id.map)).getMap();


   **NPE here** 
    Marker abcd = map.addMarker(new MarkerOptions().position(abcd)); 

    map.moveCamera(CameraUpdateFactory.newLatLngZoom(abcd, 15));

    map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);

logcat:-

06-04 09:18:48.714: E/AndroidRuntime(816): FATAL EXCEPTION: main
06-04 09:18:48.714: E/AndroidRuntime(816): java.lang.NullPointerException
06-04 09:18:48.714: E/AndroidRuntime(816):  at com.example.mapsapiv2demo.Fragment2.onCreateView(Fragment2.java:49)
06-04 09:18:48.714: E/AndroidRuntime(816):  at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
06-04 09:18:48.714: E/AndroidRuntime(816):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
06-04 09:18:48.714: E/AndroidRuntime(816):  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
06-04 09:18:48.714: E/AndroidRuntime(816):  at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
06-04 09:18:48.714: E/AndroidRuntime(816):  at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
06-04 09:18:48.714: E/AndroidRuntime(816):  at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)
06-04 09:18:48.714: E/AndroidRuntime(816):  at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
06-04 09:18:48.714: E/AndroidRuntime(816):  at android.support.v4.view.ViewPager.populate(ViewPager.java:1064)
06-04 09:18:48.714: E/AndroidRuntime(816):  at android.support.v4.view.ViewPager.populate(ViewPager.java:911)
06-04 09:18:48.714: E/AndroidRuntime(816):  at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1432)
06-04 09:18:48.714: E/AndroidRuntime(816):  at android.view.View.measure(View.java:15518)
06-04 09:18:48.714: E/AndroidRuntime(816):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
06-04 09:18:48.714: E/AndroidRuntime(816):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-04 09:18:48.714: E/AndroidRuntime(816):  at android.view.View.measure(View.java:15518)
06-04 09:18:48.714: E/AndroidRuntime(816):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
06-04 09:18:48.714: E/AndroidRuntime(816):  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
06-04 09:18:48.714: E/AndroidRuntime(816):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
06-04 09:18:48.714: E/AndroidRuntime(816):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
06-04 09:18:48.714: E/AndroidRuntime(816):  at android.view.View.measure(View.java:15518)
06-04 09:18:48.714: E/AndroidRuntime(816):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
06-04 09:18:48.714: E/AndroidRuntime(816):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
06-04 09:18:48.714: E/AndroidRuntime(816):  at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
06-04 09:18:48.714: E/AndroidRuntime(816):  at android.view.View.measure(View.java:15518)
06-04 09:18:48.714: E/AndroidRuntime(816):  at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
06-04 09:18:48.714: E/AndroidRuntime(816):  at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
06-04 09:18:48.714: E/AndroidRuntime(816):  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
06-04 09:18:48.714: E/AndroidRuntime(816):  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
06-04 09:18:48.714: E/AndroidRuntime(816):  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
06-04 09:18:48.714: E/AndroidRuntime(816):  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
06-04 09:18:48.714: E/AndroidRuntime(816):  at android.view.Choreographer.doCallbacks(Choreographer.java:562)
06-04 09:18:48.714: E/AndroidRuntime(816):  at android.view.Choreographer.doFrame(Choreographer.java:532)
06-04 09:18:48.714: E/AndroidRuntime(816):  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
06-04 09:18:48.714: E/AndroidRuntime(816):  at android.os.Handler.handleCallback(Handler.java:725)
06-04 09:18:48.714: E/AndroidRuntime(816):  at android.os.Handler.dispatchMessage(Handler.java:92)
06-04 09:18:48.714: E/AndroidRuntime(816):  at android.os.Looper.loop(Looper.java:137)
06-04 09:18:48.714: E/AndroidRuntime(816):  at android.app.ActivityThread.main(ActivityThread.java:5041)
06-04 09:18:48.714: E/AndroidRuntime(816):  at java.lang.reflect.Method.invokeNative(Native Method)
06-04 09:18:48.714: E/AndroidRuntime(816):  at java.lang.reflect.Method.invoke(Method.java:511)
06-04 09:18:48.714: E/AndroidRuntime(816):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-04 09:18:48.714: E/AndroidRuntime(816):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-04 09:18:48.714: E/AndroidRuntime(816):  at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

1

似乎这与SupportMapFragment.getMap()来自 API docs 的 (重点是我的)有关:

public final GoogleMap getMap()

获取与此片段包装的视图相关联的基础 GoogleMap。

返回谷歌地图。如果片段的视图尚未准备好,则为 Null。如果片段生命周期尚未通过 onCreateView(LayoutInflater, ViewGroup, Bundle) 可能会发生这种情况。如果 Google Play 服务不可用,也会发生这种情况。如果之后 Google Play 服务可用且 Fragment 已通过 onCreateView(LayoutInflater, ViewGroup, Bundle),再次调用该方法将初始化并返回 GoogleMap。

运行 Android 4.1.2 TouchWiz 的 Galaxy S3 可能无法使用 Google Play 服务。

于 2013-06-04T09:44:23.903 回答