gpt4 book ai didi

android - 如何解决 com.google.android.gms.R$styleable 错误的 java.lang.NoClassDefFoundError?

转载 作者:行者123 更新时间:2023-11-30 02:48:14 26 4
gpt4 key购买 nike

我在项目中使用 Google map ,并使用 Android.mk 构建它。但是(经过几天的研究)仍然无法弄清楚如何解决 NoClassDefFoundError ,它会及时崩溃 apk 谷歌地图 fragment 膨胀发生。我内心有什么可以解决它的方法吗?分解如下。

Google Play 服务库是通过以下方式添加的:

#Adding classpath to Google Play Services classes
LOCAL_CLASSPATH += $(LOCAL_PATH)/google-play-services_lib/bin/
...
#Google Play Services
LOCAL_STATIC_JAVA_LIBRARIES += google-play-services \
google-play-services_lib
...
LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES += google-play-services:libs/google-play-services.jar \
google-play-services_lib:libs/google-play-services_lib.jar

构建正常,但应用程序在尝试使用 Google map fragment 扩充布局时抛出运行时错误。 LogCat:

**java.lang.NoClassDefFoundError**:
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.MapFragment.onInflate(Unknown Source)
at android.app.Activity.onCreateView(Activity.java:4996)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:695)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
at com.mapsvisitor.ui.MapsFragment.onCreateView(MapsFragment.java:81)
...

代码:

final View layout = inflater.inflate(R.layout.maps_layout, null);

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/map_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />
</LinearLayout>

最佳答案

问题在于,当您将依赖项项目用作库时,生成的 JAR (google-play-services_lib.jar) 不适合独立使用(作为 java 静态库)。作为简单的解决方案,使用 Eclipse 导出生成的 JAR,而不是通过以下方式 Export > Java > JAR file 并选择要导出的 srcgen 文件夹: enter image description here

以下代码段有助于定义 com.google.android.gms.R 和嵌套类不可用(com.google.android.gms.R$attr、com.google.android. gms.R$color 等):

void isClassesAvailable(List<String> classes) {
for(String cl: classes) {
try {
Class.forName(cl);
}
catch (ClassNotFoundException e) {
Log.w(TAG, e.getMessage());
}
}
}

如何为依赖项项目打包 JAR(使用 src 文件夹类。请参阅下面的文件夹“android”): enter image description here

如何为强大的静态库打包 JAR(使用 src 和 gen 文件夹类。请参阅下面的文件夹“android”和“com”): enter image description here

然而,在使用 Eclipse export light hack 的惯用正确解决方案成为 GTD 之前。简单地用 google-play-services_lib/bin/classes 内容重新打包 google-play-services_lib.jar 并且之前在那里显示的内容使所有资源的类都可用。

关于android - 如何解决 com.google.android.gms.R$styleable 错误的 java.lang.NoClassDefFoundError?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24649217/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com