Admob应用程序由于清单错误而不断崩溃

时间:2019-01-12 15:55:33

标签: android admob

这是我的清单,我一直关注着2018年更新的横幅广告的MAN页面:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.admin.ad">
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">



            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <meta-data
        android:name="com.google.android.gms.ads.APPLICATION_ID"
        android:value="ca-app-pub-3940256099942544~3347511713"/>



    </application>

</manifest>

这是mainActivity文件,仅使用横幅广告的简单调用即可,我不知道发生了什么。我正在正确编写代码,在构建之前不会显示任何错误:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.doubleclick.PublisherAdRequest;
import com.google.android.gms.ads.doubleclick.PublisherAdView;

public class MainActivity extends AppCompatActivity {
    private AdView mAdView;
    private PublisherAdView mPublisherAdView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        // Sample AdMob app ID: ca-app-pub-3940256099942544~3347511713
////        MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713");
//        mPublisherAdView = findViewById(R.id.publisherAdView);
//        PublisherAdRequest adRequest = new PublisherAdRequest.Builder().build();
//        mPublisherAdView.loadAd(adRequest);
        AdView firstAd = findViewById(R.id.adView);
        MobileAds.initialize(this,"ca-app-pub-3940256099942544~3347511713");
    AdRequest adRequest = new AdRequest.Builder().build();
    firstAd.loadAd(adRequest);

    }
}

我收到此错误,应用程序强制关闭:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.admin.ad, PID: 26343
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.admin.ad/com.example.admin.ad.MainActivity}: android.view.InflateException: Binary XML file line #24: Binary XML file line #24: Error inflating class com.google.android.gms.ads.Adview
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2485)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2545)
        at android.app.ActivityThread.access$1100(ActivityThread.java:151)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1396)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:157)
        at android.app.ActivityThread.main(ActivityThread.java:5601)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)
     Caused by: android.view.InflateException: Binary XML file line #24: Binary XML file line #24: Error inflating class com.google.android.gms.ads.Adview
        at android.view.LayoutInflater.inflate(LayoutInflater.java:543)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:427)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
        at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
        at com.example.admin.ad.MainActivity.onCreate(MainActivity.java:18)
        at android.app.Activity.performCreate(Activity.java:6355)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2438)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2545) 
        at android.app.ActivityThread.access$1100(ActivityThread.java:151) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1396) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:157) 
        at android.app.ActivityThread.main(ActivityThread.java:5601) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652) 
     Caused by: android.view.InflateException: Binary XML file line #24: Error inflating class com.google.android.gms.ads.Adview
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:780)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:708)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:839)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:802)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:519)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:427) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
        at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469) 
        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
        at com.example.admin.ad.MainActivity.onCreate(MainActivity.java:18) 
        at android.app.Activity.performCreate(Activity.java:6355) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2438) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2545) 
        at android.app.ActivityThread.access$1100(ActivityThread.java:151) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1396) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:157) 
        at android.app.ActivityThread.main(ActivityThread.java:5601) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652) 
     Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.ads.Adview" on path: DexPathList[[zip file "/data/app/com.example.admin.ad-1/base.apk", zip file "/data/app/com.example.admin.ad-1/split_lib_dependencies_apk.apk", zip file "/data/app/com.example.admin.ad-1/split_lib_slice_0_apk.apk", zip file "/data/app/com.example.admin.ad-1/split_lib_slice_1_apk.apk", zip file "/data/app/com.example.admin.ad-1/split_lib_slice_2_apk.apk", zip file "/data/app/com.example.admin.ad-1/split_lib_slice_3_apk.apk", zip file "/data/app/com.example.admin.ad-1/split_lib_slice_4_apk.apk", zip file "/data/app/com.example.admin.ad-1/split_lib_slice_5_apk.apk", zip file "/data/app/com.example.admin.ad-1/split_lib_slice_6_apk.apk", zip file "/data/app/com.example.admin.ad-1/split_lib_slice_7_apk.apk", zip file "/data/app/com.example.admin.ad-1/split_lib_slice_8_apk.apk", zip file "/data/app/com.example.admin.ad-1/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.example.admin.ad-1/lib/arm64, /vendor/lib64, /system/lib64]]
        at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
        at android.view.LayoutInflater.createView(LayoutInflater.java:587)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:768)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:708) 
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:839) 
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:802) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:519) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:427) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
        at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469) 
        at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
        at com.example.admin.ad.MainActivity.onCreate(MainActivity.java:18) 
        at android.app.Activity.performCreate(Activity.java:6355) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2438) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2545) 
        at android.app.ActivityThread.access$1100(ActivityThread.java:151) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1396) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:157) 
        at android.app.ActivityThread.main(ActivityThread.java:5601) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652) 
        Suppressed: java.lang.ClassNotFoundException: com.google.android.gms.ads.Adview
        at java.lang.Class.classForName(Native Method)
        at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
        at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
        at java.lang.ClassLoader.loadClass(ClassLoader.java:504)

这是我的布局文件:-

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    <com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
    </com.google.android.gms.ads.AdView>



</RelativeLayout>

这是build.gradle文件:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.myapplication"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    implementation 'com.google.firebase:firebase-ads:17.1.2'
    implementation 'com.android.support:animated-vector-drawable:28.0.0'
    implementation 'com.android.support:exifinterface:28.0.0'
    implementation 'com.android.support:cardview-v7:28.0.0'
    implementation 'com.android.support:customtabs:28.0.0'
    implementation 'com.android.support:support-media-compat:28.0.0'
    implementation 'com.android.support:support-v4:28.0.0'
    implementation 'com.google.firebase:firebase-core:16.0.6'

    implementation 'com.google.android.gms:play-services-ads:17.1.2'
}

我只是admob的初学者,请帮助!

3 个答案:

答案 0 :(得分:1)

这些答案似乎没有说服力,因为它们没有解决错误消息。

a)它抱怨XML无效,因为在错误的节点上进行了名称空间声明。

  

二进制XML文件第24行:膨胀类com.google.android.gms.ads.Adview的错误

因此,请替换为:

<com.google.android.gms.ads.AdView
    xmlns:ads="http://schemas.android.com/apk/res-auto"

具有:

<com.google.android.gms.ads.AdView

,而是将名称空间声明添加到最顶部的节点:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"

只有这样,名称空间ads才可以使用。

b)不需要在XMLJava代码中设置值;一次设置就足够了

Java代码中设置值时,基本上可以完全删除以下行:

xmlns:ads="http://schemas.android.com/apk/res-auto"

ads:adSize="BANNER"
ads:adUnitId="ca-app-pub-3940256099942544/6300978111"

答案 1 :(得分:-1)

删除gradle文件,使缓存无效并重新启动项目,还放置

  

MobileAds.initialize(this,“ ca-app-pub-3940256099942544〜3347511713”);

在顶部。也会和我一起工作。

答案 2 :(得分:-1)

我认为这是由于DEX限制问题引起的,因为您已经采用了多个大型库,例如支持库。

有多种解决方案:

i)简单快速:在您的应用程序中启用MultiDex。尽管这是一种简便快捷的方法,但是我不建议您这样做,因为我们应该始终深入研究错误和问题以找到根本原因并积累知识!

ii)有点挑战性(虽然不是那么具有挑战性!)::分析您的apk并注意到它推动结果apk的方法数量。这将帮助您确定罪魁祸首并最大程度地减少使用次数!

根据您的分析,然后可以为每个库启用proguard,以最小化结果apk中的代码!