首頁>Program>source

所以我有一个包含地圖片段的空片段.每当我尝試啟用包含地圖的片段時,我的應用程式都会崩潰並在此行上返迴空指標錯誤:

map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
                .getMap();

完整錯誤訊息:

java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.GoogleMap com.google.android.gms.maps.MapFragment.getMap()' on a null object reference
            at com.collusion.serviceassistant.ReturnVisitDetails.onViewCreated(ReturnVisitDetails.java:39)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:904)
            at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
            at android.app.BackStackRecord.run(BackStackRecord.java:684)
            at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
            at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5001)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)

這是我的片段代碼:

import android.app.Activity;
import android.app.Dialog;
import android.net.Uri;
import android.os.Bundle;
import android.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

public class ReturnVisitDetails extends Fragment {
    static final LatLng HAMBURG = new LatLng(53.558, 9.927);
    static final LatLng KIEL = new LatLng(53.551, 9.993);
    private GoogleMap map;
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_return_visit_add, container, false);
        return rootView;
    }
    public void onViewCreated(View view, Bundle savedInstanceState)
    {
        // Get a handle to the Map Fragment
        map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
                .getMap();
        Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
                .title("Hamburg"));
        Marker kiel = map.addMarker(new MarkerOptions()
                .position(KIEL)
                .title("Kiel")
                .snippet("Kiel is cool")
                .icon(BitmapDescriptorFactory
                        .fromResource(R.drawable.ic_launcher)));
        // Move the camera instantly to hamburg with a zoom of 15.
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));
        // Zoom in, animating the camera.
        map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
    }
}

還有我的父片段XML佈局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.collusion.serviceassistant.ReturnVisitDetails"
    android:background="#009688">

    <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.MapFragment" />
</RelativeLayout>

有人知道為什麼地圖片段返迴null吗?

編輯:所以現在我有以下代碼来初始化地圖:

MapFragment mapFragment = new MapFragment();
        FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
        transaction.add(R.id.rl_map_container, mapFragment).commit();
        map = mapFragment.getMap();
        Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG)
                .title("Hamburg"));
        Marker kiel = map.addMarker(new MarkerOptions()
                .position(KIEL)
                .title("Kiel")
                .snippet("Kiel is cool")
                .icon(BitmapDescriptorFactory
                        .fromResource(R.drawable.ic_launcher)));
        // Move the camera instantly to hamburg with a zoom of 15.
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));
        // Zoom in, animating the camera.
        map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);

它仍然返迴空指標值

最新回復
  • 5月前
    1 #

    当我尝試將Eclipse專案移植到Android Studio專案時,發生了此問题。

    java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.GoogleMap com.google.android.gms.maps.SupportMapFragment.getMap()' on a null object reference
    

    我將其与Fragment Activity一起使用,如下所示:

    ((SupportMapFragment) getActivity().getSupportFragmentManager().findFragmentById(R.id.mapToday)).getMap();
    

    但是,問题是,我之前使用的是特定版本的Google Play服務(4.3.23)。

    但是Gradle構建会選擇最新版本( compile 'com.google.android.gms:play-services:+')

    我尝試了其他一些特定版本,例如(6.1.71).但是仍然给出了空值異常。

    我使用了与以前相同的旧版本,並且可以正常工作。

    compile 'com.google.android.gms:play-services:4.3.23'
    

    這對我来說是一个臨時解決方案.但是,当我需要更新到 新的播放服務庫。

    似乎没有特定版本的地圖庫可用於Gradle構建 当我這樣尝試時:

    compile 'com.google.android.gms:play-services-maps:4.3.23'
    

  • 5月前
    2 #

    我已经解決了這个問题。 当我有一个viewpager時,此行:

    mMap = ((SupportMapFragment) MainActivity.fragmentManager
                    .findFragmentById(R.id.mapView)).getMap();
    

    您必须更改為此:

     mMap = ((SupportMapFragment) getChildFragmentManager()
                .findFragmentById(R.id.mapView)).getMap();
    

  • 5月前
    3 #

    您的Class程从 Fragment擴充套件 ,而您使用的是 Fragment 在佈局中宣告,但巢狀片段仅在動態添加時起作用。

    因此,您有两个選擇:

    1)从 FragmentAcivity擴充套件 代替 Fragment 並采用適当的方法.

    2)添加 Fragment 動態,如下所示:

    // ReturnVisitDetails

    MapFragment mapFragment = new MapFragment();
    FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
    transaction.add(R.id.rl_map_container, mapFragment).commit();
    

    //佈局

    <FrameLayout
            android:id="@+id/rl_map_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    

    --UPDATED--

    Fragment內使用Google Maps的完整示例 :

    // TestFragmentActivity

    public class TestFragmentActivity extends android.support.v4.app.FragmentActivity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.test_fragment_activity);
        }
    }
    

    // TestFragment

    public class TestFragment extends android.support.v4.app.Fragment {
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.test_fragment, container, false);
            CustomMapFragment mapFragment = new CustomMapFragment();
            FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
            transaction.add(R.id.map_container, mapFragment).commit();
            return rootView;
        }
    }
    

    // CustomMapFragment

    public class CustomMapFragment extends com.google.android.gms.maps.SupportMapFragment {
        private final LatLng HAMBURG = new LatLng(53.558, 9.927);
        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
            GoogleMap googleMap = getMap();
            googleMap.addMarker(new MarkerOptions().position(HAMBURG).title("Hamburg"));
            googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(HAMBURG, 15));
            googleMap.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
        }
    }
    

    // test_fragment_activity.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
        <fragment
            android:id="@+id/map"
            class="your_package.TestFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </RelativeLayout>
    

    // test_fragment.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
        <FrameLayout
            android:id="@+id/map_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </RelativeLayout>
    

  • 5月前
    4 #

    我在此搜尋了很多主题,因為我在使用旧版Google Play服務時遇到了同樣的問题.正如Victor所說,您的班級是从片段開始的,它必须使用佈局中定義的另一个片段,所以最好的方法是使用 getChildFragmentManager() 代替 getSupportFragmentManager()

    getMap() method is deprecated 現在,如google docs中所述.請參考更多: https://developers.google.com/android/reference/com/google/android/gms/maps/MapFragment.html#getMap()

    下面是我在较早的播放服務的各種設備上測試過的工作代碼.如果設備具有较旧的Google Play服務,並且物件為 GoogleMap,則這將生成預設訊息 返迴null:

    class MyFragment extends Fragment
    {
          SupportMapFragment myMapFragment;
        GoogleMap myMap;
        View convertView;
        FragmentManager fm;
        public View onCreateView(LayoutInflater inflater, ViewGroup container,  Bundle savedInstanceState) {
        convertView=inflater.inflate(R.layout.mymap,null);
        fm=getChildFragmentManager();
            myMapFragment=(SupportMapFragment) fm.findFragmentById(R.id.myMapId);
    
            // here instead of using getMap(), we are using getMapAsync() which returns a callback and shows map only when it gets ready.
            //it automatically checks for null pointer or older play services version, getMap() is deprecated as mentioned in google docs.
    
            myMapFragment.getMapAsync(new OnMapReadyCallback() {
                @Override
                public void onMapReady(GoogleMap googlemap) {
                    // TODO Auto-generated method stub
                    myMap=googlemap;
                    startMap();
                }
            });
    
        return convertView;
        }
    }
    

    有關更多資訊,請參见此執行緒。

  • 5月前
    5 #

    這是否是遷移到目標sdk版本21的問题? 如果是這樣,您可能会感兴趣以下解決方案:

    MapFragment或MapView getMap()在棒棒糖上返迴null

  • 弄清楚selenium文件隱式和顯式等待混合的原因
  • SQL Server仅使用最新值選擇不同的行