1

我想在 android 2.2 中使用 google api v2。

我已经完成了它的所有先决条件。

喜欢:谷歌地图 api 密钥。

  1. 安卓支持-v4.jar

  2. google-play-services_lib.jar

  3. google-play-services.jar

我得到这个错误。

02-26 09:32:04.055: E/AndroidRuntime(334): FATAL EXCEPTION: main
02-26 09:32:04.055: E/AndroidRuntime(334): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.mapfragment/com.mapfragment.MainActivity}: java.lang.ClassNotFoundException: com.mapfragment.MainActivity in loader dalvik.system.PathClassLoader[/system/framework/com.google.android.maps.jar:/data/app/com.mapfragment-2.apk]
02-26 09:32:04.055: E/AndroidRuntime(334):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
02-26 09:32:04.055: E/AndroidRuntime(334):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-26 09:32:04.055: E/AndroidRuntime(334):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-26 09:32:04.055: E/AndroidRuntime(334):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-26 09:32:04.055: E/AndroidRuntime(334):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-26 09:32:04.055: E/AndroidRuntime(334):  at android.os.Looper.loop(Looper.java:123)
02-26 09:32:04.055: E/AndroidRuntime(334):  at android.app.ActivityThread.main(ActivityThread.java:4627)
02-26 09:32:04.055: E/AndroidRuntime(334):  at java.lang.reflect.Method.invokeNative(Native Method)
02-26 09:32:04.055: E/AndroidRuntime(334):  at java.lang.reflect.Method.invoke(Method.java:521)
02-26 09:32:04.055: E/AndroidRuntime(334):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-26 09:32:04.055: E/AndroidRuntime(334):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-26 09:32:04.055: E/AndroidRuntime(334):  at dalvik.system.NativeStart.main(Native Method)
02-26 09:32:04.055: E/AndroidRuntime(334): Caused by: java.lang.ClassNotFoundException: com.mapfragment.MainActivity in loader dalvik.system.PathClassLoader[/system/framework/com.google.android.maps.jar:/data/app/com.mapfragment-2.apk]
02-26 09:32:04.055: E/AndroidRuntime(334):  at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
02-26 09:32:04.055: E/AndroidRuntime(334):  at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
02-26 09:32:04.055: E/AndroidRuntime(334):  at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
02-26 09:32:04.055: E/AndroidRuntime(334):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
02-26 09:32:04.055: E/AndroidRuntime(334):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
02-26 09:32:04.055: E/AndroidRuntime(334):  ... 11 more

我的代码:

MainActivity.java

package com.mapfragment;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;

import com.google.android.gms.common.GooglePlayServicesNotAvailableException;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapsInitializer;
import com.google.android.gms.maps.SupportMapFragment;

public class MainActivity extends FragmentActivity {
GoogleMap mapView;
com.google.android.gms.maps.Projection projection;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    FragmentTransaction mTransaction = getSupportFragmentManager()
            .beginTransaction();
    SupportMapFragment mFRaFragment = new MapFragmentD();
    mTransaction.add(R.id.mainl, mFRaFragment);
    mTransaction.commit();

    try {
        MapsInitializer.initialize(this);
    } catch (GooglePlayServicesNotAvailableException e) {
        e.printStackTrace();
    }
}

}

MapFragmentD.java

package com.mapfragment;

import android.app.Activity;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapFragmentD extends SupportMapFragment {

GoogleMap mapView;

@Override
public void onCreate(Bundle arg0) {
    super.onCreate(arg0);
}

@Override
public View onCreateView(LayoutInflater mInflater, ViewGroup arg1,
        Bundle arg2) {
    return super.onCreateView(mInflater, arg1, arg2);
}

@Override
public void onInflate(Activity arg0, AttributeSet arg1, Bundle arg2) {
    super.onInflate(arg0, arg1, arg2);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    mapView = getMap();
    MarkerOptions markerOptions = new MarkerOptions();
    markerOptions.draggable(true);
    markerOptions.position(new LatLng(23.231251f, 71.648437f));
    markerOptions.icon(BitmapDescriptorFactory.defaultMarker());
    mapView.addMarker(markerOptions);
}
}

主要的.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="@+id/mainl"
android:layout_height="match_parent" >

<!--     <com.mapfragment.MapFragmentD
    android:id="@+id/mapview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:clickable="true" /> -->

</FrameLayout>

地图片段.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<com.google.android.maps.MapView
    android:id="@+id/mapview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:apiKey="0WyiQdWfvE1GnA4nWEz8rX1Ezf-rWMEDShPt5BQ"
    android:clickable="true" />

</RelativeLayout>

谷歌 api v2 的几个程序我每次都会尝试 bt,这个唯一的错误发生。请帮我解决这个错误..

提前致谢。

4

1 回答 1

1

您需要在清单中添加以下内容。

<permission
    android:name="package.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />


<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

<uses-permission android:name="package.permission.MAPS_RECEIVE" />

并在应用程序标签中

    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="@string/map_key" />
于 2013-02-26T04:47:57.713 回答