我有以下代码。我看不到任何广告,但在日志消息中,我看到广告已完成加载。
我所做的只是阅读json
并显示数据。我在布局xml中没有任何东西,我正在动态创建所有视图元素....
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;
}
}
答案 0 :(得分:3)
您可能遇到问题,因为您正在从#onCreate调用#displayInterstitial。 不应在加载后立即调用#displayInterstitial。
您应该在应用中的自然断点处调用#displayInterstitial。
答案 1 :(得分:0)
尝试在onCreate上对其进行初始化,但只能从事件(例如单击按钮)中调用show()方法。
您可以使用此代码,id起作用。
@Override
protected void onCreate(Bundle savedInstanceState) {
...
MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713"); //test id
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
mInterstitialAd.loadAd(new AdRequest.Builder().build());
mInterstitialAd.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 onAdLeftApplication() {
// Code to be executed when the user has left the app.
}
@Override
public void onAdClosed() {
// Code to be executed when when the interstitial ad is closed.
}
});
...
btn_test.setOnClickListener(view -> {
showInterstitial();
});
private void showInterstitial()
{
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
Log.d("TAG", "The interstitial wasn't loaded yet.");
}
}