android cnfException
我正在尝试编写一个在地图上显示当前设备位置的应用程序。当我尝试将其安装到设备上时,它说找不到我的主要活动类。它还说 dalvikvm 无法解析该类的超类,这显然是 MapActivity。有什么想法吗?谢谢。
import java.util.List;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ZoomControls;
public class WcFinderActivity extends MapActivity implements LocationListener {
/** Called when the activity is first created. */
EditText txted = null;
Button btnSimple = null;
MapView gMapView = null;
MapController mc = null;
Drawable defaultMarker = null;
GeoPoint p = null;
double latitude = 18.9599990845, longitude = 72.819999694;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Creating TextBox displying Lat, Long
txted = (EditText) findViewById(R.id.id1);
String currentLocation = "Lat: " + latitude + " Lng: " + longitude;
txted.setText(currentLocation);
// Creating and initializing Map
gMapView = (MapView) findViewById(R.id.myGMap);
p = new GeoPoint((int) (latitude * 1000000), (int) (longitude * 1000000));
gMapView.setSatellite(true);
mc = gMapView.getController();
mc.setCenter(p);
mc.setZoom(14);
// Add a location mark
MyLocationOverlay myLocationOverlay = new MyLocationOverlay();
List<Overlay> list = gMapView.getOverlays();
list.add(myLocationOverlay);
// Adding zoom controls to Map
ZoomControls zoomControls = (ZoomControls) gMapView.getZoomControls();
zoomControls.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
gMapView.addView(zoomControls);
gMapView.displayZoomControls(true);
// Getting locationManager and reflecting changes over map if distance travel by
// user is greater than 500m from current location.
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 500.0f, this);
}
/* This method is called when use position will get changed */
public void onLocationChanged(Location location) {
if (location != null) {
double lat = location.getLatitude();
double lng = location.getLongitude();
String currentLocation = "Lat: " + lat + " Lng: " + lng;
txted.setText(currentLocation);
p = new GeoPoint((int) lat * 1000000, (int) lng * 1000000);
mc.animateTo(p);
}
}
public void onProviderDisabled(String provider) {
// required for interface, not used
}
public void onProviderEnabled(String provider) {
// required for interface, not used
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// required for interface, not used
}
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
/* User can zoom in/out using keys provided on keypad */
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_I) {
gMapView.getController().setZoom(gMapView.getZoomLevel() + 1);
return true;
} else if (keyCode == KeyEvent.KEYCODE_O) {
gMapView.getController().setZoom(gMapView.getZoomLevel() - 1);
return true;
} else if (keyCode == KeyEvent.KEYCODE_S) {
gMapView.setSatellite(true);
return true;
} else if (keyCode == KeyEvent.KEYCODE_T) {
gMapView.setTraffic(true);
return true;
}
return false;
}
/* Class overload draw method which actually plot a marker,text etc. on Map */
protected class MyLocationOverlay extends com.google.android.maps.Overlay {
@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
Paint paint = new Paint();
super.draw(canvas, mapView, shadow);
// Converts lat/lng-Point to OUR coordinates on the screen.
Point myScreenCoords = new Point();
mapView.getProjection().toPixels(p, myScreenCoords);
paint.setStrokeWidth(1);
paint.setARGB(255, 255, 255, 255);
paint.setStyle(Paint.Style.STROKE);
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
canvas.drawBitmap(bmp, myScreenCoords.x, myScreenCoords.y, paint);
canvas.drawText("I am here...", myScreenCoords.x, myScreenCoords.y, paint);
return true;
}
}
}
01-18 15:04:07.223:W/dalvikvm(3554):无法解析 Lcom/tecmark/WcFinderActivity 的超类; (23) 01-18 15:04:07.238: W/dalvikvm(3554): 类“Lcom/tecmark/WcFinderActivity;”的链接失败的 01-18 15:04:07.238:D/AndroidRuntime(3554):关闭虚拟机 01-18 15:04:07.238: W/dalvikvm(3554): threadid=3: 线程因未捕获的异常而退出 (group=0x4001b180) 01-18 15:04:07.238:E / AndroidRuntime(3554):未捕获的处理程序:由于未捕获的异常,线程主退出 01-18 15:04:07.278: E/AndroidRuntime(3554): java.lang.RuntimeException: 无法实例化活动 ComponentInfo{com.tecmark/com.tecmark.WcFinderActivity}: java.lang.ClassNotFoundException: com.tecmark.WcFinderActivity在加载器 dalvik.system.PathClassLoader@43e57a18 中 01-18 15:04:07.278:E / AndroidRuntime(3554):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417) 01-18 15:04:07.278:E / AndroidRuntime(3554):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512) 01-18 15:04:07.278: E/AndroidRuntime(3554): 在 android.app.ActivityThread.access$2200(ActivityThread.java:119) 01-18 15:04:07.278: E/AndroidRuntime(3554): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863) 01-18 15:04:07.278: E/AndroidRuntime(3554): 在 android.os.Handler.dispatchMessage(Handler.java:99) 01-18 15:04:07.278: E/AndroidRuntime(3554): 在 android.os.Looper.loop(Looper.java:123) 01-18 15:04:07.278: E/AndroidRuntime(3554): 在 android.app.ActivityThread.main(ActivityThread.java:4363) 01-18 15:04:07.278:E / AndroidRuntime(3554):在java.lang.reflect.Method.invokeNative(本机方法) 01-18 15:04:07.278: E/AndroidRuntime(3554): 在 java.lang.reflect.Method.invoke(Method.java:521) 01-18 15:04:07.278: E/AndroidRuntime(3554): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 01-18 15:04:07.278: E/AndroidRuntime(3554): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 01-18 15:04:07.278:E / AndroidRuntime(3554):在dalvik.system.NativeStart.main(本机方法) 01-18 15:04:07.278:E / AndroidRuntime(3554):引起:java.lang.ClassNotFoundException:加载程序dalvik.system.PathClassLoader@43e57a18中的com.tecmark.WcFinderActivity 01-18 15:04:07.278:E / AndroidRuntime(3554):在dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243) 01-18 15:04:07.278: E/AndroidRuntime(3554): 在 java.lang.ClassLoader.loadClass(ClassLoader.java:573) 01-18 15:04:07.278: E/AndroidRuntime(3554): 在 java.lang.ClassLoader.loadClass(ClassLoader.java:532) 01-18 15:04:07.278: E/AndroidRuntime(3554): 在 android.app.Instrumentation.newActivity(Instrumentation.java:1021) 01-18 15:04:07.278:E / AndroidRuntime(3554):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2409) 01-18 15:04:07.278: E/AndroidRuntime(3554): ... 11 更多
。
<?xml version="1.0" ?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tecmark"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-library android:name="com.google.android.maps" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".WcFinderActivity"
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>
i'm trying to code an app that displays the current device position on a map. When i try to install it onto the device it says it cant find the class that is my main activity. it also says that dalvikvm can't resolve it the class' superclass, which is clearly MapActivity. Any ideas? thanks.
import java.util.List;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ZoomControls;
public class WcFinderActivity extends MapActivity implements LocationListener {
/** Called when the activity is first created. */
EditText txted = null;
Button btnSimple = null;
MapView gMapView = null;
MapController mc = null;
Drawable defaultMarker = null;
GeoPoint p = null;
double latitude = 18.9599990845, longitude = 72.819999694;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Creating TextBox displying Lat, Long
txted = (EditText) findViewById(R.id.id1);
String currentLocation = "Lat: " + latitude + " Lng: " + longitude;
txted.setText(currentLocation);
// Creating and initializing Map
gMapView = (MapView) findViewById(R.id.myGMap);
p = new GeoPoint((int) (latitude * 1000000), (int) (longitude * 1000000));
gMapView.setSatellite(true);
mc = gMapView.getController();
mc.setCenter(p);
mc.setZoom(14);
// Add a location mark
MyLocationOverlay myLocationOverlay = new MyLocationOverlay();
List<Overlay> list = gMapView.getOverlays();
list.add(myLocationOverlay);
// Adding zoom controls to Map
ZoomControls zoomControls = (ZoomControls) gMapView.getZoomControls();
zoomControls.setLayoutParams(new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
gMapView.addView(zoomControls);
gMapView.displayZoomControls(true);
// Getting locationManager and reflecting changes over map if distance travel by
// user is greater than 500m from current location.
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000L, 500.0f, this);
}
/* This method is called when use position will get changed */
public void onLocationChanged(Location location) {
if (location != null) {
double lat = location.getLatitude();
double lng = location.getLongitude();
String currentLocation = "Lat: " + lat + " Lng: " + lng;
txted.setText(currentLocation);
p = new GeoPoint((int) lat * 1000000, (int) lng * 1000000);
mc.animateTo(p);
}
}
public void onProviderDisabled(String provider) {
// required for interface, not used
}
public void onProviderEnabled(String provider) {
// required for interface, not used
}
public void onStatusChanged(String provider, int status, Bundle extras) {
// required for interface, not used
}
protected boolean isRouteDisplayed() {
// TODO Auto-generated method stub
return false;
}
/* User can zoom in/out using keys provided on keypad */
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_I) {
gMapView.getController().setZoom(gMapView.getZoomLevel() + 1);
return true;
} else if (keyCode == KeyEvent.KEYCODE_O) {
gMapView.getController().setZoom(gMapView.getZoomLevel() - 1);
return true;
} else if (keyCode == KeyEvent.KEYCODE_S) {
gMapView.setSatellite(true);
return true;
} else if (keyCode == KeyEvent.KEYCODE_T) {
gMapView.setTraffic(true);
return true;
}
return false;
}
/* Class overload draw method which actually plot a marker,text etc. on Map */
protected class MyLocationOverlay extends com.google.android.maps.Overlay {
@Override
public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) {
Paint paint = new Paint();
super.draw(canvas, mapView, shadow);
// Converts lat/lng-Point to OUR coordinates on the screen.
Point myScreenCoords = new Point();
mapView.getProjection().toPixels(p, myScreenCoords);
paint.setStrokeWidth(1);
paint.setARGB(255, 255, 255, 255);
paint.setStyle(Paint.Style.STROKE);
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
canvas.drawBitmap(bmp, myScreenCoords.x, myScreenCoords.y, paint);
canvas.drawText("I am here...", myScreenCoords.x, myScreenCoords.y, paint);
return true;
}
}
}
01-18 15:04:07.223: W/dalvikvm(3554): Unable to resolve superclass of Lcom/tecmark/WcFinderActivity; (23)
01-18 15:04:07.238: W/dalvikvm(3554): Link of class 'Lcom/tecmark/WcFinderActivity;' failed
01-18 15:04:07.238: D/AndroidRuntime(3554): Shutting down VM
01-18 15:04:07.238: W/dalvikvm(3554): threadid=3: thread exiting with uncaught exception (group=0x4001b180)
01-18 15:04:07.238: E/AndroidRuntime(3554): Uncaught handler: thread main exiting due to uncaught exception
01-18 15:04:07.278: E/AndroidRuntime(3554): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.tecmark/com.tecmark.WcFinderActivity}: java.lang.ClassNotFoundException: com.tecmark.WcFinderActivity in loader dalvik.system.PathClassLoader@43e57a18
01-18 15:04:07.278: E/AndroidRuntime(3554): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
01-18 15:04:07.278: E/AndroidRuntime(3554): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
01-18 15:04:07.278: E/AndroidRuntime(3554): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
01-18 15:04:07.278: E/AndroidRuntime(3554): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
01-18 15:04:07.278: E/AndroidRuntime(3554): at android.os.Handler.dispatchMessage(Handler.java:99)
01-18 15:04:07.278: E/AndroidRuntime(3554): at android.os.Looper.loop(Looper.java:123)
01-18 15:04:07.278: E/AndroidRuntime(3554): at android.app.ActivityThread.main(ActivityThread.java:4363)
01-18 15:04:07.278: E/AndroidRuntime(3554): at java.lang.reflect.Method.invokeNative(Native Method)
01-18 15:04:07.278: E/AndroidRuntime(3554): at java.lang.reflect.Method.invoke(Method.java:521)
01-18 15:04:07.278: E/AndroidRuntime(3554): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
01-18 15:04:07.278: E/AndroidRuntime(3554): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
01-18 15:04:07.278: E/AndroidRuntime(3554): at dalvik.system.NativeStart.main(Native Method)
01-18 15:04:07.278: E/AndroidRuntime(3554): Caused by: java.lang.ClassNotFoundException: com.tecmark.WcFinderActivity in loader dalvik.system.PathClassLoader@43e57a18
01-18 15:04:07.278: E/AndroidRuntime(3554): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
01-18 15:04:07.278: E/AndroidRuntime(3554): at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
01-18 15:04:07.278: E/AndroidRuntime(3554): at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
01-18 15:04:07.278: E/AndroidRuntime(3554): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
01-18 15:04:07.278: E/AndroidRuntime(3554): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2409)
01-18 15:04:07.278: E/AndroidRuntime(3554): ... 11 more
.
<?xml version="1.0" ?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.tecmark"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-library android:name="com.google.android.maps" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".WcFinderActivity"
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
抱歉我犯了一个愚蠢的错误。我将 use-library 标签移至应用程序标签内,它现在可以编译并安装在手机上。我认为这也解释了超类不能被解析的错误。我的活动扩展了 MapActivity,这可能是在 google api 库中定义的,但未在应用程序标记之间声明。
Sorry i made a silly mistake. i moved the uses-library tag inside the application tag and it compiles and installs on phone now. i think that explains the superclass can not be resloved error as well. my activity extends MapActivity, this is probably defined in the google api library that was not declared between the application tags.