我无法在点击监听器的列表视图中放置插页式广告。 我只是堆叠到这里我找到答案,但没有一个做得很好。我正在尝试,但我找不到任何错误。
任何人都可以告诉我问题在哪里&什么是解决方案。
这是我的代码
public class ListViewAdapter extends BaseAdapter {
// Declare Variables
private PublisherInterstitialAd mInterstitialAd;
Context context;
LayoutInflater inflater;
ArrayList<HashMap<String, String>> data;
ImageLoader imageLoader;
HashMap<String, String> resultp = new HashMap<String, String>();
public ListViewAdapter(Context context,
ArrayList<HashMap<String, String>> arraylist) {
this.context = context;
data = arraylist;
imageLoader = new ImageLoader(context);
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
public View getView(final int position, View convertView, ViewGroup parent) {
// Declare Variables
TextView video;
ImageView image;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.listview_item, parent, false);
// Get the position
resultp = data.get(position);
// Locate the TextViews in listview_item.xml
//rank = (TextView) itemView.findViewById(R.id.rank);
video = (TextView) itemView.findViewById(R.id.country);
// population = (TextView) itemView.findViewById(R.id.population);
// Locate the ImageView in listview_item.xml
image = (ImageView) itemView.findViewById(R.id.flag);
// Capture position and set results to the TextViews
// rank.setText(resultp.get(MainActivity.VIDEO_ID));
video.setText(resultp.get(MainActivity.TITLE));
// population.setText(resultp.get(MainActivity.DESCRIPTION));
// Capture position and set results to the ImageView
// Passes flag images URL into ImageLoader.class
imageLoader.DisplayImage(resultp.get(MainActivity.THUMBNAILS), image);
itemView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
InterstitialAd mInterstitialAd = new InterstitialAd(context);
mInterstitialAd.setAdUnitId("ca-app-pub-4265785833148880/6768099054");
AdRequest adRequest = new AdRequest.Builder().build();
mInterstitialAd.loadAd(adRequest);
mInterstitialAd.setAdListener(new AdListener() {
public void onAdLoaded() {
displayInterstitial();
}
});
}
public void displayInterstitial() {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
}
else {
// Get the position
resultp = data.get(position);
Intent intent = new Intent(context, PlayerViewDemoActivity.class);
intent.putExtra("videoId", resultp.get(MainActivity.VIDEO_ID));
context.startActivity(intent);
}
}
});
return itemView;
}
哦,最后我在logcat中得到了一个java.lang.NullPointerException。
这里是logcat
12-09 22:15:27.675 26726-26726/com.nextappsbd.banglanatok2015 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.nextappsbd.banglanatok2015, PID: 26726
java.lang.NullPointerException
at com.nextappsbd.banglanatok2015.ListViewAdapter$1.displayInterstitial(ListViewAdapter.java:101)
at com.nextappsbd.banglanatok2015.ListViewAdapter$1$1.onAdLoaded(ListViewAdapter.java:95)
at com.google.android.gms.ads.internal.client.zzc.onAdLoaded(Unknown Source)
at com.google.android.gms.ads.internal.client.zzo$zza.onTransact(Unknown Source)
at android.os.Binder.transact(Binder.java:361)
at com.google.android.gms.ads.internal.client.t.c(SourceFile:152)
at com.google.android.gms.ads.internal.a.o(SourceFile:771)
at com.google.android.gms.ads.internal.x.o(SourceFile:386)
at com.google.android.gms.ads.internal.a.b(SourceFile:405)
at com.google.android.gms.ads.internal.b.b(SourceFile:149)
at com.google.android.gms.ads.internal.p.a.a(SourceFile:125)
at com.google.android.gms.ads.internal.p.m.a(SourceFile:102)
at com.google.android.gms.ads.internal.p.a.a(SourceFile:91)
at com.google.android.gms.ads.internal.u.c.e(SourceFile:398)
at com.google.android.gms.ads.internal.u.c.onPageFinished(SourceFile:374)
at com.android.webview.chromium.WebViewContentsClientAdapter.onPageFinished(WebViewContentsClientAdapter.java:475)
at com.android.org.chromium.android_webview.AwContentsClient$AwWebContentsObserver$1.run(AwContentsClient.java:73)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5344)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:676)
at dalvik.system.NativeStart.main(Native Method)