繁体   English   中英

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

[英]ADMOB XML The following classes could not be instantiated: - com.google.android.gms.ads.AdView

好的,所以我在这里尝试了一些指南,但没有一个对我有所帮助,该admob的更新太多,所以我不知道我的admob应该是什么样子,但是希望您能对我有所帮助:)

这是我的xml文件:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/patternsplash"
    android:orientation="vertical" xmlns:app="http://schemas.android.com/apk/lib/com.google.ads">

    <Button
        android:id="@+id/play"
        android:layout_width="120dp"
        android:layout_height="70dp"
        android:layout_marginLeft="100dp"
        android:layout_marginTop="150dp"
        android:background="@drawable/startb" />

    <Button
        android:id="@+id/help"
        android:layout_width="120dp"
        android:layout_height="70dp"
        android:layout_marginLeft="100dp"
        android:layout_marginTop="30dp"
        android:background="@drawable/helpb" />

    <com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
        android:id="@+id/adView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:adSize="BANNER"
        app:adUnitId="ca-app-pub-XXXXXXXXXXXXXXXX/YYYYYYYYY" />

</LinearLayout>

这是我的课程(我正在尝试将admob广告添加到名为“初始屏幕”的主菜单中[这是一个初始屏幕,然后将其更改为主菜单]):

package com.FirstAppDevelopment.BlueTAP;

import com.example.whitetap.R;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;

public class SplashScreen extends Activity {
    private AdView adView;
    private static final String AD_UNIT_ID = "ca-app-pub-2481700940550944/3300455315";

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        adView = new AdView(this);
        adView.setAdSize(AdSize.BANNER);
        adView.setAdUnitId(AD_UNIT_ID);
        LinearLayout layout = (LinearLayout) findViewById(R.layout.activity_splash);
        layout.addView(adView);
        AdRequest adRequest = new AdRequest.Builder()
            .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
            .addTestDevice("36E5E931B9B5381CC9997F30A397FECF")
            .addTestDevice("1D193D5A8541BDFF3DF312372FFD6048")
            .build();
        adView.loadAd(adRequest);
        final Button button = (Button) findViewById(R.id.play);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                finish();
                Intent myIntent = new Intent(SplashScreen.this,
                        FirstActivity.class);
                startActivity(myIntent);
            }
        });
        final Button button1 = (Button) findViewById(R.id.help);
        button1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                finish();
                Intent intent = new Intent(SplashScreen.this, help.class);
                startActivity(intent);
            }
        });
    }
     @Override
      public void onResume() {
        super.onResume();
        if (adView != null) {
          adView.resume();
        }
      }

      @Override
      public void onPause() {
        if (adView != null) {
          adView.pause();
        }
        super.onPause();
      }

      @Override
      public void onDestroy() {
        if (adView != null) {
          adView.destroy();
        }
        super.onDestroy();
      }
    }

我的保镖:

    # 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;
#}
#}

我的AndroidManifest:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.FirstAppDevelopment.BlueTap"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="9"
        android:targetSdkVersion="19" />

    <uses-permission android:name="com.android.vending.BILLING" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" >
    </uses-permission>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/CustomActionBarTheme" >
        <meta-data
            android:name="com.google.android.gms.version"
            android:value="@integer/google_play_services_version" />

        <activity
            android:name="com.FirstAppDevelopment.BlueTAP.SplashScreen"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.Black.NoTitleBar" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.FirstAppDevelopment.BlueTAP.FirstActivity"
            android:label="@string/app_name" >
        </activity>
        <activity android:name="com.FirstAppDevelopment.BlueTAP.Level2" />
        <activity android:name="com.FirstAppDevelopment.BlueTAP.Shop" />
        <activity android:name="com.FirstAppDevelopment.BlueTAP.help" />
        <activity
            android:name="com.google.ads.AdActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" />
    </application>

</manifest>

LOGCAT输出:

    07-11 16:51:30.300: E/AndroidRuntime(15291): FATAL EXCEPTION: main
07-11 16:51:30.300: E/AndroidRuntime(15291): Process: com.example.whitetap, PID: 15291
07-11 16:51:30.300: E/AndroidRuntime(15291): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.whitetap/com.FirstAppDevelopment.BlueTAP.SplashScreen}: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
07-11 16:51:30.300: E/AndroidRuntime(15291):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2205)
07-11 16:51:30.300: E/AndroidRuntime(15291):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2255)
07-11 16:51:30.300: E/AndroidRuntime(15291):    at android.app.ActivityThread.access$800(ActivityThread.java:142)
07-11 16:51:30.300: E/AndroidRuntime(15291):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1203)
07-11 16:51:30.300: E/AndroidRuntime(15291):    at android.os.Handler.dispatchMessage(Handler.java:102)
07-11 16:51:30.300: E/AndroidRuntime(15291):    at android.os.Looper.loop(Looper.java:136)
07-11 16:51:30.300: E/AndroidRuntime(15291):    at android.app.ActivityThread.main(ActivityThread.java:5118)
07-11 16:51:30.300: E/AndroidRuntime(15291):    at java.lang.reflect.Method.invoke(Native Method)
07-11 16:51:30.300: E/AndroidRuntime(15291):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:794)
07-11 16:51:30.300: E/AndroidRuntime(15291):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:610)
07-11 16:51:30.300: E/AndroidRuntime(15291): Caused by: android.util.AndroidRuntimeException: requestFeature() must be called before adding content
07-11 16:51:30.300: E/AndroidRuntime(15291):    at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:278)
07-11 16:51:30.300: E/AndroidRuntime(15291):    at android.app.Activity.requestWindowFeature(Activity.java:3342)
07-11 16:51:30.300: E/AndroidRuntime(15291):    at com.FirstAppDevelopment.BlueTAP.SplashScreen.onCreate(SplashScreen.java:25)
07-11 16:51:30.300: E/AndroidRuntime(15291):    at android.app.Activity.performCreate(Activity.java:5275)
07-11 16:51:30.300: E/AndroidRuntime(15291):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-11 16:51:30.300: E/AndroidRuntime(15291):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
07-11 16:51:30.300: E/AndroidRuntime(15291):    ... 9 more

谁能找到我的问题:(?

和应用程序崩溃。

您的Logcat说问题出在您课程的第25行: this.requestWindowFeature(Window.FEATURE_NO_TITLE); 它还说在添加内容之前需要调用此方法。 这意味着该行必须移到setContentView();上方setContentView(); 并且您的崩溃应该得到修复

崩溃不是由admob引起的,而是由您的Activity.requestWindowFeature();引起的Activity.requestWindowFeature(); 放置

只需更改编译:

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

编译

com.google.android.gms:play-services:4.4.+

build.gradle文件中

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM