不幸的是,添加ADMOB后APP已停止

时间:2014-11-30 23:15:49

标签: java android eclipse android-intent emulation

我正在尝试在我的Android应用程序中添加adMob广告,但它让我不幸停止了错误,

我不是android程序的专家所以不知道什么是错误的。

HomeActivity

package com.innobee.finddifferences;
import com.google.android.gms.ads.*;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.innobee.finddifferences.utility.Prefs;

public class HomeActivity extends Activity{
    private int mLevelDuration;         //Max Duration of the level in seconds
    private Button mBtnResume;
    private Button mBtnStart;   
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);             
        setContentView(R.layout.activity_home);     

       AdView adView = (AdView)this.findViewById(R.id.adView);
       AdRequest adRequest = new AdRequest.Builder().build();
       adView.loadAd(adRequest);

        mLevelDuration = getResources().getInteger(R.integer.levelDuration);        
        mLevelDuration = mLevelDuration * 1000;

        mBtnStart = (Button)findViewById(R.id.btnStart);
        mBtnStart.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Prefs.clearPref(getApplicationContext());
                Prefs.setStagePref(getApplicationContext(), 1);
                Intent play = new Intent(HomeActivity.this,
                        PlayActivity.class);                        
                startActivity(play);
            }
        });

        mBtnResume = (Button)findViewById(R.id.btnResume);
        mBtnResume.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent play = new Intent(HomeActivity.this,
                        PlayActivity.class);                        
                startActivity(play);
            }
        });         

        if(Prefs.getResumePref(getApplicationContext()) == mLevelDuration) {        
            mBtnResume.setVisibility(View.INVISIBLE);           
        }
        else {
            mBtnResume.setVisibility(View.VISIBLE);
        }
    }   

    @Override
    protected void onRestart() {
        super.onRestart();
        //Show/hide resume button 
        if(Prefs.getResumePref(getApplicationContext()) == mLevelDuration) {        
            mBtnResume.setVisibility(View.INVISIBLE);
        }
        else {
            mBtnResume.setVisibility(View.VISIBLE);
        }

    }

}

activity_home

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_gravity="bottom"
    android:background="@drawable/background"
    android:gravity="bottom"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btnStart"
        android:layout_width="209dip"
        android:layout_height="55dip"
        android:layout_marginBottom="30dip"
        android:layout_gravity="center_horizontal"
        android:background="@drawable/button_start" />

    <Button
        android:id="@+id/btnResume"
        android:layout_width="209dip"
        android:layout_height="55dip"
        android:layout_gravity="center_horizontal"
        android:background="@drawable/button_resume"
        android:layout_marginBottom="50dip"
        android:visibility="gone" />

     <com.google.android.gms.ads.AdView android:id="@+id/adView"
                         android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
                         ads:adUnitId="ca-app-pub-1504309466120108/6661079420"
                         ads:adSize="BANNER"/>

</LinearLayout>

的AndroidManifest.xml

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


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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppBaseTheme.NoTitle" >

          <meta-data android:name="com.google.android.gms.version"
               android:value="@integer/google_play_services_version"/>

        <activity            
            android:name="com.innobee.finddifferences.HomeActivity"
            android:screenOrientation="portrait"
            android:theme="@style/AppBaseTheme.NoTitle"
            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.innobee.finddifferences.NextGameActivity"
            android:screenOrientation="portrait" 
            android:theme="@style/Theme.Transparent"
            android:configChanges="orientation|keyboardHidden" >
        </activity>
        <activity
            android:name="com.innobee.finddifferences.SummaryActivity"
            android:screenOrientation="portrait" 
           android:theme="@style/AppBaseTheme.NoTitle"
            android:configChanges="orientation|keyboardHidden" >
        </activity>
           <activity
            android:name="com.innobee.finddifferences.LostActivity"
            android:screenOrientation="portrait" 
            android:theme="@style/AppBaseTheme.NoTitle"
            android:configChanges="orientation|keyboardHidden" >
        </activity>
        <activity
            android:name="com.innobee.finddifferences.StagePickActivity"
            android:screenOrientation="portrait" 
            android:theme="@style/AppBaseTheme.NoTitle"
            android:configChanges="orientation|keyboardHidden" >
        </activity>
        <activity
            android:name="com.innobee.finddifferences.PlayActivity"
            android:screenOrientation="portrait" 
            android:theme="@style/AppBaseTheme.NoTitle"
            android:configChanges="orientation|keyboardHidden" >
        </activity>
        <activity
            android:name="com.innobee.finddifferences.ListMobileActivity"
            android:screenOrientation="portrait" 
            android:theme="@style/AppBaseTheme.NoTitle"
            android:configChanges="orientation|keyboardHidden" >
        </activity>
         <activity android:name="com.google.ads.AdActivity"
          android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>       
    </application>

</manifest>

logcat的

11-30 22:52:47.121: E/AndroidRuntime(1282):     at com.innobee.finddifferences.HomeActivity.onCreate(HomeActivity.java:21)
11-30 22:52:47.121: E/AndroidRuntime(1282):     at android.app.Activity.performCreate(Activity.java:5104)
11-30 22:52:47.121: E/AndroidRuntime(1282):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
11-30 22:52:47.121: E/AndroidRuntime(1282):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
11-30 22:52:47.121: E/AndroidRuntime(1282):     ... 11 more
11-30 22:52:47.121: E/AndroidRuntime(1282): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.ads.AdView" on path: /data/app/com.innobee.finddifferences-1.apk
11-30 22:52:47.121: E/AndroidRuntime(1282):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
11-30 22:52:47.121: E/AndroidRuntime(1282):     at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
11-30 22:52:47.121: E/AndroidRuntime(1282):     at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
11-30 22:52:47.121: E/AndroidRuntime(1282):     at android.view.LayoutInflater.createView(LayoutInflater.java:552)
11-30 22:52:47.121: E/AndroidRuntime(1282):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
11-30 22:52:47.121: E/AndroidRuntime(1282):     ... 21 more

谢谢

1 个答案:

答案 0 :(得分:0)

错误可能来自您在清单中列出的错误活动,使用此错误:

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

尽管如此,正如我在评论中所说,你应该使用minSDK =&#34; 9&#34;使用最新版本的Google服务,否则会导致问题