admob - 看不到广告,但日志显示广告已完成加载

标签 admob

我有以下代码。我看不到任何广告,但在日志消息中我可以看到广告已完成加载。

我所做的就是读取 json 并显示数据。我的布局 xml 中没有任何内容,并且我正在动态创建所有 View 元素......

package com.ark.itilfoundation;

import java.io.*;
import java.util.Random;
import org.json.*;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.*;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.*;

import com.google.android.gms.ads.*;
/**
 * @author Sravan Alaparthi
 */
public class Questionare extends Activity {

    JSONArray jArray;
    int navigator=0;
    RelativeLayout layout;
    private static InterstitialAd interstitial;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SharedPreferences sharedPreferences = this.getSharedPreferences("com.ark.itilfoundation", MODE_PRIVATE);
        layout = new RelativeLayout(this);      

        // Create the interstitial.
        interstitial = new InterstitialAd(this);

        interstitial.setAdUnitId("ca-app-pub-5273873598316367/6682193940");

        // Create ad request.
        AdRequest adRequest = new AdRequest.Builder()
        .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)       // Emulator
        .addTestDevice("AEA7763360CC914D4A30F78D9553511B") // My Galaxy Nexus test phone
        .build(); 

        interstitial.setAdListener(new AdListener() {});

        // Begin loading your interstitial.
        interstitial.loadAd(adRequest);

        // Invoke displayInterstitial() when you are ready to display an interstitial.
        displayInterstitial();

        //Get Data From Text Resource File Contains Json Data.    
        InputStream inputStream = getResources().openRawResource(R.raw.questions);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

        int ctr;
        try {
            ctr = inputStream.read();
            while (ctr != -1) {
                byteArrayOutputStream.write(ctr);
                ctr = inputStream.read();
            }
            inputStream.close();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        Log.v("Text Data", byteArrayOutputStream.toString());
        try {
            // Parse the data into json object to get original data in form of json.
            JSONObject jObject =
                    new JSONObject(byteArrayOutputStream.toString());
            JSONObject jObjectResult = jObject.getJSONObject("Questions");
            jArray = jObjectResult.getJSONArray("Question");

            layoutcreator(navigator,sharedPreferences);
            //  ArrayList<String[]> data = new ArrayList<String[]>();

        }
        catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static void displayInterstitial() {
        if (interstitial.isLoaded())
              interstitial.show();
    }

    public void layoutcreator(int i,SharedPreferences sharedPreferences){
        try {
            TextView tv1 = new TextView(this);          
            tv1.setText(jArray.getJSONObject(i).getString("Q"));
            tv1.setId(12);
            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
            layout.addView(tv1,lp);             

            final SharedPreferences sharedPreference = sharedPreferences;
            final int x =i+1;
            final int y =i-1;
            Button nextbt = new Button(this);
            if(i>=0 & i<59){
                nextbt.setText("Next");                
                nextbt.setId(123);

                nextbt.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        layout.removeAllViews();
                        layoutcreator(x,sharedPreference);
                    }
                });

                RelativeLayout.LayoutParams n = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                n.addRule(RelativeLayout.BELOW,tv1.getId());
                n.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
                layout.addView(nextbt, n);
            }
            if(i>=1 & i<=59){
                Button prevbt = new Button(this);
                prevbt.setText("Prev");                    

                prevbt.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        layout.removeAllViews();    
                        layoutcreator(y,sharedPreference);
                    }
                });
                RelativeLayout.LayoutParams np =
                        new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                np.addRule(RelativeLayout.BELOW,nextbt.getId());
                np.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);                          
                layout.addView(prevbt,np);
            }

            RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            p.addRule(RelativeLayout.BELOW,tv1.getId());
            p.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            layout.addView(createRadioButton(i,sharedPreferences),p);
            setContentView(layout);
        }
        catch (JSONException e) {
            e.printStackTrace();
        }

    }

    private RadioGroup createRadioButton(int a, SharedPreferences sharedPreferences) {

        RadioGroup rg = new RadioGroup(this); // create the RadioGroup
        rg.setOrientation(LinearLayout.VERTICAL);

        final SharedPreferences sharedPreference = sharedPreferences;
        final RadioButton rb  = new RadioButton(this);
        final RadioButton rb1  = new RadioButton(this);
        final RadioButton rb2  = new RadioButton(this);
        try {

            rb.setText(jArray.getJSONObject(a).getString("o1"));

            rb.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    if(sharedPreference.getBoolean("colorcheck", true)){
                        rb1.setTextColor(Color.WHITE);
                        rb2.setTextColor(Color.WHITE);
                        rb.setTextColor(Color.RED);
                    }
                    rb1.setChecked(false);
                    rb2.setChecked(false);

                }               
            });

        } catch (JSONException e) {
            e.printStackTrace();
        }
        try {
            rb1.setText(jArray.getJSONObject(a).getString("o2"));

            rb1.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    if(sharedPreference.getBoolean("colorcheck", true)){
                        rb1.setTextColor(Color.RED);
                        rb2.setTextColor(Color.WHITE);
                        rb.setTextColor(Color.WHITE);
                    }

                    rb.setChecked(false);
                    rb2.setChecked(false);
                }

            });

        }
        catch (JSONException e) {
            e.printStackTrace();
        }

        try {
            rb2.setText(jArray.getJSONObject(a).getString("a"));
            rb2.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    if(sharedPreference.getBoolean("colorcheck", true)){
                        rb1.setTextColor(Color.WHITE);
                        rb2.setTextColor(Color.GREEN);
                        rb.setTextColor(Color.WHITE);
                    }

                    rb1.setChecked(false);
                    rb.setChecked(false);

                }               
            });

        }
        catch (JSONException e) {
            e.printStackTrace();
        }

        Random rand = new Random(); 
        int pickedNumber = rand.nextInt(3); 

        if(pickedNumber == 0){
            rg.addView(rb2);
            rg.addView(rb1);
            rg.addView(rb);         
        }   

        if(pickedNumber == 1){
            rg.addView(rb1);
            rg.addView(rb2);
            rg.addView(rb);         
        }

        if(pickedNumber ==2){
            rg.addView(rb1);                    
            rg.addView(rb);
            rg.addView(rb2);
        }

        return rg; // return RadioGroup

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.options_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.list:
            startActivity(new Intent(getApplicationContext(), List.class));
            break;
        case R.id.settings:
            startActivity(new Intent(getApplicationContext(), ColorCheck.class));
            break;
        case R.id.scores:
            startActivity(new Intent(getApplicationContext(), Scores.class));
            break;
        }
        return true;
    }   
}

最佳答案

您可能遇到问题,因为您正在从#onCreate 调用#displayInterstitial。您不应该在加载后立即调用#displayInterstitial。

  1. 它可能不起作用(这就是您所看到的
  2. 这将使您的 Admob 帐户被禁止。

您应该在应用程序的自然断点处调用#displayInterstitial。

关于admob - 看不到广告,但日志显示广告已完成加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24709969/

相关文章:

android - AdMob 加载广告失败

flutter - flutter 的 admob 的替代品

iphone - AdMob 中介 SDK 不会抛出失败的广告?

android - 编写我自己的 Google 移动广告 (Admob) 中介适配器

Android + AdMob 问题

android - java.lang.NullPointerException 在 android.webkit.ZoomManager.setupZoomOverviewWidth(ZoomManager.java :1195)

android - 更新到 Android 11 后 AdMob 中出现严格的安全 Cookie 政策错误

android - 原生 Admob 广告导致窗口泄漏

android - 当应用程序处于 "pending publication"阶段并被评论者/测试者(Google Play 商店)使用/评论时,InstallerPackageName 是什么?

android - 如何获取自适应横幅 (Admob) API 30 的尺寸