最近,Google改变了admob广告在Android应用中的集成方式,因此很难找到有关谷歌播放服务的信息。
我有一个应用程序,我从Github获得了很多代码,我正在尝试将admob广告与它集成。但是,无论我尝试订购布局的方式有多少,admob linearLayout都会导致它在启动时崩溃。我试图让它显示在页面底部。
以下是布局XML的外观,而不为admob添加linearLayout:
<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"/>
以下是我尝试使用线性布局来适应广告,这会导致崩溃:
<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>
以下是设置广告视图的代码:
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);
这是崩溃时的logcat输出:
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
答案 0 :(得分:0)
您需要致电
#setContentView(R.layout.whatever_your_layout_is_called);
在从布局中查找资源之前。