我正在尝试在我的活动中添加admob广告,但它显示我的onCreate方法中存在错误()

时间:2016-11-23 04:47:47

标签: android android-studio dependencies admob ads

我正在尝试在我的活动中添加admob广告但是它显示我的onCreate方法()在我点击按钮输入下面的活动后出现错误。我特此发布我的代码和Logcat日志,其中引发了错误。清单文件中提供了所有必要的权限。

的build.gradle:

dependencies {
    compile project(':playservicesads921')
    compile 'com.android.support:support-v4:23.4.0'
    compile 'com.android.support:appcompat-v7:23.1.1'
    compile 'com.android.support:design:23.1.1'
    compile 'com.google.android.gms:play-services-ads:8.4.0'

}

MainActivity:

    import com.google.android.gms.ads.AdListener;
    import com.google.android.gms.ads.AdRequest;
    import com.google.android.gms.ads.AdView;

    public class Temperature extends Activity implements OnClickListener{

    private AdView mAdView;
    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.temperature);
        Title=(TextView) findViewById(R.id.title);
        Result=(TextView) findViewById(R.id.result);
        Value=(EditText) findViewById(R.id.value);
        spinnerfrom=(Spinner) findViewById(R.id.spinnerfrom);
        spinnerto=(Spinner) findViewById(R.id.spinnerto);
        Calculate=(Button) findViewById(R.id.calculate);
        Bundle b=getIntent().getExtras();
        //Title.setText(b.getString("item"));
        imm=(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

        mAdView = (AdView) findViewById(R.id.adView);

        AdRequest adRequest = new AdRequest.Builder()
                 .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                .addTestDevice("F2B5F7BB75F462E9074A7905A1047616")
                .build();
        mAdView.loadAd(adRequest);
        mAdView.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
            }

            @Override
            public void onAdClosed() {
                Toast.makeText(getApplicationContext(), "Ad is closed!", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onAdFailedToLoad(int errorCode) {
                Toast.makeText(getApplicationContext(), "Ad failed to load! error code: " + errorCode, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onAdLeftApplication() {
                Toast.makeText(getApplicationContext(), "Ad left application!", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onAdOpened() {
                super.onAdOpened();
            }
Display d=getWindowManager().getDefaultDisplay();
        int h=d.getHeight();
        int w=d.getWidth();
        if(h < 1280 && w < 720)
        {
            Title.setTextSize(15);
            Calculate.setTextSize(15);
        }

        TemperatureItems=getResources().getStringArray(R.array.temperatureitems);
        ArrayAdapter<String> Temperatureadapter=new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_spinner_dropdown_item, TemperatureItems);
        spinnerfrom.setAdapter(Temperatureadapter);
        spinnerto.setAdapter(Temperatureadapter);

        Calculate.setOnClickListener(this);

//Futher main logic for the code inside onCreate which works perfectly when ads arent added.
        });


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

    @Override
    public void onResume() {
        super.onResume();
        if (mAdView != null) {
            mAdView.resume();
        }
    }

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

activity_main.xml中:

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

    <TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true" 
        android:textSize="20sp"
        android:textStyle="bold"
        />

    <TextView 
        android:id="@+id/from"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:text="From"
        android:textSize="16sp"
        android:textStyle="bold"
        android:layout_below="@id/title"
        android:layout_alignParentLeft="true"
        android:layout_marginTop="10sp"/>

    <com.google.android.gms.ads.AdView
        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="@string/banner_home_footer">
    </com.google.android.gms.ads.AdView>
</RelativeLayout>

清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="tech.pocket.converter"
    android:versionCode="3"
    android:versionName="1.2" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="23" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/convert"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <!--Include the AdActivity configChanges and theme. -->
        <activity
            android:name="com.google.android.gms.ads.AdActivity"
            android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
            android:theme="@android:style/Theme.Translucent" />
        <activity
            android:name="tech.pocket.converter.SplashScreen"
            android:label="@string/app_name" 
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name="tech.pocket.converter.Temperature"
            android:label="@string/title_activity_temperature"
            android:screenOrientation="portrait" >
        </activity>

    </application>

</manifest>

Logcat报告:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: tech.pocket.converter, PID: 13341
                  java.lang.NoSuchMethodError: No static method retainReference()V in class Lcom/google/android/gms/ads/internal/ClientApi; or its super classes (declaration of 'com.google.android.gms.ads.internal.ClientApi' appears in /data/data/tech.pocket.converter/files/instant-run/dex/slice-playservicesads921-unspecified-jars_a79d5d19414b37778fbbf51e978b6dbb12d40ba5-classes.dex)
                      at com.google.android.gms.ads.internal.client.zzl.<init>(Unknown Source)
                      at com.google.android.gms.ads.internal.client.zzn.<init>(Unknown Source)
                      at com.google.android.gms.ads.internal.client.zzn.<clinit>(Unknown Source)
                      at com.google.android.gms.ads.internal.client.zzn.zzcS(Unknown Source)
                      at com.google.android.gms.ads.internal.client.zzaa.<clinit>(Unknown Source)
                      at com.google.android.gms.ads.AdRequest.<clinit>(Unknown Source)
                      at com.google.android.gms.ads.AdRequest$Builder.<init>(Unknown Source)
                      at tech.pocket.converter.Temperature.onCreate(Temperature.java:57)
                      at android.app.Activity.performCreate(Activity.java:6259)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
                      at android.app.ActivityThread.-wrap11(ActivityThread.java)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:148)
                      at android.app.ActivityThread.main(ActivityThread.java:5443)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

1 个答案:

答案 0 :(得分:0)

尝试使用

更新您的gradle dependecy

compile 'com.google.android.gms:play-services-ads:9.6.1'