public class RingtonesActivity extends Activity {
private ListView listView;
private ListRingtonesAdapter adapter;
private ArrayList<SongInfo> listSong = new ArrayList<SongInfo>();
private Util util = new Util();
private ProgressBar progressBarParent;
private LinearLayout linearLayout_contentProgress;
private static final int PREFERENCES = Menu.FIRST;
private static final int QUIT = Menu.FIRST + 1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ringtonelist);
listView = (ListView) findViewById(R.id.list);
progressBarParent = (ProgressBar)findViewById(R.id.progressBarParent);
Resources res = getResources();
progressBarParent.setProgressDrawable(res.getDrawable(R.drawable.progressbarstyle));
linearLayout_contentProgress = (LinearLayout)findViewById(R.id.LL_contentProgressBarParent);
refreshList();
}
public static class Admob extends Activity {
InterstitialAd mInterstitialAd;
public void onCreate() {
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
requestNewInterstitial();
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
}
};
public void requestNewInterstitial() {
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("YOUR_DEVICE_HASH")
.build();
mInterstitialAd.loadAd(adRequest);
}
}
没有onCreate方法来包含Admob代码,当我尝试创建onCreate方法并添加admob代码时,我遇到了错误。
伙计们,非常欢迎任何帮助! :) 谢谢, 安娜
在我使用@SafwanHijazi的建议后,我收到了此错误
FATAL EXCEPTION: main
Process: ringtones.codebhak, PID: 20938
java.lang.NullPointerException
at ringtones.codebhak.RingtonesActivity.nativeDisplayAd(RingtonesActivity.java:73)
at ringtones.codebhak.ListRingtonesAdapter.getView(ListRingtonesAdapter.java:175)
at android.widget.AbsListView.obtainView(AbsListView.java:2715)
at android.widget.ListView.makeAndAddView(ListView.java:1801)
at android.widget.ListView.fillDown(ListView.java:697)
at android.widget.ListView.fillFromTop(ListView.java:763)
at android.widget.ListView.layoutChildren(ListView.java:1627)
at android.widget.AbsListView.onLayout(AbsListView.java:2528)
at android.view.View.layout(View.java:15655)
at android.view.ViewGroup.layout(ViewGroup.java:4856)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1677)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1531)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1440)
at android.view.View.layout(View.java:15655)
at android.view.ViewGroup.layout(ViewGroup.java:4856)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:15655)
at android.view.ViewGroup.layout(ViewGroup.java:4856)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1677)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1531)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1440)
at android.view.View.layout(View.java:15655)
at android.view.ViewGroup.layout(ViewGroup.java:4856)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:15655)
at android.view.ViewGroup.layout(ViewGroup.java:4856)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1677)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1531)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1440)
at android.view.View.layout(View.java:15655)
at android.view.ViewGroup.layout(ViewGroup.java:4856)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:15655)
at android.view.ViewGroup.layout(ViewGroup.java:4856)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:15655)
at android.view.ViewGroup.layout(ViewGroup.java:4856)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1677)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1531)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1440)
at android.view.View.layout(View.java:15655)
at android.view.ViewGroup.layout(ViewGroup.java:4856)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:15655)
at android.view.ViewGroup.layout(ViewGroup.java:4856)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:15655)
at android.view.ViewGroup.layout(ViewGroup.java:4856)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1677)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1531)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1440)
at android.view.View.layout(View.java:15655)
at android.view.ViewGroup.layout(ViewGroup.java:4856)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:15655)
at android.view.ViewGroup.layout(ViewGroup.java:4856)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2284)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2004)
at android.view.ViewRootImpl.doTrave
答案 0 :(得分:3)
尝试这样的事情:
import android.app.Activity;
import android.os.Bundle;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;
public class MyActivity extends Activity {
private InterstitialAd interstitial;
private boolean loaded = false;
private static MyActivity appActivity;
AdRequest adRequest;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
appActivity = this;
createAdmob();
}
private void createAdmob() {
// Create the interstitial.
interstitial = new InterstitialAd(this);
interstitial.setAdUnitId("YOUR UNIT ID");
// Create ad request.
adRequest = new AdRequest.Builder().build();
// Begin loading your interstitial.
interstitial.loadAd(adRequest);
loaded = true;
}
public static void nativeDisplayAd() {
appActivity.displayAd();
}
public void displayAd() {
// Just to load ad before display as required from admob sdk.
if (loaded) {
displayInterstitial();
loaded = false;
} else {
// or only load new ad.
createAdmob();
}
}
// Invoke displayInterstitial() when you are ready to display ad
// interstitial.
public void displayInterstitial() {
if (interstitial.isLoaded()) {
interstitial.show();
}
}
}
从你的适配器调用静态方法(nativeDisplayAd),但要注意加载后显示的广告。