尝试添加计时器以触发应用程序中的广告时出现错误

时间:2019-05-24 21:58:06

标签: java android

我尝试投放广告,但一直说该对象不能在另一个名为adTimer.schedule的类中运行

我尝试使用MainActivity.mInterstAd.show();那没用。

import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
import android.widget.Button;
 import android.view.View;
import android.util.Log;
 import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;
import com.google.android.gms.ads.MobileAds;
import java.util.Timer;
import java.util.TimerTask;



public class MainActivity extends AppCompatActivity {

private InterstitialAd mInterstAd;
public long adClockTimer = 1 * 60000; //by milliseconds

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Testing Ad Unit
    mInterstAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
    mInterstAd.loadAd(new AdRequest.Builder().build());
    //Timer Ran
    Timer adTimer = new Timer();
    adTimer.schedule(new TimerTask() {

        @Override
        public void run() {
            if (mInterstAd.isLoaded()) {
                mInterstAd.show();
            } else {
                Log.d("TAG", "The interstitial wasn't loaded yet.");
            }
        }
    }, 0, adClockTimer);


    //Button Listener
    final Button micB1 = findViewById(R.id.micButton);
    micB1.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Code here executes on main thread after user presses button
            if (mInterstAd.isLoaded()) {
                mInterstAd.show();
            } else {
                Log.d("TAG", "The interstitial wasn't loaded yet.");
            }
        }
    });

    mInterstAd.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
            // Code to be executed when an ad finishes loading.
        }

        @Override
        public void onAdFailedToLoad(int errorCode) {
            // Code to be executed when an ad request fails.
        }

        @Override
        public void onAdOpened() {
            // Code to be executed when the ad is displayed.
        }

        @Override
        public void onAdClicked() {
            // Code to be executed when the user clicks on an ad.
        }

        @Override
        public void onAdLeftApplication() {
            // Code to be executed when the user has left the app.
        }

        @Override
        public void onAdClosed() {
            // Code to be executed when the interstitial ad is closed.
            // Load the next interstitial.
            mInterstAd.loadAd(new AdRequest.Builder().build());
        }
    });
}
}

2019-05-24 16:02:04.402 6677-6778 / com.ernshu.www.learnlanguagexj E / Android运行时:致命异常:计时器-0     流程:com.ernshu.www.learnlanguagexj,PID:6677     java.lang.IllegalStateException:isLoaded必须在主UI线程上调用。         在com.google.android.gms.common.internal.Preconditions.checkMainThread处(未知来源:46)         com.google.android.gms.internal.ads.z​​zcqc.isReady(未知来源:76)         com.google.android.gms.internal.ads.z​​zabb.isLoaded(未知来源:20)         位于com.google.android.gms.ads.InterstitialAd.isLoaded(未知来源:7)         在com.ernshu.www.learnlanguagexj.MainActivity $ 1.run(MainActivity.java:41)         在java.util.TimerThread.mainLoop(Timer.java:555)         在java.util.TimerThread.run(Timer.java:505)

0 个答案:

没有答案