2013-03-14 4 views
0

Так что я хочу добавить объекты на карту, у меня есть круг, когда пользователь нажимает кнопку «добавить круг», но когда я пытаюсь получить карту из xml, я получаю nullpointerexeption когда он .getmap() под newCircle() выполняет любые исправления или обходные пути? Пример кода полезен.Добавление объектов карты Google в android

import android.os.Bundle; 
import android.support.v4.app.FragmentActivity; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.ViewGroup; 
import android.widget.Button; 

import com.google.android.gms.maps.*; 
import com.google.android.gms.maps.model.CircleOptions; 
import com.google.android.gms.maps.model.LatLng; 

public class GoogleMaps extends FragmentActivity { 
    GoogleMap mMap; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_google_maps); 
     makeCircleButton(); 
    } 

    private void makeCircleButton() { 
     OnClickListener onClickListener = new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       newCircle(); 
      } 
     }; 

     Button button = (Button) findViewById(R.id.add_circular_area); 
     button.setOnClickListener(onClickListener); 
    } 

    public void newCircle() { 
     mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)) 
       .getMap(); 

     // Instantiates a new CircleOptions object and defines the center and 
     // radius 
     CircleOptions circleOptions = new CircleOptions().center(
       new LatLng(37.4, -122.1)).radius(1000); // In meters 
     mMap.addCircle(circleOptions); 

     // Get back the mutable Circle 
     // Circle circle = mMap.addCircle(circleOptions); 
    } 

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <include 
     android:id="@+id/map" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     layout="@layout/fragment" /> 

    <ScrollView 
     android:id="@+id/map_scrollview" 
     android:layout_width="match_parent" 
     android:layout_height="100dp" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentLeft="true" 
     android:fadingEdge="none" 
     android:scrollbarAlwaysDrawVerticalTrack="true" > 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:orientation="vertical" > 

      <Button 
       android:id="@+id/button1" 
       android:layout_width="245dp" 
       android:layout_height="wrap_content" 
       android:text="Add rectangular area" /> 

      <Button 
       android:id="@+id/add_circular_area" 
       android:layout_width="245dp" 
       android:layout_height="wrap_content" 
       android:text="Add circular area" /> 

     </LinearLayout> 

    </ScrollView> 

</RelativeLayout> 

XML включают в себя:

<?xml version="1.0" encoding="utf-8"?> 
<fragment 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:map="http://schemas.android.com/apk/res-auto" 
     android:id="@+id/map" 
     android:name="com.google.android.gms.maps.SupportMapFragment" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     map:uiTiltGestures="false" 
     map:mapType="hybrid" 
     /> 

ответ

2

вы использовали свой Fragment Имя, как com.google.android.gms.maps.SupportMapFragment, так что вы не можете получить MapFragment вы должны использовать getSupportFragmentManager() вместо getFragmentManager() и брось до SupportMapFragment.

Изменить

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

Для

mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)) 
       .getMap(); 

Надеется, что это поможет.

+0

Вот и все! Благодарю. –

Смежные вопросы