I have developed an app to show ATM machines in your area. It works on my android phone but when I try to launch it on my android wearable watch it crashes. With the error "Binary XML File line #2 Error inflating class fragment" Any help is much appreciated.
Here is my xml code
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment"/>
and my activity:
public class MainActivity extends Activity {
private final String TAG = getClass().getSimpleName();
private GoogleMap mMap;
private String[] places;
private LocationManager locationManager;
private Location loc;
SharedPreferences sp;
static String distance;
public static final String PREF = "MyPrefs";
public static final String DISTANCE = "distance";
@Override
public void onResume() {
super.onResume();
sp = this.getSharedPreferences(PREF, Context.MODE_PRIVATE);
distance = sp.getString(DISTANCE, "1000");
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
.getMap();
currentLocation();
places=new String[1];
places[0]="ATM";
mMap.setMyLocationEnabled(true);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
private class GetPlaces extends AsyncTask<Void, Void, ArrayList<Place>> {
private ProgressDialog dialog;
private Context context;
private String places;
private double longitude;
private double latitude;
public GetPlaces(Context context, String places) {
this.context = context;
this.places = places;
}
@Override
protected void onPostExecute(ArrayList<Place> result) {
super.onPostExecute(result);
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location location = lm
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
longitude = location.getLongitude();
latitude = location.getLatitude();
if (dialog.isShowing()) {
dialog.dismiss();
}
for (int i = 0; i < result.size(); i++) {
mMap.addMarker(new MarkerOptions()
.title(result.get(i).getName())
.position(
new LatLng(result.get(i).getLatitude(), result
.get(i).getLongitude()))
.icon(BitmapDescriptorFactory
.fromResource(R.drawable.pin))
.snippet(result.get(i).getVicinity()));
}
CameraPosition cameraPosition = new CameraPosition.Builder()
.target(new LatLng(latitude, longitude)) // Sets the center of the map to
// Mountain View
.zoom(14) // Sets the zoom
.tilt(30) // Sets the tilt of the camera to 30 degrees
.build(); // Creates a CameraPosition from the builder
mMap.animateCamera(CameraUpdateFactory
.newCameraPosition(cameraPosition));
}
@Override
protected ArrayList<Place> doInBackground(Void... arg0) {
PlacesService service = new PlacesService(
"xxxxxxxxxxxxx");
ArrayList<Place> findPlaces = service.findPlaces(loc.getLatitude(),
loc.getLongitude(), places, distance);
for (int i = 0; i < findPlaces.size(); i++) {
Place placeDetail = findPlaces.get(i);
Log.e(TAG, "places : " + placeDetail.getName());
}
return findPlaces;
}
}
private void currentLocation() {
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
String provider = locationManager
.getBestProvider(new Criteria(), false);
Location location = locationManager.getLastKnownLocation(provider);
if (location == null) {
locationManager.requestLocationUpdates(provider, 0, 0, listener);
} else {
loc = location;
new GetPlaces(MainActivity.this, places[0].toLowerCase().replace(
"-", "_")).execute();
Log.e(TAG, "location : " + location);
}
}
private LocationListener listener = new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onLocationChanged(Location location) {
Log.e(TAG, "location update : " + location);
loc = location;
locationManager.removeUpdates(listener);
}
};
}
不幸的是,Android Wear设备目前不支持MapFragment
和GoogleMap
(以及整个Google Maps Android API)。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.