2015-11-16 112 views
0

我想在一個片段中設置Google Map,並遇到一些試圖讓按鈕正常收聽的問題。我有以下代碼:谷歌地圖API設置按鈕點擊監聽器錯誤

public DefaultMap extends android,.support.v4.app.Fragment implements OnMapReadyCallback { 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

     View v = inflater.inflate(R.layout.map_fragment, container, false); 

     MapsInitializer.initialize(getActivity()); 

     // Obtain the SupportMapFragment and get notified when the map is ready to be used. 
     mMapFragment = new SupportMapFragment() { 
      @Override 
      public void onActivityCreated(Bundle savedInstanceState) { 
       super.onActivityCreated(savedInstanceState); 
       mMap = mMapFragment.getMap(); 
       if (mMap != null) { 

       } 
      } 
     }; 

     mMapFragment.getMapAsync(this); 
//  mAutocompleteView = (AutoCompleteTextView) findViewById(R.id.TFSearch); 
//  mAutocompleteView.setAdapter(new PlaceAutocompleteAdapter(this, android.R.layout.simple_dropdown_item_1line)); 
     searchSetup(v); 
     zoomSetup(v); 
     setupMapType(v); 
     return v; 
    } 
    //an example of one of the setup functions 
    public void setupMapType(View view) { 
     typeMapButton = (Button) view.findViewById(R.id.TFtype); 
     typeMapButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if (mMap.getMapType() == GoogleMap.MAP_TYPE_NORMAL) { 
        mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE); 
       } else { 
        mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); 
       } 
      } 
     }); 
    } 
} 

相關件map_fragment.xml的:

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    xmlns:tools="http://schemas.android.com/tools"> 
    <fragment xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:map="http://schemas.android.com/apk/res-auto" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:id="@+id/mapScreen" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     tools:context=".DefaultMap" 
     android:name="com.google.android.gms.maps.SupportMapFragment" 
     /> 

    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 
     <LinearLayout 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:orientation="horizontal"> 

      <Button 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/map_type" 
       android:id="@+id/TFtype" 
       android:onClick="onTypeMap" 
       tools:context=".DefaultMap" 
       android:layout_marginRight="5dp" /> 
     </LinearLayout> 

    </LinearLayout> 

</FrameLayout> 

,然後點擊按鈕(同樣爲所有4個按鈕),當我收到以下錯誤:

11-15 22:57:22.481 32523-32523/oose2017.place2b E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'int com.google.android.gms.maps.GoogleMap.getMapType()' on a null object reference 
11-15 22:57:22.481 32523-32523/myproject E/AndroidRuntime:  at myproject.interfaces.map.DefaultMap$5.onClick(DefaultMap.java:153) 

有誰知道我如何解決這些問題?

+1

顯示'map_fragment.xml'文件還 –

+0

更新,以顯示map_fragment.xml片斷 – Eric

+0

見下面的例子http://www.truiton.com/2013/05/android-supportmapfragment-example/ –

回答

1

使用SupportMapFragment.newInstance()獲取SupportMapFragment的新實例以從Fragment中添加它,並且還從DefaultMap的xml中刪除android:name="com.google.android.gms.maps.SupportMapFragment"片段。

的詳細信息,請參閱以下教程:

SupportMapFragment problem when loding in Fragment class