用于MenuItem ActionBarSherlock的Admob NullPointer

时间:2013-06-13 17:49:02

标签: android-fragments actionbarsherlock admob menuitem

我正在使用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;

    }

}

1 个答案:

答案 0 :(得分:0)

确保你做了一个干净的构建。

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