简体   繁体   中英

Admob in navigation drawer layout causes app to crash on startup

Recently, Google changed the way admob ads are integrated in android apps, so it's been hard to find info about this that does it with google play services.

I have an app, which I got a lot of the code of from Github, and I'm trying to integrate admob ads with it. However, no matter how many different ways I try to order the layout, the admob linearLayout causes it to crash on startup. I'm trying to get it to show up on the bottom of the page.

Here is what the layout XML looks like without adding linearLayout for the admob:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<FrameLayout
    android:id="@+id/main_content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</FrameLayout>

<ListView
    android:id="@+id/main_left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@color/drawerColor"
    android:choiceMode="singleChoice"
    android:divider="@color/drawerDividerColor"
    android:dividerHeight="1dp" 
    android:paddingTop="10dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingBottom="20dp"/>

Here is my attempt at putting a linear layout to fit the ad in, which causes the crash:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" 
android:layout_marginBottom="50dp">

<FrameLayout
    android:id="@+id/main_content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</FrameLayout>

<ListView
    android:id="@+id/main_left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@color/drawerColor"
    android:choiceMode="singleChoice"
    android:divider="@color/drawerDividerColor"
    android:dividerHeight="1dp" 
    android:paddingTop="10dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingBottom="20dp"/>

<LinearLayout 
    android:id="@+id/linearLayout1"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"></LinearLayout>

Here is the code that sets up the adview:

private AdView adView;
private final String unitId = "MY UNIT ID";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    LinearLayout rootLayout = (LinearLayout) findViewById(R.id.linearLayout1);
    adView = new AdView(this);
    adView.setAdSize(AdSize.SMART_BANNER);
    adView.setAdUnitId(unitId);
    rootLayout.addView(adView,0);

and here is the logcat output when it crashes:

    10-15 17:43:42.263: I/Process(13465): Sending signal. PID: 13465 SIG: 9
10-15 17:43:42.373: D/AndroidRuntime(13511): Shutting down VM
10-15 17:43:42.373: W/dalvikvm(13511): threadid=1: thread exiting with uncaught exception (group=0x41510ba8)
10-15 17:43:42.373: E/AndroidRuntime(13511): FATAL EXCEPTION: main
10-15 17:43:42.373: E/AndroidRuntime(13511): Process: com.metrico.trailerparkboyssoundboard, PID: 13511
10-15 17:43:42.373: E/AndroidRuntime(13511): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.metrico.trailerparkboyssoundboard/com.metrico.trailerparkboyssoundboard.MainActivity}: java.lang.NullPointerException
10-15 17:43:42.373: E/AndroidRuntime(13511):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
10-15 17:43:42.373: E/AndroidRuntime(13511):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
10-15 17:43:42.373: E/AndroidRuntime(13511):    at android.app.ActivityThread.access$800(ActivityThread.java:135)
10-15 17:43:42.373: E/AndroidRuntime(13511):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
10-15 17:43:42.373: E/AndroidRuntime(13511):    at android.os.Handler.dispatchMessage(Handler.java:102)
10-15 17:43:42.373: E/AndroidRuntime(13511):    at android.os.Looper.loop(Looper.java:136)
10-15 17:43:42.373: E/AndroidRuntime(13511):    at android.app.ActivityThread.main(ActivityThread.java:5017)
10-15 17:43:42.373: E/AndroidRuntime(13511):    at java.lang.reflect.Method.invokeNative(Native Method)
10-15 17:43:42.373: E/AndroidRuntime(13511):    at java.lang.reflect.Method.invoke(Method.java:515)
10-15 17:43:42.373: E/AndroidRuntime(13511):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
10-15 17:43:42.373: E/AndroidRuntime(13511):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
10-15 17:43:42.373: E/AndroidRuntime(13511):    at dalvik.system.NativeStart.main(Native Method)
10-15 17:43:42.373: E/AndroidRuntime(13511): Caused by: java.lang.NullPointerException
10-15 17:43:42.373: E/AndroidRuntime(13511):    at com.metrico.trailerparkboyssoundboard.MainActivity.onCreate(MainActivity.java:55)
10-15 17:43:42.373: E/AndroidRuntime(13511):    at android.app.Activity.performCreate(Activity.java:5231)
10-15 17:43:42.373: E/AndroidRuntime(13511):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-15 17:43:42.373: E/AndroidRuntime(13511):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
10-15 17:43:42.373: E/AndroidRuntime(13511):    ... 11 more
10-15 17:43:45.383: I/Process(13511): Sending signal. PID: 13511 SIG: 9

You need to call

#setContentView(R.layout.whatever_your_layout_is_called);

prior to looking for View resources from the layout.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM