我是android新手(2周岁),我在寻呼机查看器中片段更新谷歌地图标记时遇到困难
在使用第一张片段的相机拍摄照片后获取用户经度和纬度后,我想更新标记,或者如果可能的话,在谷歌地图上用第二片段中的新标记重新创建地图。
使用接口将数据从fragment1(ImageFragment)传输到fragment2(MapFragment)后,我仍然无法更新地图,我在这一行上有错误,而且任何与GoogleMap有关的内容
googleMap.addMarker(marker);// null pointer exeception here
CameraPosition cameraPosition = new CameraPosition.Builder().target(new LatLng(latitude, longitude)).zoom(15).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));// and null pointer exeception here too
我仍然在下面这一行得到nullpointer异常错误。 //添加标记googleMap.addMarker(marker);这是我的logcat
Caused by: java.lang.NullPointerException at com.solexadex.itrak.i_trak.MapFragment.displayMapOnFragment(MapFragment.java:101)
这些是我的代码:MapFragment Class
public class MapFragment extends Fragment implements ImageFragment.DisplayMapOnFragment {
//-------------- class variables
private MapView mMapView;
private GoogleMap mGoogleMap;
private GoogleMap googleMap;
private FragmentActivity myContext;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// inflat and return the layout
View v = inflater.inflate(R.layout.fragment_map, container,
false);
mMapView = (MapView) v.findViewById(R.id.mapView);
mMapView.onCreate(savedInstanceState);
mMapView.onResume();// needed to get the map to display immediately
try {
MapsInitializer.initialize(getActivity().getApplicationContext());
} catch (Exception e) {
e.printStackTrace();
}
googleMap = mMapView.getMap();
// latitude and longitude
double latitude = 7.385044;
double longitude = 9.486671;
// create marker
MarkerOptions marker = new MarkerOptions().position(
new LatLng(latitude, longitude)).title("Hello Maps");
// Changing marker icon
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_ROSE));
// adding marker
googleMap.addMarker(marker);
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(latitude, longitude)).zoom(12).build();
googleMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
// Perform any camera updates here
return v;
}
@Override
public void displayMapOnFragment(double latitude, double longitude) {
//initilizeMap();
// latitude and longitude
// double latitude = 9.155543;
//double longitude = 7.321151;//9.155543, 7.321151
MarkerOptions marker = new MarkerOptions().position(
new LatLng(latitude, longitude)).title("Hello Maps");
// Changing marker icon
marker.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_ROSE));
// adding marker
googleMap.addMarker(marker);
//googleMap.
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(latitude, longitude)).zoom(15).build();
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
@Override
public void onResume() {
super.onResume();
}
@Override
public void onPause() {
super.onPause();
mMapView.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
mMapView.onDestroy();
}
@Override
public void onLowMemory() {
super.onLowMemory();
mMapView.onLowMemory();
}}
下面的ImageFragment类
public class ImageFragment extends Fragment {
private Context mContext;
double longitude, latitude;
public String street,city, state, country,zip,knownName,iDate;
private final Context dialogContext = getActivity();
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 1888;
FloatingActionButton button;
ImageView imageView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
inflater = getActivity().getLayoutInflater();
final View rootView = inflater.inflate(R.layout.fragment_image,
container, false);
mContext=getActivity().getApplicationContext();
button = ( FloatingActionButton) rootView.findViewById(R.id.fab);
imageView = (ImageView) rootView.findViewById(R.id.imageView);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent,
CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
});
return rootView;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// now access the TextView as you want
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK) {
Bitmap bmp = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
// convert byte array to Bitmap
Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray, 0,
byteArray.length);
imageView.setImageBitmap(bitmap);
//call
this.getCoord();
}
}
}
public interface OnDataPass {
public void onDataPass(double uLat, double uLong);
}
public interface OnDataPass2 {
public void onDatapass2(String address, String city, String state, String postalCode, String KnowName, String createdDate);
}
public interface DisplayMapOnFragment {
public void displayMapOnFragment(double mapLat, double mapLong);
}
OnDataPass dataPasser;
OnDataPass2 dataPasser2;
DisplayMapOnFragment displayMapOnFragment;
@Override
public void onAttach(Activity a) {
super.onAttach(a);
try {
displayMapOnFragment = (DisplayMapOnFragment) a;
dataPasser = (OnDataPass) a;
dataPasser2 = (OnDataPass2) a;
} catch (ClassCastException e) {
throw new ClassCastException(a.toString()
+ " must implement OnDataPass");
}
}
public void passData(double uLong, double uLat) {
dataPasser.onDataPass(uLong, uLat);
}
int aCount=20; //initialize counter to loop getCoord method if 0.0 is returned so as to ge value that is not 0.0
public void getCoord()
{
MapFragment mf = new MapFragment();
NewTrackActivity newTrackActivity=new NewTrackActivity();
TrackGPS gps = new TrackGPS(mContext);
if(gps.canGetLocation()){
longitude = gps.getLongitude();
latitude = gps.getLatitude();
if(aCount!=20)
Toast.makeText(mContext,"Retrying to get Coordinate: "+aCount+"\nLongitude:"+Double.toString(longitude)+"\nLatitude:"+Double.toString(latitude), Toast.LENGTH_LONG).show();
Geocoder geocoder;
List<Address> addresses;
geocoder = new Geocoder(getActivity(), Locale.getDefault());
try {
//Log.e("latitude", "inside latitude--" + latitude);
addresses = geocoder.getFromLocation(latitude, longitude, 1);
if (addresses != null && addresses.size() > 0) {
street = addresses.get(0).getAddressLine(0);
city = addresses.get(0).getLocality();
state = addresses.get(0).getAdminArea();
country = addresses.get(0).getCountryName();
zip = addresses.get(0).getPostalCode();
knownName = addresses.get(0).getFeatureName();
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
iDate=dateFormat.format(date);
// locationTxt.setText(address + " " + city + " " + country);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
newTrackActivity.onDataPass( longitude, latitude);
newTrackActivity.onDatapass2( street, city, state, zip, knownName, iDate);
if(Double.compare(latitude,0.00)==0 && aCount>0) {
aCount--;
getCoord();
}
if(aCount<=1) {
Toast.makeText(mContext,"Please retake photo to get current position coordinate", Toast.LENGTH_LONG).show();
aCount=20; //reset count
}else
{
Toast.makeText(mContext,"Coordinates found", Toast.LENGTH_LONG).show();
}
newTrackActivity.displayMapOnFragment(latitude, longitude);
// Supply value
// Bundle args = new Bundle();
// args.putDouble("latitude", latitude);
//args.putDouble("longitude", longitude);
//mf.setArguments(args);
}
else
{
gps.showSettingsAlert(mContext);
}
}
}
Fragment_map.xml
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.gms.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent" />