I'm doing an app using ActionBarSherlock, fragments and Admob ads. The app works fine without the ads. It also works fine after I add the Admob jar file to the project, after I update my manifest to include the ad activity, and correct permissions. It crashes when I try to add the ads to a fragment layout. Then the onCreateOptionsMenu throws a null pointer exception.
Here is the xml of the fragment:
<?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>
Code that crashes: (Null pointer exception when trying to access menu item 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;
}
}
Make sure you have done a clean build.
There is nothing wrong with any of the config or code that you have posted. I suspect that you are getting an mismatched resource class into your build.