Problems trying to use Android Mapview classes

2019-09-04 00:00发布

问题:

I'm a noob to Android so bear with me and apologies if my post is moronic. Basically, I'm trying to add a mapview to the XML which is causing a few problems, the error I'm getting is:

06-30 12:29:04.760: ERROR/AndroidRuntime(320): java.lang.NoClassDefFoundError: package.android.mapclass

From what I can tell the error is happening on this call:

Intent i = new Intent(oldclass.this, mapclass.class);

The mapclass itself is:

package package.android;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import com.google.android.maps.MapView;
import com.google.android.maps.MapActivity;
import android.util.Log;

public class mapclass extends MapActivity {
   public void onCreate(Bundle savedInstanceState)
   {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.viewmap);

   protected boolean isRouteDisplayed() {
        return false;
   }
}

Here's where it gets interesting, if I swap out

public class mapclass extends MapActivity {

and replace it with

public class mapclass extends Activity {

Then it works absolutely correctly (albeit without calling the calling the MapActivity). There is a reference to the maps in the manifest:

<manifest   xmlns:android="http://schemas.android.com/apk/res/android"
        package="package.android"
        android:versionCode="1"
        android:versionName="1.0">
<uses-sdk   android:minSdkVersion="7" />
<uses-permission android:name="android.permission.LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" /> 
<uses-library android:name="com.google.android.maps" />
<uses-library android:name="com.google.android.maps.MapView" />
<uses-library android:name="com.google.android.maps.MapActivity" />

<application    android:icon="@drawable/icon"
                android:label="@string/app_name">

I'm guessing that I'm doing something horribly wrong with regards to the activity in the xml? I've tried fiddling but so far to no avail, does anyone have any ideas?

Thanks,

回答1:

Step #1: Remove your second and third <uses-library> elements.

Step #2: Change your package to not include package as part of the name, to minimize odds of reserved-keyword issues.

Step #3: Remove your <uses-permission android:name="android.permission.LOCATION"/> line, as there is no such permission.

Step #4: Run your application on a device or emulator that has Google Maps installed.

Your real problem is Step #4 -- the first three are just to clean things up a bit. FWIW, I also recommend you follow CamelCase conventions for your class names.