使用 MapView 抛出 ClassNotFoundException

发布于 2024-12-08 05:36:47 字数 9560 浏览 1 评论 0原文

我正在努力寻找这段代码锁定的原因。据我所知,我在清单文件中声明了正确的权限,如下所示。我尝试在实际设备(Droid x w./ 2.3.3)和 AVD 上运行它,但没有成功。我还附加了 logcat,这使我遇到了 classnotfound 异常。我已经清理了该项目,并验证了maps.jar 是否包含在googleapi 的包中,我也将其用作应用程序的目标。 (Google API 的 2.3.3)。不知道从这里去哪里。

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();
        }
    }





}'

清单:

<?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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

念﹏祤嫣 2024-12-15 05:36:47

在应用程序标签中添加使用库...(您在活动标签中添加...)

所以您的 androidmanifyst 如下::

<?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>

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>
无所谓啦 2024-12-15 05:36:47

您的清单中不应该是 MapViewActivity 而不是 MapviewActivity 吗?

Shouldn't it be MapViewActivity and not MapviewActivity in your manifest?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文