我已经从 Eclipse 切换到使用 Android Studio 并且一切正常,除非尝试将 AdMob 合并到我的项目中。我已经从我之前的项目中正确设置了代码,因此只需告诉 Android Studio 使用它进行构建。
我一直在阅读很多关于将 .jar 添加到 libs 文件夹的问题,但我认为最新版本的 Android Studio 改变了布局。所以我只是将它拖到 .idea > 库中,然后右键单击并“添加为库”。
在项目结构中,我有两个模块,和 -。在 - 下,它显示了列出的所有依赖项,包括 support-v4-18.0.0 和 GoogleAdMobAdsSdk-6.4.1,标记为 Export and Compile for Scope。
在 Android Studio 的预览窗口中,它可以正确显示 AdView,但在运行时会崩溃。我已经尝试使用'gradlew clean'并多次重建项目但没有成功。
目前,除非我在任何布局上都有 AdView,否则该项目可以正常工作。如果我在页面上确实有它,我会得到“java.lang.ClassNotFoundException:com.google.ads.AdView”指向我使用充气机来充气我的布局的行。
调用栈:
android.view.InflateException: Binary XML file line #155: Error inflating class com.google.ads.AdView
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:698)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
at com.awestruck.pokedex.Display_Main.onCreateView(Display_Main.java:53)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1478)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1460)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:472)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1068)
at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1436)
at android.view.View.measure(View.java:15279)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4832)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
at android.view.View.measure(View.java:15279)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4832)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15279)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
at android.view.View.measure(View.java:15279)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4832)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2271)
at android.view.View.measure(View.java:15279)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2091)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1253)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1483)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1151)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4661)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
at android.view.Choreographer.doCallbacks(Choreographer.java:555)
at android.view.Choreographer.doFrame(Choreographer.java:525)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:155)
at android.app.ActivityThread.main(ActivityThread.java:5493)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:795)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: com.google.ads.AdView
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
at android.view.LayoutInflater.createView(LayoutInflater.java:552)
这指向该行:
v = inflater.inflate(R.layout.display_main, container, false);
构建.gradle:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.+'
}
}
apply plugin: 'android'
repositories {
mavenCentral()
}
android {
compileSdkVersion 18
buildToolsVersion "18.1.0"
defaultConfig {
minSdkVersion 11
targetSdkVersion 18
}
}
dependencies {
compile 'com.android.support:appcompat-v7:18.0.0'
compile files('libs/GoogleAdMobAdsSdk-6.4.1.jar')
}
谢谢你的帮助。