我正在尝试创建一个活动但有多个片段的应用程序。为此,我创建了 Main Activity 并在其布局文件中设置了 View-pager。
主活动通过其 Ascyn 任务连接到互联网,下载所需的信息将其存储在一个数组列表中,该列表将用于填写三个主要片段(地图片段、列表片段、增强现实片段)的数据我遇到的是由于某种原因地图不会显示它在我加载数据之前给了我一个空指针。
编辑我已经按照@Roman 的建议进行了一些更改。我的OnResume
方法仍然存在一些问题。
编辑 2我取得了一些进展。在地图仍然没有显示的同时,我不再得到空指针,而是我只有一个空白屏幕。
编辑 3 已修复
public class MapDisplayFragment extends SupportMapFragment implements
OnInfoWindowClickListener {
public ArrayList<HashMap<String, String>> UplacesListItems = new ArrayList<HashMap<String, String>>();
static List<Float> fmyLat = new ArrayList<Float>();
static List<Float> fmyLng = new ArrayList<Float>();
HashMap<String, String> song = new HashMap<String, String>();
public static final String ARG_SECTION_NUMBER = "section_number";
public static final String placesListItems = "ArrayList";
public static final String myLAt = "myLAt";
public static final String myLng = "myLng";
public static String User_lat = "user_lat";
public static String User_lng = "user_lng";
public static String KEY_REFRENCE = "refrence";
public static String KEY_NAME = "name";
public static String KEY_VICINITY = "vivinity";
public static String KEY_CATEGORY = "category";
private static String[] marker_Id;
private static String[] refrences;
public static String ref;
private GoogleMap mMap;
FragmentTransaction view;
// View mView;
ViewGroup layout;
MapView mView;
double Ulat = 0.0;
double Ulng = 0.0;
Marker FourSquare;
Marker User;
String Reference = "";
LatLng ULatLng;
GLSurfaceView mGLView;
Location userLoc;
@Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
mView.onResume();
setUpMapIfNeeded();
Log.d("OnResume View", "Sucess");
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.d("Destroy MEthod", "Sucess");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
System.gc();
super.onCreateView(inflater, container, savedInstanceState);
layout = (LinearLayout) inflater.inflate(R.layout.map_fragment,
container, false);
mView = (MapView) layout.findViewById(R.id.mapView);
mView.onCreate(savedInstanceState);
setMapTransparent(layout);
setUpMapIfNeeded();
Log.d("OnCreate View", "Sucess");
return layout;
}
private void setMapTransparent(ViewGroup group) {
int childCount = group.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = group.getChildAt(i);
if (child instanceof ViewGroup) {
setMapTransparent((ViewGroup) child);
} else if (child instanceof SurfaceView) {
child.setBackgroundColor(0x00000000);
}
}
}
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the
// map.
if (mMap == null) {
SupportMapFragment.newInstance();
mMap = mView.getMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
setUpMap();
}
else {
setUpMap();
}
}
@SuppressWarnings("unchecked")
private void setUpMap() {
Ulat = getArguments().getDouble(User_lat);
Ulng = getArguments().getDouble(User_lng);
userLoc = new Location("manaul");
userLoc.setLatitude(Ulat);
userLoc.setLongitude(Ulng);
Log.d("uwer LAt", String.valueOf(Ulat));
UplacesListItems = (ArrayList<HashMap<String, String>>) getArguments()
.get(placesListItems);
if (UplacesListItems.size() != 0) {
Log.d("ArrayList", String.valueOf(UplacesListItems.size()));
} else {
Log.d("ArrayList", "ArrayList = null");
}
mMap = mView.getMap();
mMap.setMyLocationEnabled(true);
// AddUser();
if (UplacesListItems.size() != 0) {
addMarkersToMap();
}
mMap.setMapType(MAP_TYPE_HYBRID);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(Ulat, Ulng), 15));
mMap.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
}
@SuppressWarnings("unchecked")
private void addMarkersToMap() {
// TODO Auto-generated method stub
UplacesListItems = (ArrayList<HashMap<String, String>>) getArguments()
.get(placesListItems);
fmyLat = (List<Float>) getArguments().get(myLAt);
fmyLng = (List<Float>) getArguments().get(myLng);
double flat;
double flng;
if (UplacesListItems != null) {
marker_Id = new String[UplacesListItems.size()];
refrences = new String[UplacesListItems.size()];
for (int i = 0; i < UplacesListItems.size(); i++) {
Log.d("lat of fourSquarePlaces", String.valueOf(fmyLat.get(0)));
song = UplacesListItems.get(i);
flat = fmyLat.get(i);
flng = fmyLng.get(i);
Location destination = new Location("manual");
destination.setLatitude(flat);
destination.setLongitude(flng);
// System.out.print(fPLat);
// StoreReference(song.get(KEY_REFRENCE));
if (mMap != null) {
ULatLng = new LatLng(flat, flng);
DecimalFormat df = new DecimalFormat("#.##");
final String distance = String.valueOf(df
.format(destination.distanceTo(userLoc) / 1000))
+ "Km";
final String Reference = (song.get(KEY_REFRENCE));
final String category = (song.get(KEY_CATEGORY));
FourSquare = mMap
.addMarker(new MarkerOptions()
.position(ULatLng)
.title(song.get(KEY_NAME))
.snippet(
distance + " " + Reference + " "
+ category)
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.point_map)));
marker_Id[i] = FourSquare.getId();
refrences[i] = song.get(KEY_REFRENCE);
mMap.setOnMarkerClickListener(new OnMarkerClickListener() {
@Override
public boolean onMarkerClick(Marker marker) {
// TODO Auto-generated method stub
marker.showInfoWindow();
return false;
}
});
mMap.setInfoWindowAdapter(new InfoWindowAdapter() {
@Override
public View getInfoContents(Marker marker) {
// TODO Auto-generated method stub
return null;
}
@Override
public View getInfoWindow(Marker marker) {
// TODO Auto-generated method stub
View v = ((Activity) getActivity())
.getLayoutInflater().inflate(
R.layout.custom_info_windows, null);
TextView name = (TextView) v
.findViewById(R.id.name);
TextView Distance = (TextView) v
.findViewById(R.id.distance);
TextView reference = (TextView) v
.findViewById(R.id.reference);
TextView category = (TextView) v
.findViewById(R.id.category);
String value = marker.getSnippet();
String[] result = value.split(" ", 3);
String distanceText = result[0];
String referenceText = result[1];
String categoryText = result[2];
name.setText(marker.getTitle());
Distance.setText(distanceText);
reference.setText(referenceText);
category.setText(categoryText);
return v;
}
});
mMap.setOnInfoWindowClickListener(this);
Log.d("Add Marker Options Method", "Sucess");
}
else {
// setUpMapIfNeeded();
}
}
} else {
Log.d("Add Marker Options Method", "ArraryList Empty");
}
}
@Override
public void onInfoWindowClick(Marker marker) {
// TODO Auto-generated method stub
String value = marker.getSnippet();
String[] result = value.split(" ", 3);
String referenceText = result[1];
String categoryText = result[2];
Intent intent = new Intent(getActivity(), SingleFragmentActivity.class);
intent.putExtra("KEY_REFRENCE", referenceText);
intent.putExtra("KEY_CATEGORY", categoryText);
startActivity(intent);
}
}
布局
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.google.android.gms.maps.MapView
android:id="@+id/mapView"
android:name="mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
/>
</LinearLayout>