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

时间:2014-07-11 12:24:17

标签: java android xml

好的,所以我在这里尝试了一些指南,但没有人帮助我,有很多关于admob的更新,所以我不知道我的admob应该是什么样子,但我希望你能帮助我:)) p>

这是我的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广告添加到名为splashscreen的主菜单中[它是一个启动画面,我将其更改为主菜单]):

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 OUTPUT:

    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

任何人都可以找到我的问题:(?

和APP崩溃。

2 个答案:

答案 0 :(得分:0)

您的Logcat说问题是您班级的第25行:this.requestWindowFeature(Window.FEATURE_NO_TITLE);。它还说在添加内容之前需要调用此方法。这意味着此行必须移至setContentView();以上并且您的崩溃应该是固定的

此次崩溃不是由admob造成的,而是由Activity.requestWindowFeature();展示位置

引起的

答案 1 :(得分:0)

只需更改编译:

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

编译

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

build.gradle文件