问题
I am struggling with finding the reason why this code locks up. As far as I know, I am declaring the correct permissions in my manifest file as shown below. I have tried running it on an actual device(Droid x w./ 2.3.3) and an AVD to no avail. I've attached the logcat as well which brought me to the classnotfound exception. I have cleaned the project, and verified that the maps.jar is included in the googleapi's package, which I've also used as the target for the application. (Google API's 2.3.3). Not sure where to go from here.
Activity Code:
'package com.SmartPark;
import java.util.List;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.widget.LinearLayout;
//import android.widget.TextView;
import android.widget.Toast;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
public class SmartParkActivity extends MapActivity {
//** Called when the activity is first created. */
LinearLayout linearlayout;
MapView mapView;
List<Overlay> mapOverlays;
Drawable drawable;
HelloItemizedOverlay itemizedOverlay;
GeoPoint point = new GeoPoint(42392400,-83134700);
OverlayItem overlayitem = new OverlayItem(point, "", "");
private LocationManager lm;
private LocationListener locationListener;
public double[] gps=new double[2];
public boolean setgps;
final Handler mHandler = new Handler();
//TextView tvl; //latitude display
// TextView tvlong;//longitude display
int lat2;
int longitude2;
public float z;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapOverlays = mapView.getOverlays();
drawable = this.getResources().getDrawable(R.drawable.greendot1);
itemizedOverlay = new HelloItemizedOverlay(drawable);
OverlayItem overlayitem = new OverlayItem(point, "", "");
itemizedOverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedOverlay);
// tvl = (TextView) this.findViewById(R.id.gpslview);
// tvlong = (TextView) this.findViewById(R.id.gpslongview);
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
@Override
protected void onStart() {
super.onStart();
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationListener = new MyLocationListener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 100, 0,
locationListener);
mHandler.postDelayed(mUpdateTimeTask, 500);
}
@Override
protected void onStop() {
super.onStop();
lm.removeUpdates(locationListener);
mHandler.removeCallbacks(mUpdateTimeTask);
}
@Override
protected void onDestroy() {
super.onDestroy();
lm.removeUpdates(locationListener);
mHandler.removeCallbacks(mUpdateTimeTask);
}
//}
// helper method to schedule the postdelayed mhandler at specified interval
private boolean foo() {
setgps=false;
mHandler.postDelayed(mUpdateTimeTask, 500);
return true;
};
//Method to acquire set gps variable and display it. update overlay if possible
private Runnable mUpdateTimeTask = new Runnable() {
public void run() {
mapOverlays.clear();
GeoPoint point = setgeopoint(gps[0],gps[1]);
OverlayItem overlayitem = new OverlayItem(point, "", "");
mapOverlays.add(itemizedOverlay);
itemizedOverlay.addOverlay(overlayitem);
// tvl.setText("Latitude:"+gps[0]);
// tvlong.setText("Longitude"+gps[1]);
// z=(float)android.location.Location.getSpeed();
setgps=foo(); //reschedules the task
};
};
//math functions
//convert gps double to an int and set to GeoPoint
public GeoPoint setgeopoint (double dl, double dlong) {
int intl = (int) (dl*1000000);
int intlong = (int) (dlong*1000000);
GeoPoint point= new GeoPoint ( intl, intlong);
return point;
}
public class MyLocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location loc)
{
if (loc != null) {
Toast.makeText(
getBaseContext(),
"Location changed : Lat: " + loc.getLatitude()
+ " Lng: " + loc.getLongitude(),
Toast.LENGTH_SHORT).show();
gps[0]= lat2;
gps[1]=longitude2;
gps[0] = loc.getLatitude();
gps[1] = loc.getLongitude();
z=loc.getSpeed();
}
}
public void onProviderDisabled(String provider)
{
Toast.makeText( getApplicationContext(),"Gps Disabled",Toast.LENGTH_SHORT ).show();
}
public void onProviderEnabled(String provider)
{
Toast.makeText( getApplicationContext(),"Gps Enabled",Toast.LENGTH_SHORT).show();
}
public void onStatusChanged(String provider, int status, Bundle extras)
{
Toast.makeText( getApplicationContext(),"Status changed",Toast.LENGTH_SHORT).show();
}
}
}'
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.SmartPark"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses- permission>
<uses-permission android:name="android.permission.ACCESS_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_GPS"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_ASSISTED_GPS"></uses-permission>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<uses-library android:name="com.google.android.maps" />
<activity android:name=".MapviewActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Logcat:
10-06 09:19:07.239: ERROR/AndroidRuntime(13602): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.SmartPark/com.SmartPark.MapviewActivity}: java.lang.ClassNotFoundException: com.SmartPark.MapviewActivity in loader dalvik.system.PathClassLoader[/system/framework/com.google.android.maps.jar:/data/app/com.SmartPark-2.apk]
10-06 09:19:07.239: ERROR/AndroidRuntime(13602): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1618)
10-06 09:19:07.239: ERROR/AndroidRuntime(13602): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1716)
10-06 09:19:07.239: ERROR/AndroidRuntime(13602): at android.app.ActivityThread.access$1500(ActivityThread.java:124)
10-06 09:19:07.239: ERROR/AndroidRuntime(13602): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:968)
10-06 09:19:07.239: ERROR/AndroidRuntime(13602): at android.os.Handler.dispatchMessage(Handler.java:99)
10-06 09:19:07.239: ERROR/AndroidRuntime(13602): at android.os.Looper.loop(Looper.java:123)
10-06 09:19:07.239: ERROR/AndroidRuntime(13602): at android.app.ActivityThread.main(ActivityThread.java:3806)
10-06 09:19:07.239: ERROR/AndroidRuntime(13602): at java.lang.reflect.Method.invokeNative(Native Method)
10-06 09:19:07.239: ERROR/AndroidRuntime(13602): at java.lang.reflect.Method.invoke(Method.java:507)
10-06 09:19:07.239: ERROR/AndroidRuntime(13602): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-06 09:19:07.239: ERROR/AndroidRuntime(13602): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-06 09:19:07.239: ERROR/AndroidRuntime(13602): at dalvik.system.NativeStart.main(Native Method)
10-06 09:19:07.239: ERROR/AndroidRuntime(13602): Caused by: java.lang.ClassNotFoundException: com.SmartPark.MapviewActivity in loader dalvik.system.PathClassLoader[/system/framework/com.google.android.maps.jar:/data/app/com.SmartPark-2.apk]
10-06 09:19:07.239: ERROR/AndroidRuntime(13602): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
10-06 09:19:07.239: ERROR/AndroidRuntime(13602): at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
10-06 09:19:07.239: ERROR/AndroidRuntime(13602): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
10-06 09:19:07.239: ERROR/AndroidRuntime(13602): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
10-06 09:19:07.239: ERROR/AndroidRuntime(13602): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1610)
10-06 09:19:07.239: ERROR/AndroidRuntime(13602): ... 11 more
回答1:
Shouldn't it be MapViewActivity
and not MapviewActivity
in your manifest?
回答2:
add uses library in application tag...(You add in activity tag...)
So your androidmanifiest is below ::
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.SmartPark"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_GPS"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_ASSISTED_GPS"> </uses-permission>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<uses-library android:name="com.google.android.maps"></uses-library>
<activity android:name=".SmartParkActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<uses-library android:name="com.google.android.maps" />
</activity>
</application>
</manifest>
来源:https://stackoverflow.com/questions/7675310/classnotfoundexception-thrown-using-mapview