【发布时间】:2014-01-17 07:29:06
【问题描述】:
我已在视图寻呼机中集成了新的 google maps api v2 片段。当我从地图选项卡(街景屏幕)移动到第二个选项卡(客户端详细信息)时,显示的黑色视图将数据插入片段。有人知道解决方案吗??
截图:
代码:
ListingStreetViewFragment.java
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View inflatedView = inflater.inflate(R.layout.map_view, container, false);
try {
MapsInitializer.initialize(getActivity());
} catch (GooglePlayServicesNotAvailableException e) {
// TODO handle this situation
}
markers = new Hashtable<String, String>();
mMapView = (MapView) inflatedView.findViewById(R.id.map);
mMapView.onCreate(mBundle);
mMapView.onResume();
setUpMapIfNeeded(inflatedView);
return inflatedView;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mBundle = savedInstanceState;
}
// mapping between map object with xml layout.
private void setUpMapIfNeeded(View inflatedView) {
if (mMap == null) {
mMap = ((MapView) inflatedView.findViewById(R.id.map)).getMap();
if (mMap != null) {
mMap.setInfoWindowAdapter(new CustomInfoWindowAdapter());
setUpMap();
}
}
}
//set location on map and show marker on random location
private void setUpMap() {
// random latitude and logitude
// Adding a marker
MarkerOptions marker = new MarkerOptions().position(
new LatLng(53.558, 9.927))
.title("Hello Maps ");
marker.snippet("dinet");
// changing marker color
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_BLUE));
mMap.addMarker(marker);
// Move the camera to last position with a zoom level
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(53.558,
9.927)).zoom(16).build();
mMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
}
@Override
public void onResume() {
if (null != mMapView)
mMapView.onResume();
super.onResume();
}
@Override
public void onPause() {
super.onPause();
if (null != mMapView)
mMapView.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
if (null != mMapView)
mMapView.onDestroy();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (null != mMapView)
mMapView.onSaveInstanceState(outState);
}
@Override
public void onLowMemory() {
super.onLowMemory();
if (null != mMapView)
mMapView.onLowMemory();
}
ListingClientDetailFragment.java
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.client_detail, container, false);
initUi(rootView);
idval = ListingListFragment.cid;
System.out.println("SET: " + idval);
setValues();
return rootView;
}
protected void initUi(View v){
db = new Databasehelper(getActivity());
cDetail = new HashMap<String, String>();
tvName = (TextView)v.findViewById(R.id.tvName);
tvType = (TextView)v.findViewById(R.id.tvType);
tvStatus = (TextView)v.findViewById(R.id.tvStatus);
tvEmail = (TextView)v.findViewById(R.id.tvEmail);
tvPhone = (TextView)v.findViewById(R.id.tvPhone);
ivCall = (ImageView)v.findViewById(R.id.ivCall);
ivMsg = (ImageView)v.findViewById(R.id.ivMsg);
}
//get data from database and set into controls.
protected void setValues(){
cDetail = db.getClientDetail(idval);
tvName.setText(cDetail.get("name"));
tvType.setText(cDetail.get("type"));
tvStatus.setText(cDetail.get("status"));
tvEmail.setText(cDetail.get("email"));
tvPhone.setText(cDetail.get("phone"));
}
logcat 中没有显示错误。所以,我无法从那里追踪它。
【问题讨论】:
-
请添加一些代码或查看log cat 这可能是因为api key的身份验证错误。
-
@GrIsHu 现在检查..
-
@Rajiv 现在看看我的代码..
-
@Segi 您需要在
onCreateView()类的onCreateView()方法中使用getMap()方法获取地图。将线路改为mMapView = (MapView) inflatedView.findViewById(R.id.map).getMap();也不需要调用oncreate()和onResume()。它会被自动调用。 -
@GrIs 我已经尝试过使用 getMap()。仍然没有工作..
标签: android android-viewpager android-maps-v2