安卓工作室AdMob -- 无法实例化以下类

2022-01-25 00:00:00 android android-studio java admob

我一直在尝试在我的应用中使用 AdMob 横幅广告,但一直收到错误消息;

I have been trying to use an AdMob banner ad in my app but keep getting the error;

无法实例化以下类:- com.google.android.gms.ads.AdView

The following classes could not be instantiated: - com.google.android.gms.ads.AdView

java.lang.VerifyError: Expecting a stackmap frame at branch target 57
Exception Details:
Location:
com/google/android/gms/ads/AdView.onMeasure(II)V @20: ifnull
Reason:
Expected stackmap frame at this location.
Bytecode:
0000000: 033e 0336 042a 03b6 0015 3a05 2ab6 0014
0000010: 3a06 1905 c600 2519 05b6 000b 1008 9f00
0000020: 1b2a 1905 1b1c b600 1919 05b6 000a 3e19
0000030: 05b6 0009 3604 a700 1f19 06c6 001a 2ab6
0000040: 0016 3a07 1906 1907 b600 133e 1906 1907
0000050: b600 1236 041d 2ab6 0018 b800 2a3e 1504
0000060: 2ab6 0017 b800 2a36 042a 1d1b b800 0d15
0000070: 041c b800 0db6 001a b1                 

at java.lang.Class.getDeclaredConstructors0(Native Method)
at java.lang.Class.privateGetDeclaredConstructors(Class.java:2658)
at java.lang.Class.getConstructor0(Class.java:2964)
at java.lang.Class.getConstructor(Class.java:1812)
at org.jetbrains.android.uipreview.ViewLoader.createNewInstance(ViewLoader.java:310)
at org.jetbrains.android.uipreview.ViewLoader.loadView(ViewLoader.java:100)
at com.android.tools.idea.rendering.LayoutlibCallback.loadView(LayoutlibCallback.java:172)
at android.view.BridgeInflater.loadCustomView(BridgeInflater.java:207)
at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:132)
at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:802)
at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:64)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:778)
at android.view.LayoutInflater.inflate(LayoutInflater.java:500)
at android.view.LayoutInflater.inflate(LayoutInflater.java:381)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:395)
at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:329)
at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:332)
at com.android.tools.idea.rendering.RenderService$3.compute(RenderService.java:575)
at com.android.tools.idea.rendering.RenderService$3.compute(RenderService.java:564)
at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.java:932)
at com.android.tools.idea.rendering.RenderService.createRenderSession(RenderService.java:564)
at com.android.tools.idea.rendering.RenderService.render(RenderService.java:691)
at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager.doRender(AndroidLayoutPreviewToolWindowManager.java:586)
at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager.access$1900(AndroidLayoutPreviewToolWindowManager.java:80)
at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager$6$1.run(AndroidLayoutPreviewToolWindowManager.java:528)
at com.intellij.openapi.progress.impl.ProgressManagerImpl$2.run(ProgressManagerImpl.java:178)
at com.intellij.openapi.progress.ProgressManager.executeProcessUnderProgress(ProgressManager.java:209)
at com.intellij.openapi.progress.impl.ProgressManagerImpl.executeProcessUnderProgress(ProgressManagerImpl.java:212)
at com.intellij.openapi.progress.impl.ProgressManagerImpl.runProcess(ProgressManagerImpl.java:171)
at org.jetbrains.android.uipreview.AndroidLayoutPreviewToolWindowManager$6.run(AndroidLayoutPreviewToolWindowManager.java:523)
at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.java:320)
at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.java:310)
at com.intellij.util.ui.update.MergingUpdateQueue$2.run(MergingUpdateQueue.java:254)
at com.intellij.util.ui.update.MergingUpdateQueue.flush(MergingUpdateQueue.java:269)
at com.intellij.util.ui.update.MergingUpdateQueue.flush(MergingUpdateQueue.java:227)
at com.intellij.util.ui.update.MergingUpdateQueue.run(MergingUpdateQueue.java:217)
at com.intellij.util.concurrency.QueueProcessor.runSafely(QueueProcessor.java:238)
at com.intellij.util.Alarm$Request$1.run(Alarm.java:327)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

我已经尝试了在这里和谷歌搜索可以找到的所有解决方案.我有;

I have tried every solution I can find on here and by Googling. I have;

compile 'com.google.android.gms:play-services:5.0.77'

在我的毕业典礼上.我已经输入了这里指定的所有部分;

in my gradle. I have entered all the parts specified here;

https://developers.google.com/mobile-ads-sdk/docs/

但还是一无所获.希望有人能帮忙..!

But still nothing. Hope someone can help..!

干杯

activity_main.xml

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
xmlns:ads="http://schemas.android.com/apk/res-auto"
tools:context=".Main">



<TextView
    android:text="Test"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="91dp"
    android:height="150dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:id="@+id/txtTest" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/button"
    android:layout_above="@+id/txtTest"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:onClick="buttonOnClick" />


<com.google.android.gms.ads.AdView android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    ads:adUnitId="MY_AD_UNIT_ID"
    ads:adSize="BANNER"/>
</RelativeLayout>

AndroidManifest.xml

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
package="com.example.betty8080.brentmeister" >
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <meta-data android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
    <activity
        android:name=".Main"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name="com.google.android.gms.ads.AdActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>

</application>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

</manifest>

build.gradle(依赖)

build.gradle (dependencies)

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.google.android.gms:play-services:5.0.77'
}

推荐答案

我在尝试预览自定义视图 AdView 时遇到了同样的问题.我的项目是通过 Intellij 社区版 (13.x) 使用 gradle 构建的,我最终将 google play 服务的版本从

I had the same problem when trying to preview the custom view AdView. My project is built using gradle via Intellij community edition (13.x) and I ended up changing the version of google play services from

compile 'com.google.android.gms:play-services:5.+'

compile 'com.google.android.gms:play-services:4.+'

具体来说,我将 gradle 文件的依赖项部分更改为如下所示:

specifically, I changed the dependencies section of my gradle file to look like this:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.google.android.gms:play-services:4.+'
}

相关文章