0

我正在使用 ActionBarSherlock、片段和 Admob 广告做一个应用程序。该应用程序在没有广告的情况下运行良好。在我将 Admob jar 文件添加到项目后,在我更新清单以包含广告活动和正确的权限后,它也可以正常工作。当我尝试将广告添加到片段布局时,它会崩溃。然后 onCreateOptionsMenu 会抛出空指针异常。

这是片段的xml:

<?xml version="1.0" encoding="utf-8"? >
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" 
android:gravity="center">
<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"  
    >
<TextView
    android:id="@+id/ticker"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="20sp"
    android:textStyle="bold" 
    android:gravity="center"
    android:textColor="#ffffff" 
    android:text="" />
<TextView
    android:id="@+id/space"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="20sp"
    android:text="" />
</LinearLayout>

 <LinearLayout
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >  
    <Button
    android:id="@+id/back"
    android:layout_width="0dip"
    android:layout_weight="1"
    android:layout_height="fill_parent"
    android:background="@drawable/custom_button"
    android:drawableTop="@drawable/ic_action_playback_rew"
    android:gravity="center"
    android:text=" Back " 
    android:textColor="@drawable/text"
    android:textSize="12sp"/>

    <Button
    android:id="@+id/pause"
    android:layout_width="0dip"
    android:layout_weight="1"
    android:layout_height="fill_parent"
    android:background="@drawable/custom_button"
    android:drawableTop="@drawable/ic_action_playback_pause"
    android:textColor="@drawable/text"
    android:text="Pause " 
    android:textSize="12sp"/>

       <Button
    android:id="@+id/start"
    android:layout_width="0dip"
    android:layout_weight="1"
    android:layout_height="fill_parent"
    android:background="@drawable/custom_button"
    android:drawableTop="@drawable/ic_action_playback_play"
    android:text="Start "
    android:textColor="@drawable/text"
    android:textSize="12sp" />

     <Button
    android:id="@+id/next"
    android:layout_width="0dip"
    android:layout_weight="1"
    android:layout_height="fill_parent"
    android:background="@drawable/custom_button"
    android:drawableTop="@drawable/ic_action_playback_forw"
    android:gravity="center"
    android:textColor="@drawable/text"
    android:text=" Next " 
    android:textSize="12sp"/>

 </LinearLayout>

 </LinearLayout>
 <com.google.ads.AdView android:id="@+id/ad"
                       android:layout_width="wrap_content"
                       android:layout_height="wrap_content"
                       android:layout_centerHorizontal="true"
                       android:layout_alignParentBottom="true"
                       ads:adSize="BANNER"
                       ads:adUnitId="a151b78112c1df4"
                       ads:testDevices="TEST_EMULATOR,TEST_DEVICE_ID_GOES_HERE"
                       ads:loadAdOnCreate="true"/>

</RelativeLayout>

崩溃的代码:(尝试访问菜单项 miAdvance 时出现空指针异常):

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    new MenuInflater(this).inflate(R.menu.main, menu);
    this.menu = menu;
    miAdvance = menu.findItem(R.id.menu_automatic);
    miDifficulty = menu.findItem(R.id.menu_settings);
    updateMenuTitles(miAdvance,miDifficulty);

    return(super.onCreateOptionsMenu(menu));
}

/*
 * as the name implies
 * updates titles to match settings of Difficulty and Advance settings
 */
private void updateMenuTitles() { 
    switch (Difficulty) { 
    case DIFFICULTY_BASIC:
        miDifficulty.setTitle("Basic");
        break;
    case DIFFICULTY_ADVANCED:
        miDifficulty.setTitle("Advanced");
        break;
    case DIFFICULTY_ADVANCED_AND_BASIC:
        miDifficulty.setTitle("All");
        break;
    default:
        miDifficulty.setTitle("Difficulty");
        break;

    }

    switch (Automatic) { 
    case ADVANCE_MANUAL:
        miAdvance.setTitle("Manual");
        break;
    case ADVANCE_AUTOMATIC:
        miAdvance.setTitle("Auto");
        break;
    default:
        miAdvance.setTitle("Progress");
        break;

    }

}
4

1 回答 1

0

确保你已经完成了一个干净的构建。

您发布的任何配置或代码都没有问题。我怀疑您在构建中获得了不匹配的资源类。

于 2013-06-14T17:02:29.943 回答