我正在尝试对我的应用程序进行广告,但它最终会崩溃。我之前有过xml但是删除它因为它不起作用。我已经尝试了很多东西,但现在这就是我最终的结果。
RelativeLayout rootLayout = (RelativeLayout) findViewById(R.id.RelativeLayout);
AdView adView = new AdView(this);
adView.setAdSize(AdSize.SMART_BANNER);
adView.setAdUnitId("**-***-***-***********/**************");
rootLayout.addView(adView, 0);
AdRequest adRequest = new AdRequest.Builder().
addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build();
adView.loadAd(adRequest);
我在我的Activities类的setContentView前添加了这个。 香港专业教育学院我错过了什么吗?我已经在我的MANIFEST文件中声明了所有内容。我已经导入了所有的库。
08-02 03:08:51.922: E/AndroidRuntime(5332): FATAL EXCEPTION: main
08-02 03:08:51.922: E/AndroidRuntime(5332): Process: com.nonikhanna.Rainbox, PID: 5332
08-02 03:08:51.922: E/AndroidRuntime(5332): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nonikhanna.Rainbox/com.nonikhanna.Rainbox.MenuActivity}: java.lang.NullPointerException
08-02 03:08:51.922: E/AndroidRuntime(5332): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
08-02 03:08:51.922: E/AndroidRuntime(5332): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
08-02 03:08:51.922: E/AndroidRuntime(5332): at android.app.ActivityThread.access$800(ActivityThread.java:135)
08-02 03:08:51.922: E/AndroidRuntime(5332): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
08-02 03:08:51.922: E/AndroidRuntime(5332): at android.os.Handler.dispatchMessage(Handler.java:102)
08-02 03:08:51.922: E/AndroidRuntime(5332): at android.os.Looper.loop(Looper.java:136)
08-02 03:08:51.922: E/AndroidRuntime(5332): at android.app.ActivityThread.main(ActivityThread.java:5017)
08-02 03:08:51.922: E/AndroidRuntime(5332): at java.lang.reflect.Method.invokeNative(Native Method)
08-02 03:08:51.922: E/AndroidRuntime(5332): at java.lang.reflect.Method.invoke(Method.java:515)
08-02 03:08:51.922: E/AndroidRuntime(5332): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-02 03:08:51.922: E/AndroidRuntime(5332): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-02 03:08:51.922: E/AndroidRuntime(5332): at dalvik.system.NativeStart.main(Native Method)
08-02 03:08:51.922: E/AndroidRuntime(5332): Caused by: java.lang.NullPointerException
08-02 03:08:51.922: E/AndroidRuntime(5332): at com.nonikhanna.Rainbox.MenuActivity.onCreate(MenuActivity.java:26)
08-02 03:08:51.922: E/AndroidRuntime(5332): at android.app.Activity.performCreate(Activity.java:5231)
08-02 03:08:51.922: E/AndroidRuntime(5332): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
08-02 03:08:51.922: E/AndroidRuntime(5332): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
08-02 03:08:51.922: E/AndroidRuntime(5332): ... 11 more
答案 0 :(得分:0)
试试这个
AdView adView = new AdView(getApplicationContext());
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId("***************");
AdRequest adRequest = new AdRequest.Builder().build();
LinearLayout adView= (LinearLayout) findViewById(R.id.adView);
layout.addView(adView);
adView.loadAd(adRequest);
答案 1 :(得分:0)
尝试在AdView上设置背景颜色:
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId(AD_UNIT_ID);
adView.setBackgroundColor(Color.TRANSPARENT);
如果您仍然遇到问题,请查看this thread
答案 2 :(得分:0)
使用它。在您的代码中,您可以在加载之前添加视图,这样您就可以获得NPE。使用此
RelativeLayout rootLayout = (RelativeLayout) findViewById(R.id.RelativeLayout);
AdView adView = new AdView(this);
adView.setAdSize(AdSize.SMART_BANNER);
adView.setAdUnitId("**-***-***-***********/**************");
AdRequest adRequest = new AdRequest.Builder().
addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build();
adView.loadAd(adRequest);
rootLayout.addView(adView, 0);
同时检查您的xml文件是RelativeLayout
,其ID与RelativeLayout
相同