1
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;

//          case R.id.menu_search:
//              Toast.makeText(OpenMapforLocating.this, "Search is Selected",  Toast.LENGTH_SHORT).show();
   //               return true;
//   
//          case R.id.menu_share:
//              Toast.makeText(OpenMapforLocating.this, "Share is Selected", Toast.LENGTH_SHORT).show();
//              return true;
//   
//          case R.id.menu_delete:
//              Toast.makeText(OpenMapforLocating.this, "Delete is Selected", Toast.LENGTH_SHORT).show();
//              return true;
//   
//          case R.id.menu_preferences:
//              Toast.makeText(OpenMapforLocating.this, "Preferences is Selected", Toast.LENGTH_SHORT).show();
//              return true;
//   
        default:
            return super.onOptionsItemSelected(item);
        }
    }

  }

从手机复制的日志猫错误

  E/AndroidRuntime(10431): at android.app.ActivityThread.performLaunchActivity   
  E/AndroidRuntime(10431): ... 11 more 
  E/AndroidRuntime(10431): 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
 E/AndroidRuntime(10431): at  android.support.v4.app.FragmentActivity.onCreateView    
 E/AndroidRuntime(10431): at   android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:594) 
 E/AndroidRuntime(10431): ... 20 more 
 E/AndroidRuntime(10431): 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.s tpl.fasttrackbooking-1.apk]
 E/AndroidRuntime(10431): at  dalvik.system.PathClassLoader.findClass(PathClassLoader.java:271)
 E/AndroidRuntime(10431):   at java.lang.ClassLoader.loadClass  
 E/AndroidRuntime(10431):  at java.lang.ClassLoader.loadClass(ClassLoader.java:542) 

我读了这个(无法实例化片段确保类名存在,是公共的,并且有一个公共的空构造函数)问题答案,但现在我仍然无法在我的代码中找到错误。请帮我。很抱歉问了已经发布的相同问题。我不知道通过阅读这两个答案来找到解决方案。我通过模拟器运行意味着由于网络问题它显示错误。所以我在移动设备上运行并记录了上面提到的日志猫错误。请帮我。

4

0 回答 0