我在我的应用程序中使用GoogleMaps。为此,我在自定义Fragment类中实现了SupportMapFragment Map-v2。但不幸的是,我在行
中没有任何理由得到NullPointerExceptionSupportMapFragment fm = ((SupportMapFragment) getActivity ().getSupportFragmentManager ().findFragmentById (R.id.map));
googleMap = fm.getMap();
我真的不知道该怎么办,我完全无助。我甚至提到了link 1,link 2和link 3中几个类似的问题。但似乎没有任何结果。请帮助我。请查看我的logcat错误和完整代码,如下所示。
MapFragment.java
public class MapFragment extends Fragment implements GoogleMap.OnMarkerClickListener {
GoogleMap googleMap;
private Map<Marker, Integer> markerPosition = new HashMap ();
private ImageView imgUser;
private TextView txtName, txtLocation,txtCount;
private ArrayList<Info> mList;
public static MapFragment newInstance() {
MapFragment fragment = new MapFragment();
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_map, container, false);
findViews (view);
googleMapsData ();
return view;
}
@Override
public void onViewCreated (View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated (view, savedInstanceState);
mList = getHelper ().getList ();
counts ();
}
private void googleMapsData () {
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable (getActivity ());
if(status!= ConnectionResult.SUCCESS){
int requestCode = 10;
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, getActivity (), requestCode);
dialog.show();
}else {
SupportMapFragment fm = ((SupportMapFragment) getActivity ().getSupportFragmentManager ().findFragmentById (R.id.map)); //This is the line getting error
googleMap = fm.getMap(); //This is the line getting error
googleMap.setMapType (GoogleMap.MAP_TYPE_NORMAL);
googleMap.setOnMarkerClickListener(this);
googleMap.setMyLocationEnabled(true);
for(int i=0;i<mList.size ();i++){
drawMarker (new LatLng (Double.parseDouble (mList.get (i).getLatitude ()), Double.parseDouble (mList.get (i).getLongitude ())),i);
}
CameraPosition cameraPosition = new CameraPosition.Builder()
.target (new LatLng (Double.parseDouble ("13.0900"), Double.parseDouble ("80.2700")))
.zoom (7)
.build ();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition (cameraPosition), new GoogleMap.CancelableCallback () {
@Override
public void onFinish() {
CameraUpdate cu_scroll = CameraUpdateFactory.scrollBy(-300, 70);
googleMap.animateCamera(cu_scroll);
}
@Override
public void onCancel() {
}
});
}
googleMap.setOnMapClickListener(new OnMapClickListener () {
@Override
public void onMapClick(LatLng point) {
}
});
googleMap.setOnMapLongClickListener(new OnMapLongClickListener () {
@Override
public void onMapLongClick(LatLng point) {
}
});
}
public void details(Info Info){
cardBottomdetails.setVisibility (View.VISIBLE);
txtName.setText (Info.getHotelName ());
txtLocation.setText (Info.getCity ());
Glide.with (getActivity ())
.load(Constant.BASE_IP+Info.getImage ())
.centerCrop()
.placeholder(getResources ().getDrawable (R.drawable.img))
.crossFade()
.into(imgUser);
}
public void counts(){
txtCount.setText (""+mList.size ());
if(Build.VERSION.SDK_INT >= 16) {
txtCount.setBackground (Util.drawCircle (getActivity (), 25, 25, getResources ().getColor (R.color.yellow)));
}else{
txtCount.setBackgroundDrawable (Util.drawCircle (getActivity (), 25, 25, getResources ().getColor (R.color.yellow)));
}
}
private void drawMarker(LatLng point , int index){
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position (point);
Marker marker = googleMap.addMarker(markerOptions);
markerPosition.put (marker,index);
}
public void findViews(View view){
imgUser = (ImageView)view.findViewById (R.id.img_user);
txtName = (TextView)view.findViewById (R.id.txt_name);
txtLocation = (TextView)view.findViewById (R.id.txt_location);
txtCount = (TextView)view.findViewById (R.id.txt_count);
}
@Override
public boolean onMarkerClick (Marker marker) {
int index = markerPosition.get (marker);
details (mList.get (index));
return false;
}
@Override
public void onResume () {
super.onResume ();
}
}
fragment_map.xml
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
</FrameLayout>
Logcat错误:
java.lang.NullPointerException
at com.example.map.fragment.MapFragment.googleMapsData(MapFragment.java:105)
at com.example.map.fragment.MapFragment.onCreateView(MapFragment.java:68)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:458)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5602)
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:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
注意:
1)我在代码中导入了import android.support.v4.app.Fragment;
。
2)我在Activity
中使用此代码时工作正常。仅当我从Activity
切换到Fragment
时才会出现此问题。
提前致谢。所有解决方案对于解决我的问题都会更有用,更有价值。
答案 0 :(得分:2)
当我刚开始使用地图时,我得到了同样的问题。问题是因为加载谷歌地图需要时间(如果您在片段内部使用,您可以预期此加载时间会很长),并且当您尝试在加载地图之前访问地图时,您需要#39 ; ll得到NullPointerException,说地图引用变为null。您应该等待地图首先加载,然后您应该访问地图视图。你有预定义的回调,这就是你应该如何使用它。
supportMapFragment.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(GoogleMap map) {
googleMap = map;
googleMap.setMapType (GoogleMap.MAP_TYPE_NORMAL);
googleMap.setOnMarkerClickListener(this);
googleMap.setMyLocationEnabled(true);
for(int i=0;i<mList.size ();i++){
drawMarker (new LatLng (Double.parseDouble (mList.get (i).getLatitude ()), Double.parseDouble (mList.get (i).getLongitude ())),i);
}
CameraPosition cameraPosition = new CameraPosition.Builder()
.target (new LatLng (Double.parseDouble ("13.0900"), Double.parseDouble ("80.2700")))
.zoom (7)
.build ();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition (cameraPosition), new GoogleMap.CancelableCallback () {
@Override
public void onFinish() {
CameraUpdate cu_scroll = CameraUpdateFactory.scrollBy(-300, 70);
googleMap.animateCamera(cu_scroll);
}
@Override
public void onCancel() {
}
});
}
});