Android谷歌地图v2 - 支持片段null poiner异常

时间:2015-02-07 14:27:52

标签: android android-fragments android-support-library android-fragmentactivity android-maps-v2

在我的Android应用程序中,我在片段中有谷歌地图,但我在下一行得到空指针异常。我已经提到了许多关于支持片段空指针异常的堆栈流,但我无法解决我的问题。

myMAPF.getMap(); // Null pointer error occurred line.

这是我在片段实现中的完整地图代码。

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;

public class MapFragment2 extends SupportMapFragment {

    private GoogleMap googleMap;

    // latitude and longitude
    double latitude = 17.385044;
    double longitude = 78.486671;

    static final LatLng HAMBURG = new LatLng(53.558, 9.927);
    static final LatLng KIEL = new LatLng(53.551, 9.993);

    private FragmentManager myFM;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.location_fragment, container,
                false);
        myFM = getActivity().getSupportFragmentManager();

        if (googleMap == null) {
            final SupportMapFragment myMAPF = (SupportMapFragment) myFM
                    .findFragmentById(R.id.location_map);
            if (myMAPF == null) {
                Log.e("myMAPF", "null");
            } else {
                Log.e("myMAPF", "NOT null");
            }
            myMAPF.getMap();

            if (googleMap == null) {
                Toast.makeText(getActivity().getApplicationContext(),
                        "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                        .show();
            }
        }

        return view;
    }
}

XML布局:(location_fragment.xml)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp" >

    <Button
        android:id="@+id/location_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="map button" />

    <fragment
        android:id="@+id/location_map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="10dp"
        class="com.google.android.gms.maps.SupportMapFragment" />

</LinearLayout>

我的FragmentActivity类定义如下:

    import android.support.v4.app.FragmentActivity;
    import android.support.v4.app.FragmentManager;
    import android.support.v4.app.FragmentTabHost;
    .......

    public class FragActivity extends FragmentActivity implements
            OnTabChangeListener {
        private static FragmentTabHost mTabHost;
        // This activity has 3 tabs one of the tab has map
// ...............
    }

Posted full Logcat:

java.lang.NullPointerException
at in.murali.pupdemo.MapFragment2.onCreateView(MapFragment2.java:44)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:486)
at android.support.v4.app.FragmentTabHost.onAttachedToWindow(FragmentTabHost.java:283)
at android.view.View.dispatchAttachedToWindow(View.java:13418)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2653)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2660)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2660)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2660)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2660)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1476)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1239)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6486)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:803)
at android.view.Choreographer.doCallbacks(Choreographer.java:603)
at android.view.Choreographer.doFrame(Choreographer.java:573)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:789)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)

我的应用程序在libs文件夹中有android-support-v4.jar,我的应用程序参考如下图所示。

enter image description here

我已经在manifest xml中提供了所有必要的权限和密钥。

1 个答案:

答案 0 :(得分:0)

查看此代码

 import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;

public class MapFragment2 extends SupportMapFragment {

    private GoogleMap googleMap;

    // latitude and longitude
    double latitude = 17.385044;
    double longitude = 78.486671;

    static final LatLng HAMBURG = new LatLng(53.558, 9.927);
    static final LatLng KIEL = new LatLng(53.551, 9.993);

    private FragmentManager myFM;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View view = inflater.inflate(R.layout.location_fragment, container,
                false);
        myFM = getActivity().getSupportFragmentManager();

        if (googleMap == null) {

       final MapView myMAPF = (MapView) view.findViewByIdR.id.location_map);     

       myMAPF.onCreate(savedInstanceState); 
      myMAPF.onResume();
            if (myMAPF == null) {
                Log.e("myMAPF", "null");
            } else {
                Log.e("myMAPF", "NOT null");
            }

          googleMap=myMAPF.getMap();

            if (googleMap == null) {
                Toast.makeText(getActivity().getApplicationContext(),
                        "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                        .show();
            }
        }

        return view;
    }
}