0

请帮忙。我已经尝试并实施了所有其他解决方案,但在我的应用程序中集成谷歌地图仍然没有运气。

当前的问题是在我的片段中的膨胀点

05-21 00:17:24.629  26619-26619/com.wimedias12.orderr          E/AndroidRuntime: FATAL EXCEPTION: main
    java.lang.NoClassDefFoundError: com.google.android.gms.R$styleable
    at com.google.android.gms.maps.GoogleMapOptions.createFromAttributes(Unknown Source)
    at com.google.android.gms.maps.SupportMapFragment.onInflate(Unknown Source)
    at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:279)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:749)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
    at com.wimedias12.orderr.RestaurantFragments.RestaurantHomeFragment.onCreateView(RestaurantHomeFragment.java:66)

作为答案弹出的第一件事是以正确的方式引用谷歌服务库,但这就是设置。

所以这是我目前的配置,如果可以的话,请帮助我。

restaurant_home.xml - 我希望在其中显示地图的片段布局

... Other fragment layout XML code ...

<LinearLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="@+id/mapMap"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp">
<fragment
        android:id="@+id/mapMapppp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.SupportMapFragment" />

</LinearLayout>

... Rest of the fragment layout XML ... 

我尝试过使用 MapView 但没有运气,我读到使用片段更容易。

RestaurantHomeFragment.java - 片段的代码

public class RestaurantHomeFragment extends SherlockFragment {
 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.restaurant_home, container, false); //<-- This line breakes
 ....
}

此片段托管在 SherlockFragmentActivity 中。

AndroidManifest.xml

<uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="14" />

    <permission android:name="com.wimedias12.orderr.MAPS_RECEIVE" android:protectionLevel="signature" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
    <uses-permission android:name="com.wimedias12.orderr.MAPS_RECEIVE" />

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

   <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.VPI" >
       <uses-library android:name="com.google.android.maps" />
....
<meta-data
           android:name="com.google.android.maps.v2.API_KEY"
           android:value="AIzaXxXxXXXxXxxXx-WHmjbRpFz4eOqWBXdqUtQ" />
   </application>

这是我的项目属性,只是为了验证该库是否被很好地引用。 在此处输入图像描述

任何帮助深表感谢。谢谢!

更新:

@CommonsWare 答案是正确的。但是因为我使用的是 Android Studio 即 IntelliJ Idea,所以我觉得我需要回答我是如何让它工作的。

我刚刚从“项目结构”窗口中删除了所有引用,然后又一个一个地重新添加,不知何故它起作用了,配置仍然与上图相同。我猜 IntelliJ 在引用该库时遇到了一些问题。

所以正确引用图书馆就是答案。

PS 我最终在 SherlockFragment 中使用了 MapView。感谢您的帮助。

4

2 回答 2

3

You appear to be using Android Studio or IntelliJ IDEA, and I cannot comment on that.

However, your error is normally associated with a project where you added the JAR for Google Play Services, not the entire Android library project. Here are instructions for attaching the Android library project in Eclipse or from the command line -- you will have to find the equivalent instructions for your IDE of choice.

Also note that you are presently attempting to use fragments in fragments, by having RestaurantHomeFragment inflate a layout containing SupportMapFragment. That is possible to do, but unless you have a strong and clear reason to do so, I wouldn't, as nested fragments add complexity.

于 2013-05-20T22:52:33.433 回答
0

你为什么不尝试这样的事情:

public class MainActivity extends FragmentActivity {

private GoogleMap map;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main); <--This line..I dont see why would you inflate it, like you did

    map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
    map.setMyLocationEnabled(true);
}
于 2013-05-20T22:59:26.470 回答