在MapsActivity中添加MapOverlay类后,我的应用程序在开始时崩溃。我添加了Mapoverlay类& onTouchevent得到lat&当我触摸地图时没有它。没有Mapoverlay课它正在工作,但我需要得到lat&很长一段时间在地图上触摸。所以什么是solurion.Help me pla .... 的 MapsActivity.java
package com.mamun.tasktest;
import java.io.IOException;
import java.util.ArrayList;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.location.LocationClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.maps.MapActivity;
public class MapsActivity<GeoPoint, OverlayItem> extends MapActivity implements
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener, LocationListener {
private LocationManager manager;
private TextView tvAddress;
private Button btnSearch;
private EditText etSearch;
private LocationClient locationClient;
private GoogleMap googleMap;
private MapFragment mapFragment;
public Bundle mapOverlays;
MapView mapView;
GeoPoint p;
class MapOverlay extends com.google.android.maps.Overlay
{
@Override
public boolean onTouchEvent(MotionEvent e,
com.google.android.maps.MapView mapView) {
// TODO Auto-generated method stub
if(e.getAction()==1){
com.google.android.maps.GeoPoint p = mapView.getProjection().fromPixels((int) e.getX(), (int) e.getY());
Toast.makeText(getBaseContext(),
p.getLatitudeE6() / 1E6 + "," +
p.getLongitudeE6() /1E6 ,
Toast.LENGTH_SHORT).show();
}
return false;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
manager = (LocationManager) getSystemService(LOCATION_SERVICE);
tvAddress = (TextView) findViewById(R.id.tvaddress);
btnSearch = (Button) findViewById(R.id.btnSearch);
etSearch = (EditText) findViewById(R.id.etSearch);
mapFragment = (MapFragment) getFragmentManager().findFragmentById(
R.id.maps);
googleMap = mapFragment.getMap();
locationClient = new LocationClient(this, this, this);
}
public void onSearch(View v) {
// Getting user input location
String location = etSearch.getText().toString();
if (location != null && !location.equals("")) {
new GeocoderTask().execute(location);
}
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
locationClient.connect();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
locationClient.disconnect();
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
}
@Override
public void onConnectionFailed(ConnectionResult result) {
}
@Override
public void onConnected(Bundle connectionHint) {
try {
Location currentLocation = locationClient.getLastLocation();
double lat = currentLocation.getLatitude();
double lng = currentLocation.getLongitude();
// txtLocation.setText(lat + ", " + lng);
Geocoder geocoder = new Geocoder(this);
ArrayList<Address> address = (ArrayList<Address>) geocoder
.getFromLocation(currentLocation.getLatitude(),
currentLocation.getLongitude(), 5);
Address addr = address.get(0);
String currentAddress = (addr.getAddressLine(0) + "-"
+ addr.getAdminArea() + "-" + addr.getLocality() + "-"
+ addr.getPostalCode() + "-" + addr.getCountryCode());
MarkerOptions options = new MarkerOptions();
options.position(new LatLng(lat, lng));
options.title(currentAddress);
options.snippet("Current location");
options.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_AZURE));
if (googleMap != null) {
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(
new LatLng(lat, lng), 14.0f));
googleMap.addMarker(options);
} else {
Toast.makeText(getApplicationContext(), "Map is null",
Toast.LENGTH_LONG).show();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void onDisconnected() {
// TODO Auto-generated method stub
}
// An AsyncTask class for accessing the GeoCoding Web Service
private class GeocoderTask extends
AsyncTask<String, Void, ArrayList<Address>> {
@Override
protected ArrayList<Address> doInBackground(String... locationName) {
// Creating an instance of Geocoder class
Geocoder geocoder = new Geocoder(getBaseContext());
ArrayList<Address> addresses = null;
try {
// Getting a maximum of 3 Address that matches the input text
addresses = (ArrayList<Address>) geocoder.getFromLocationName(
locationName[0], 3);
} catch (IOException e) {
e.printStackTrace();
}
return addresses;
}
@Override
protected void onPostExecute(ArrayList<Address> addresses) {
if (addresses == null || addresses.size() == 0) {
Toast.makeText(getBaseContext(), "No Location found",
Toast.LENGTH_SHORT).show();
return;
}
// Clears all the existing markers on the map
googleMap.clear();
// Adding Markers on Google Map for each matching address
for (int i = 0; i < addresses.size(); i++) {
Address address = (Address) addresses.get(i);
// Creating an instance of GeoPoint, to display in Google Map
LatLng latLng;
latLng = new LatLng(address.getLatitude(),
address.getLongitude());
String addressText = String.format(
"%s, %s",
address.getMaxAddressLineIndex() > 0 ? address
.getAddressLine(0) : "", address
.getCountryName());
MarkerOptions markerOptions = new MarkerOptions();
// markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title(addressText);
googleMap.addMarker(markerOptions);
// Locate the first location
if (i == 0)
googleMap.animateCamera(CameraUpdateFactory
.newLatLng(latLng));
}
}
}
@Override
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
}
*的Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mamun.tasktest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="16" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="com.mamun.tasktest.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-library android:name="com.google.android.maps"/>
<permission
android:name="com.mamun.tasktest.permission.MAPS_RECEIVE"
android:protectionLevel="signature" >
</permission>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.mamun.tasktest.MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait" >
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyCgGng3iaqbTxJ3B_lYemZBEqXOonUtFEI" />
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity android:name=".MapsActivity"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
logcat的
02-28 16:52:05.369: E/AndroidRuntime(11464): FATAL EXCEPTION: main
02-28 16:52:05.369: E/AndroidRuntime(11464): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.mamun.tasktest/com.mamun.tasktest.MapsActivity}: java.lang.ClassNotFoundException: Didn't find class "com.mamun.tasktest.MapsActivity" on path: DexPathList[[zip file "/data/app/com.mamun.tasktest-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.mamun.tasktest-1, /vendor/lib, /system/lib]]
02-28 16:52:05.369: E/AndroidRuntime(11464): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
02-28 16:52:05.369: E/AndroidRuntime(11464): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
02-28 16:52:05.369: E/AndroidRuntime(11464): at android.app.ActivityThread.access$600(ActivityThread.java:162)
02-28 16:52:05.369: E/AndroidRuntime(11464): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
02-28 16:52:05.369: E/AndroidRuntime(11464): at android.os.Handler.dispatchMessage(Handler.java:107)
02-28 16:52:05.369: E/AndroidRuntime(11464): at android.os.Looper.loop(Looper.java:194)
02-28 16:52:05.369: E/AndroidRuntime(11464): at android.app.ActivityThread.main(ActivityThread.java:5371)
02-28 16:52:05.369: E/AndroidRuntime(11464): at java.lang.reflect.Method.invokeNative(Native Method)
02-28 16:52:05.369: E/AndroidRuntime(11464): at java.lang.reflect.Method.invoke(Method.java:525)
02-28 16:52:05.369: E/AndroidRuntime(11464): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
02-28 16:52:05.369: E/AndroidRuntime(11464): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
02-28 16:52:05.369: E/AndroidRuntime(11464): at dalvik.system.NativeStart.main(Native Method)
02-28 16:52:05.369: E/AndroidRuntime(11464): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.mamun.tasktest.MapsActivity" on path: DexPathList[[zip file "/data/app/com.mamun.tasktest-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.mamun.tasktest-1, /vendor/lib, /system/lib]]
02-28 16:52:05.369: E/AndroidRuntime(11464): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53)
02-28 16:52:05.369: E/AndroidRuntime(11464): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
02-28 16:52:05.369: E/AndroidRuntime(11464): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
02-28 16:52:05.369: E/AndroidRuntime(11464): at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
02-28 16:52:05.369: E/AndroidRuntime(11464): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2260)
02-28 16:52:05.369: E/AndroidRuntime(11464): ... 11 more