Google Maps v2:如何将google-play-service.jar导入Android依赖项

时间:2013-09-03 08:55:55

标签: android eclipse android-maps-v2

我创建了Google Maps v2应用。我一步一步地按照教程。当我推出这个应用程序时。

显示弹出“不幸的是,谷歌地图v2已停止”。

这是logcat ......

09-03 04:51:33.296: E/AndroidRuntime(2148): FATAL EXCEPTION: main
09-03 04:51:33.296: E/AndroidRuntime(2148): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.achson.googlemapsv2/com.achson.googlemapsv2.MainActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
09-03 04:51:33.296: E/AndroidRuntime(2148):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
09-03 04:51:33.296: E/AndroidRuntime(2148):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
09-03 04:51:33.296: E/AndroidRuntime(2148):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-03 04:51:33.296: E/AndroidRuntime(2148):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
09-03 04:51:33.296: E/AndroidRuntime(2148):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-03 04:51:33.296: E/AndroidRuntime(2148):     at android.os.Looper.loop(Looper.java:137)
09-03 04:51:33.296: E/AndroidRuntime(2148):     at android.app.ActivityThread.main(ActivityThread.java:5103)
09-03 04:51:33.296: E/AndroidRuntime(2148):     at java.lang.reflect.Method.invokeNative(Native Method)
09-03 04:51:33.296: E/AndroidRuntime(2148):     at java.lang.reflect.Method.invoke(Method.java:525)
09-03 04:51:33.296: E/AndroidRuntime(2148):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-03 04:51:33.296: E/AndroidRuntime(2148):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-03 04:51:33.296: E/AndroidRuntime(2148):     at dalvik.system.NativeStart.main(Native Method)
09-03 04:51:33.296: E/AndroidRuntime(2148): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
09-03 04:51:33.296: E/AndroidRuntime(2148):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
09-03 04:51:33.296: E/AndroidRuntime(2148):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
09-03 04:51:33.296: E/AndroidRuntime(2148):     at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
09-03 04:51:33.296: E/AndroidRuntime(2148):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
09-03 04:51:33.296: E/AndroidRuntime(2148):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
09-03 04:51:33.296: E/AndroidRuntime(2148):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
09-03 04:51:33.296: E/AndroidRuntime(2148):     at android.app.Activity.setContentView(Activity.java:1895)
09-03 04:51:33.296: E/AndroidRuntime(2148):     at com.achson.googlemapsv2.MainActivity.onCreate(MainActivity.java:18)
09-03 04:51:33.296: E/AndroidRuntime(2148):     at android.app.Activity.performCreate(Activity.java:5133)
09-03 04:51:33.296: E/AndroidRuntime(2148):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
09-03 04:51:33.296: E/AndroidRuntime(2148):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
09-03 04:51:33.296: E/AndroidRuntime(2148):     ... 11 more
09-03 04:51:33.296: E/AndroidRuntime(2148): Caused by: java.lang.ClassCastException: com.google.android.gms.maps.SupportMapFragment cannot be cast to android.app.Fragment
09-03 04:51:33.296: E/AndroidRuntime(2148):     at android.app.Fragment.instantiate(Fragment.java:585)
09-03 04:51:33.296: E/AndroidRuntime(2148):     at android.app.Fragment.instantiate(Fragment.java:560)
09-03 04:51:33.296: E/AndroidRuntime(2148):     at android.app.Activity.onCreateView(Activity.java:4738)
09-03 04:51:33.296: E/AndroidRuntime(2148):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689)
09-03 04:51:33.296: E/AndroidRuntime(2148):     ... 21 more

我认为google-play-services不在Android Dependencies文件夹中。

但它位于Android Private Libraries文件夹中。我不知道如何导入Android Dependencies文件夹。有谁知道?请分享一下。

09-06 15:29:30.480: E/AndroidRuntime(1260): FATAL EXCEPTION: main
09-06 15:29:30.480: E/AndroidRuntime(1260): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.googlemapsv2/com.example.googlemapsv2.MainActivity}: java.lang.NullPointerException: CameraUpdateFactory is not initialized
09-06 15:29:30.480: E/AndroidRuntime(1260):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
09-06 15:29:30.480: E/AndroidRuntime(1260):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
09-06 15:29:30.480: E/AndroidRuntime(1260):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
09-06 15:29:30.480: E/AndroidRuntime(1260):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
09-06 15:29:30.480: E/AndroidRuntime(1260):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-06 15:29:30.480: E/AndroidRuntime(1260):     at android.os.Looper.loop(Looper.java:137)
09-06 15:29:30.480: E/AndroidRuntime(1260):     at android.app.ActivityThread.main(ActivityThread.java:5041)
09-06 15:29:30.480: E/AndroidRuntime(1260):     at java.lang.reflect.Method.invokeNative(Native Method)
09-06 15:29:30.480: E/AndroidRuntime(1260):     at java.lang.reflect.Method.invoke(Method.java:511)
09-06 15:29:30.480: E/AndroidRuntime(1260):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
09-06 15:29:30.480: E/AndroidRuntime(1260):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
09-06 15:29:30.480: E/AndroidRuntime(1260):     at dalvik.system.NativeStart.main(Native Method)
09-06 15:29:30.480: E/AndroidRuntime(1260): Caused by: java.lang.NullPointerException: CameraUpdateFactory is not initialized
09-06 15:29:30.480: E/AndroidRuntime(1260):     at com.google.android.gms.internal.ac.a(Unknown Source)
09-06 15:29:30.480: E/AndroidRuntime(1260):     at com.google.android.gms.maps.CameraUpdateFactory.aV(Unknown Source)
09-06 15:29:30.480: E/AndroidRuntime(1260):     at com.google.android.gms.maps.CameraUpdateFactory.newLatLngZoom(Unknown Source)
09-06 15:29:30.480: E/AndroidRuntime(1260):     at com.example.googlemapsv2.MainActivity.onCreate(MainActivity.java:24)
09-06 15:29:30.480: E/AndroidRuntime(1260):     at android.app.Activity.performCreate(Activity.java:5104)
09-06 15:29:30.480: E/AndroidRuntime(1260):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
09-06 15:29:30.480: E/AndroidRuntime(1260):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
09-06 15:29:30.480: E/AndroidRuntime(1260):     ... 11 more

3 个答案:

答案 0 :(得分:0)

您是否在清单中声明了活动?

编辑:将库导入工作区后(在Eclipse中),右键单击项目,打开属性,单击android,在底部您将看到库导入部分。添加它。

答案 1 :(得分:0)

您使用的是哪个版本的API?如何使用地图布局查看xml? 我最近遇到了同样的问题。这是因为我使用了错误的片段类型。

    <fragment
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

当我放android:name="com.google.android.gms.maps.SupportMapFragment"

时,它有效

然后在您的代码中,如果您使用地图,则必须为您的班级FragmentActivity延长Activity public class MapActivity extends FragmentActivity的插入内容

    GoogleMap mMap;
    SupportMapFragment mMapFragment;
    mMapFragment = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map));
    mMap = mMapFragment.getMap();

这就是你在代码中获得mapFragment的方法。

答案 2 :(得分:0)

假设布局通胀错误

主要是由基于导入的错误片段类型的膨胀引起的。

案例1:如果您在FragmentActivity或Activity中使用MapFragment,请使用import android.support.v4.app.*(即您的类扩展了此包中的Activity或Fragment Activity ),然后你会得到ClassCastException

解决方案使用SupportMapFragment

<fragment
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

然后使用代码在您的Activity中获取地图。

SupportMapFragment mapFragment = ((SupportMapFragment) this
                        .getSupportFragmentManager().findFragmentById(R.id.map));

案例2:如果您在FragmentActivity或Activity中使用SupportMapfragment,请使用import android.app.*(即您的类扩展了此包中的Activity或Fragment Activity ),那么你也会得到ClassCastException

解决方案:使用MapFragment

<fragment
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

然后在代码中使用以获取您的Acitivity中的地图参考

MapFragment mapFragment = ((MapFragment) this
                            .getSupportFragmentManager().findFragmentById(R.id.map));

此外,播放服务无需专门在Android Dependencies文件夹中,因为Android Private文件夹也添加到您的构建路径中。