Java ClassNotFoundException:未找到类“com.google.android.gms.ads.AdView”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24930782/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ClassNotFoundException: Didn't find class "com.google.android.gms.ads.AdView"
提问by flxapps
I did a lot of research and this seems to be a common error for many users but for very different reasons. None of which I found worked for me.
我做了很多研究,这对于许多用户来说似乎是一个常见的错误,但原因却截然不同。我发现其中没有一个对我有用。
I'm getting
我越来越
java.lang.RuntimeException: Unable to start activity ComponentInfo{
[...]/[...].activities.StartActivity}: android.view.InflateException:
Binary XML file line #173: Error inflating class [...].BannerAd
[...]
Caused by: android.view.InflateException: Binary XML file line #8: Error
inflating class com.google.android.gms.ads.AdView
[...]
Caused by: java.lang.ClassNotFoundException: Didn't find class
"com.google.android.gms.ads.AdView" on path: DexPathList[[zip file
"/data/app/[...]-1.apk"],nativeLibraryDirectories=[/data/app-lib/[...]-1,
/vendor/lib, /system/lib]]
I'm having the newest versions of ADT and SDK packages installed. I copied google-play-services_lib to my workspace and imported it as a Android project. I added it as a library to my app project. I checked everything under "Order and Export".
我安装了最新版本的 ADT 和 SDK 包。我将 google-play-services_lib 复制到我的工作区并将其作为 Android 项目导入。我将它作为库添加到我的应用程序项目中。我检查了“订购和出口”下的所有内容。
I'm having a banner_ad.xml:
我有一个 banner_ad.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|top"
android:orientation="vertical" >
<com.google.android.gms.ads.AdView
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="[...]" />
</LinearLayout>
And a BannerAd.java which I am using:
还有我正在使用的 BannerAd.java:
package [...];
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import [...].R;
import [...].general.Settings;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
public class BannerAd extends LinearLayout {
public BannerAd(Context context, AttributeSet attrs) {
super(context, attrs);
if (!Settings.PRO) {
LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mInflater.inflate(R.layout.banner_ad, this, true);
AdView adView = (AdView) this.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
}
}
}
Could it have something to do with proguard? I have no idea, this is my proguard-project.txt file, however:
它可能与proguard有关吗?我不知道,这是我的 proguard-project.txt 文件,但是:
# To enable ProGuard in your project, edit project.properties
# to define the proguard.config property as described in that file.
#
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in ${sdk.dir}/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the ProGuard
# include property in project.properties.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
# Add any project specific keep options here:
# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
-keep class * extends java.util.ListResourceBundle {
protected Object[][] getContents();
}
-keep public class com.google.android.gms.common.internal.safeparcel.SafeParcelable {
public static final *** NULL;
}
-keepnames @com.google.android.gms.common.annotation.KeepName class *
-keepclassmembernames class * {
@com.google.android.gms.common.annotation.KeepName *;
}
-keepnames class * implements android.os.Parcelable {
public static final ** CREATOR;
}
Any ideas what I could try to fix this?
任何想法我可以尝试解决这个问题?
Edit: Sometimes, I also get such output in the console (but not every time I compile, only sometimes):
编辑:有时,我也会在控制台中得到这样的输出(但不是每次编译时,只是有时):
[2014-07-24 12:49:05 - [...]] Dx
trouble processing:
[2014-07-24 12:49:05 - [...]] Dx bad class file magic (cafebabe) or version (0033.0000)
...while parsing com/google/android/gms/internal/mb.class
...while processing com/google/android/gms/internal/mb.class
[2014-07-24 12:49:05 - [...]] Dx
trouble processing:
[2014-07-24 12:49:05 - [...]] Dx bad class file magic (cafebabe) or version (0033.0000)
...while parsing com/google/android/gms/internal/mc.class
...while processing com/google/android/gms/internal/mc.class
[2014-07-24 12:49:05 - [...]] Dx
[...]
[Lots of similar warnings here]
[...]
trouble processing:
[2014-07-24 12:49:25 - [...]] Dx bad class file magic (cafebabe) or version (0033.0000)
...while parsing com/google/ads/mediation/customevent/CustomEventAdapter$a.class
...while processing com/google/ads/mediation/customevent/CustomEventAdapter$a.class
[2014-07-24 12:49:25 - [...]] Dx
trouble processing:
[2014-07-24 12:49:25 - [...]] Dx bad class file magic (cafebabe) or version (0033.0000)
...while parsing com/google/ads/mediation/customevent/CustomEventServerParameters.class
...while processing com/google/ads/mediation/customevent/CustomEventServerParameters.class
[2014-07-24 12:49:25 - [...]] Dx 2786 warnings
回答by flxapps
I finally solved my problems by
我终于解决了我的问题
- installing the newest version of Android SDK Build Tools (didn't realize that I did not have them yet)
- switching from Compiler compliance level 1.6 to 1.7 (Window - Preferences - Java - Compiler - Compiler compliance level)
- 安装最新版本的 Android SDK 构建工具(没有意识到我还没有它们)
- 从编译器合规级别 1.6 切换到 1.7(窗口 - 首选项 - Java - 编译器 - 编译器合规级别)
Thanks at everybody.
谢谢大家。
回答by Ignacio Rubio
I did what @user1684030 said (upgraded Android SDK Build Tools; changed compiler to 1.7), but not worked for me. I've written the solution that I found in case it could help anyone with the same problem:
我做了@user1684030 所说的(升级 Android SDK 构建工具;将编译器更改为 1.7),但对我不起作用。我写了我找到的解决方案,以防它可以帮助任何有同样问题的人:
In the step 3 of Setting Up Google Play Servicessays:
在设置 Google Play 服务的第 3 步中说:
In your app project, reference Google Play services library project. See Referencing a Library Project for Eclipse for more information on how to do this.
Note: You should be referencing a copy of the library that you copied to your development workspace—you should not reference the library directly from the Android SDK directory.
在您的应用项目中,引用 Google Play 服务库项目。有关如何执行此操作的更多信息,请参阅为 Eclipse 引用库项目。
注意:您应该引用复制到开发工作区的库的副本——您不应该直接从 Android SDK 目录引用库。
Solution: Don't do the copy manually. Instead just check the box in Eclipse that says "Copy projects into workspace"
解决方法:不要手动复制。相反,只需选中 Eclipse 中显示“将项目复制到工作区”的框
I did it so and everything worked again.
我这样做了,一切都恢复了。
Thanks to this StackOverFlow question: How to fix Google Play Services 2 Library install to Eclipse(Admob is part of Google Play Services)
感谢这个 StackOverFlow 问题:How to fix Google Play Services 2 Library install to Eclipse(Admob is part of Google Play Services)
回答by Andrey Nikishaev
This because ProGuard. To safely use ProGuard with Google Mobile Ads, add the following to your ProGuard config:
这是因为 ProGuard。要安全地将 ProGuard 与 Google 移动广告一起使用,请将以下内容添加到您的 ProGuard 配置中:
-keep public class com.google.android.gms.ads.** {
public *;
}
-keep public class com.google.ads.** {
public *;
}