我在fragment
. 下面是我的代码。
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
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.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.CameraPosition;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.tfick.android.R;
public class PoiFragment extends Fragment{
private GoogleMap mGoogleMap;
private static final LatLng SYDNEY = new LatLng(23.03,72.62);
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.poi_fragment, container,false);
MapFragment mMapFragment = MapFragment.newInstance();
FragmentTransaction fragmentTransaction =
getFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.container1, mMapFragment);
fragmentTransaction.commit();
if(mGoogleMap ==null){
/*initializing map */
mGoogleMap=mMapFragment.getMap();
/*cretaing custom camera position*/
CameraPosition mCameraPosition=new CameraPosition.Builder().target(SYDNEY).bearing(0).tilt(30).build();
/*setting custom camera position*/
mGoogleMap.animateCamera(CameraUpdateFactory.newCameraPosition(mCameraPosition));
/*adding custom marker on the map.*/
mGoogleMap.addMarker(new MarkerOptions().draggable(true).position(new LatLng(22.03, 72.56)).title("hello world").snippet("Ahmedabad").icon(BitmapDescriptorFactory.fromResource(R.drawable.wifi)));
mGoogleMap.addMarker(new MarkerOptions().draggable(true).position(new LatLng(23.03, 78.10)).title("hello world").snippet("Baroda").icon(BitmapDescriptorFactory.fromResource(R.drawable.wifi)));
}
if(mGoogleMap !=null){
/* map is already initialized then setting map type */
/*
* normal
* satellite
* hybrid
* terrain
* none
*/
mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
}
return v;
}
}
03-01 12:15:31.439: E/AndroidRuntime(9299): FATAL EXCEPTION: main
03-01 12:15:31.439: E/AndroidRuntime(9299): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tfick.android/com.tfick.android.PointOfIntrest}: java.lang.NullPointerException: CameraUpdateFactory is not initialized
03-01 12:15:31.439: E/AndroidRuntime(9299): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
03-01 12:15:31.439: E/AndroidRuntime(9299): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
03-01 12:15:31.439: E/AndroidRuntime(9299): at android.app.ActivityThread.access$600(ActivityThread.java:140)
03-01 12:15:31.439: E/AndroidRuntime(9299): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
03-01 12:15:31.439: E/AndroidRuntime(9299): at android.os.Handler.dispatchMessage(Handler.java:99)
03-01 12:15:31.439: E/AndroidRuntime(9299): at android.os.Looper.loop(Looper.java:137)
03-01 12:15:31.439: E/AndroidRuntime(9299): at android.app.ActivityThread.main(ActivityThread.java:4898)
03-01 12:15:31.439: E/AndroidRuntime(9299): at java.lang.reflect.Method.invokeNative(Native Method)
03-01 12:15:31.439: E/AndroidRuntime(9299): at java.lang.reflect.Method.invoke(Method.java:511)
03-01 12:15:31.439: E/AndroidRuntime(9299): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
03-01 12:15:31.439: E/AndroidRuntime(9299): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
03-01 12:15:31.439: E/AndroidRuntime(9299): at dalvik.system.NativeStart.main(Native Method)
03-01 12:15:31.439: E/AndroidRuntime(9299): Caused by: java.lang.NullPointerException: CameraUpdateFactory is not initialized
03-01 12:15:31.439: E/AndroidRuntime(9299): at com.google.android.gms.internal.at.a(Unknown Source)
03-01 12:15:31.439: E/AndroidRuntime(9299): at com.google.android.gms.maps.CameraUpdateFactory.J(Unknown Source)
03-01 12:15:31.439: E/AndroidRuntime(9299): at com.google.android.gms.maps.CameraUpdateFactory.newCameraPosition(Unknown Source)
03-01 12:15:31.439: E/AndroidRuntime(9299): at com.tfick.fragment.PoiFragment.onCreateView(PoiFragment.java:51)
03-01 12:15:31.439: E/AndroidRuntime(9299): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:829)
03-01 12:15:31.439: E/AndroidRuntime(9299): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1035)
03-01 12:15:31.439: E/AndroidRuntime(9299): at android.app.BackStackRecord.run(BackStackRecord.java:635)
03-01 12:15:31.439: E/AndroidRuntime(9299): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1397)
03-01 12:15:31.439: E/AndroidRuntime(9299): at android.app.Activity.performStart(Activity.java:5215)
03-01 12:15:31.439: E/AndroidRuntime(9299): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2073)
03-01 12:15:31.439: E/AndroidRuntime(9299): ... 11 more