public class OpenMapforLocating extends FragmentActivity implements OnMapClickListener,OnMapLongClickListener,OnMarkerDragListener {
public GoogleMap myMap;
Location myLocation;
LatLng currentlatlon;
String currentLocaiton;
final int RQS_GooglePlayServices = 1;
boolean markerClicked;
PolygonOptions polygonOptions;
Polygon polygon;
Button btn_selected;
EditText txt_selectedLocaiton;
String SelectioType;
SharedPreferences prefrences;
public static final String PREFS_NAME = "STPL_BOOKING_DETAILS";
SharedPreferences prefrencesFaviourtes;
public static final String PREFS_FAV = "STPL_BOOKING_FAVIOURTE_LIST";
/** Called when the activity is first created. */
@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.openmapview);
Bundle extras = getIntent().getExtras();
SelectioType = extras.getString("SelectionType");
Log.d("SelectionActivity", "selection Type"+SelectioType);
txt_selectedLocaiton =(EditText)findViewById(R.id.editlocation);
btn_selected =(Button)findViewById(R.id.btn_selectedLocation);
btn_selected.setOnClickListener(onclickListner);
android.app.FragmentManager myFragmentManager = getFragmentManager();
MapFragment myMapFragment
= (MapFragment)myFragmentManager.findFragmentById(R.id.map);
myMap = myMapFragment.getMap();
myMap.setMyLocationEnabled(true);
myMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
myMap.setOnMapClickListener(this);
myMap.setOnMapLongClickListener(this);
myMap.setOnMarkerDragListener(this);
myMap.setTrafficEnabled(false);
myMap.setMyLocationEnabled(true);
markerClicked = false;
LatLng myPoint= new LatLng(gpsdataElements.latitude, gpsdataElements.logitude);
Log.d("OpenMapFOrLocation",gpsdataElements.latitude+" "+gpsdataElements.logitude);
myMap.addMarker(new MarkerOptions().title("mylocation").position(myPoint).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)).draggable(true));
myMap.moveCamera(CameraUpdateFactory.newLatLngZoom(myPoint, 15));
// Zoom in, animating the camera.
myMap.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);
// TODO Auto-generated method stub
}
/* (non-Javadoc)
* @see android.app.Activity#onDestroy()
*/
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
/* (non-Javadoc)
* @see android.app.Activity#onPause()
*/
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
/* (non-Javadoc)
* @see android.app.Activity#onRestart()
*/
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
}
private OnClickListener onclickListner = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btn_selectedLocation:
prefrences= getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = prefrences.edit();
if(SelectioType.contains("FromAddress"))
{
Constants.pickupLat=currentlatlon.latitude;
Constants.pickupLon=currentlatlon.longitude;
editor.putString("PickupLocation",txt_selectedLocaiton.getText().toString());
}
else if(SelectioType.contains("ToAddress"))
{
Constants.dropLat=currentlatlon.latitude;
Constants.droplon=currentlatlon.longitude;
editor.putString("DropLocation",txt_selectedLocaiton.getText().toString());
}
editor.commit();
finish();
break;
default:
break;
}
}
};
@Override
protected void onResume() {
super.onResume();
int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
if (resultCode == ConnectionResult.SUCCESS){
// Toast.makeText(getApplicationContext(),
// "isGooglePlayServicesAvailable SUCCESS",
// Toast.LENGTH_LONG).show();
}else{
GooglePlayServicesUtil.getErrorDialog(resultCode, this, RQS_GooglePlayServices);
}
}
@Override
public void onMarkerDrag(Marker marker) {
// TODO Auto-generated method stub
}
@Override
public void onMarkerDragEnd(Marker marker) {
// TODO Auto-generated method stub
}
@Override
public void onMarkerDragStart(Marker marker) {
// TODO Auto-generated method stub
}
@Override
public void onMapLongClick(LatLng point) {
// TODO Auto-generated method stub
}
@Override
public void onMapClick(LatLng point) {
// TODO Auto-generated method stub
myMap.clear();
currentlatlon =point;
// Toast.makeText(getBaseContext(), point.latitude +" "+point.longitude, Toast.LENGTH_SHORT).show();
markerClicked = false;
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
StringBuilder strReturnedAddress = new StringBuilder("");
//
try {
List<Address> addresses = geocoder.getFromLocation(point.latitude, point.longitude, 1);
if(addresses != null) {
Address returnedAddress = addresses.get(0);
for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) {
strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n");
}
txt_selectedLocaiton.setText(strReturnedAddress.toString());
currentLocaiton=strReturnedAddress.toString();
}
else{
txt_selectedLocaiton.setText("No Address returned!");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
txt_selectedLocaiton.setText("Canont get Address!");
}
myMap.addMarker(new MarkerOptions().title("mylocation").position(point).icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)).draggable(true));
myMap.moveCamera(CameraUpdateFactory.newLatLngZoom(point, 15));
// Zoom in, animating the camera.
myMap.animateCamera(CameraUpdateFactory.zoomTo(15), 2000, null);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.layout.mapmenu, menu);
return true;
}
/**
* Event Handling for Individual menu item selected
* Identify single menu item by it's id
* */
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case R.id.menu_myfaviourts:
// Single menu item is selected do something
// Ex: launching new activity/screen or show alert message
prefrencesFaviourtes= getSharedPreferences(PREFS_FAV, 0);
String oldFaviouteLoc=prefrencesFaviourtes.getString("myFaviourteLocation", "");
prefrencesFaviourtes= getSharedPreferences(PREFS_FAV, 0);
SharedPreferences.Editor favEditor = prefrencesFaviourtes.edit();
favEditor.putString("myFaviourteLocation", oldFaviouteLoc+"!"+currentLocaiton+"$"+currentlatlon.latitude+"$"+currentlatlon.longitude+"!");
favEditor.commit();
Toast.makeText(OpenMapforLocating.this, "New Favioute saved...", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_enableTraffic:
Toast.makeText(OpenMapforLocating.this, "Save is Selected", Toast.LENGTH_SHORT).show();
myMap.setTrafficEnabled(true);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
openmapview.xml 布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<EditText
android:id="@+id/editlocation"
android:background="@color/Gray"
android:textColor="@color/White"
android:hint="select location"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/btn_selectedLocation"
android:layout_below="@+id/editlocation"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/map"
>
</LinearLayout>
<Button
android:id="@+id/btn_selectedLocation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="@drawable/rounded_edittext1"
android:text="ok" />
</RelativeLayout>
日志猫
08-23 15:08:49.330: E/AndroidRuntime(430): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.stpl.fasttrackbooking/com.stpl.fasttrackbooking.OpenMapforLocating}: android.view.InflateException: Binary XML file line #14: Error inflating class fragment
08-23 15:08:49.330: E/AndroidRuntime(430): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
08-23 15:08:49.330: E/AndroidRuntime(430): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
08-23 15:08:49.330: E/AndroidRuntime(430): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
08-23 15:08:49.330: E/AndroidRuntime(430): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
08-23 15:08:49.330: E/AndroidRuntime(430): at android.os.Handler.dispatchMessage(Handler.java:99)
08-23 15:08:49.330: E/AndroidRuntime(430): at android.os.Looper.loop(Looper.java:123)
08-23 15:08:49.330: E/AndroidRuntime(430): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-23 15:08:49.330: E/AndroidRuntime(430): at java.lang.reflect.Method.invokeNative(Native Method)
08-23 15:08:49.330: E/AndroidRuntime(430): at java.lang.reflect.Method.invoke(Method.java:521)
08-23 15:08:49.330: E/AndroidRuntime(430): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-23 15:08:49.330: E/AndroidRuntime(430): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-23 15:08:49.330: E/AndroidRuntime(430): at dalvik.system.NativeStart.main(Native Method)
08-23 15:08:49.330: E/AndroidRuntime(430): Caused by: android.view.InflateException: Binary XML file line #14: Error inflating class fragment
08-23 15:08:49.330: E/AndroidRuntime(430): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:582)
08-23 15:08:49.330: E/AndroidRuntime(430): at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
08-23 15:08:49.330: E/AndroidRuntime(430): at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
08-23 15:08:49.330: E/AndroidRuntime(430): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
08-23 15:08:49.330: E/AndroidRuntime(430): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
08-23 15:08:49.330: E/AndroidRuntime(430): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
08-23 15:08:49.330: E/AndroidRuntime(430): at android.app.Activity.setContentView(Activity.java:1647)
08-23 15:08:49.330: E/AndroidRuntime(430): at com.stpl.fasttrackbooking.OpenMapforLocating.onCreate(OpenMapforLocating.java:88)
08-23 15:08:49.330: E/AndroidRuntime(430): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-23 15:08:49.330: E/AndroidRuntime(430): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
08-23 15:08:49.330: E/AndroidRuntime(430): ... 11 more
08-23 15:08:49.330: E/AndroidRuntime(430): Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.maps.MapFragment: make sure class name exists, is public, and has an empty constructor that is public
08-23 15:08:49.330: E/AndroidRuntime(430): at android.support.v4.app.Fragment.instantiate(Fragment.java:401)
08-23 15:08:49.330: E/AndroidRuntime(430): at android.support.v4.app.Fragment.instantiate(Fragment.java:369)
08-23 15:08:49.330: E/AndroidRuntime(430): at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:272)
08-23 15:08:49.330: E/AndroidRuntime(430): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:558)
08-23 15:08:49.330: E/AndroidRuntime(430): ... 20 more
08-23 15:08:49.330: E/AndroidRuntime(430): Caused by: java.lang.ClassNotFoundException: com.google.android.gms.maps.MapFragment in loader dalvik.system.PathClassLoader[/system/framework/com.google.android.maps.jar:/data/app/com.stpl.fasttrackbooking-1.apk]
08-23 15:08:49.330: E/AndroidRuntime(430): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
08-23 15:08:49.330: E/AndroidRuntime(430): at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
08-23 15:08:49.330: E/AndroidRuntime(430): at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
08-23 15:08:49.330: E/AndroidRuntime(430): at android.support.v4.app.Fragment.instantiate(Fragment.java:391)
此代码在大中型屏幕上运行良好。我试图修改此代码以在小屏幕上工作。所以我在 res 下创建了 layout-small 文件夹并运行了这段代码。当单击“使用地图的地方”选项时,发生了上述错误。我不知道代码的哪一部分导致了这种错误。我在哪里可以更改代码。帮助我的朋友。