I'm facing a Nullpointer exception when using the latest version of the compatibility lib (namely, v18 of compat-lib, released with the 4.3 Android version) and using the list navigation menu mode in the actionbar. The error occurs in the 2.3.3 version of android. I believe that's because in this version (and others versions), the whole actionbar code used is the compat-lib code.
When I click on the list menu (at the actionbar) my app crashes and I receive the following error at logcat:
07-30 18:17:59.296: E/AndroidRuntime(14701): FATAL EXCEPTION: main
07-30 18:17:59.296: E/AndroidRuntime(14701): java.lang.NullPointerException
07-30 18:17:59.296: E/AndroidRuntime(14701): at android.support.v7.internal.widget.ListPopupWindow$DropDownListView.measureHeightOfChildrenCompat(ListPopupWindow.java:1317)
07-30 18:17:59.296: E/AndroidRuntime(14701): at android.support.v7.internal.widget.ListPopupWindow.buildDropDown(ListPopupWindow.java:1062)
07-30 18:17:59.296: E/AndroidRuntime(14701): at android.support.v7.internal.widget.ListPopupWindow.show(ListPopupWindow.java:514)
07-30 18:17:59.296: E/AndroidRuntime(14701): at android.support.v7.internal.widget.SpinnerICS$DropdownPopup.show(SpinnerICS.java:758)
07-30 18:17:59.296: E/AndroidRuntime(14701): at android.support.v7.internal.widget.SpinnerICS.performClick(SpinnerICS.java:443)
07-30 18:17:59.296: E/AndroidRuntime(14701): at android.view.View$PerformClick.run(View.java:9109)
07-30 18:17:59.296: E/AndroidRuntime(14701): at android.os.Handler.handleCallback(Handler.java:587)
07-30 18:17:59.296: E/AndroidRuntime(14701): at android.os.Handler.dispatchMessage(Handler.java:92)
07-30 18:17:59.296: E/AndroidRuntime(14701): at android.os.Looper.loop(Looper.java:130)
07-30 18:17:59.296: E/AndroidRuntime(14701): at android.app.ActivityThread.main(ActivityThread.java:3683)
07-30 18:17:59.296: E/AndroidRuntime(14701): at java.lang.reflect.Method.invokeNative(Native Method)
07-30 18:17:59.296: E/AndroidRuntime(14701): at java.lang.reflect.Method.invoke(Method.java:507)
07-30 18:17:59.296: E/AndroidRuntime(14701): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:895)
07-30 18:17:59.296: E/AndroidRuntime(14701): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:653)
07-30 18:17:59.296: E/AndroidRuntime(14701): at dalvik.system.NativeStart.main(Native Method)
Here is the xml of the dropdown cell (called item_menu_dropdown_celula.xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/spinner_subitem_background_ab_boadicatema"
android:gravity="center"
android:minHeight="?android:attr/listPreferredItemHeight"
android:orientation="horizontal" >
<ImageView
android:id="@+id/icone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:scaleType="fitCenter" />
<TextView
android:id="@+id/texto"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dip"
android:gravity="left|center_vertical"
android:shadowColor="@android:color/black"
android:shadowDx="0"
android:shadowDy="1"
android:shadowRadius="0.1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@android:color/white"
android:textStyle="bold" />
</LinearLayout>
And here is the code of the spinner adapter:
public class SpinnerListMenu implements SpinnerAdapter{
private List<ItemMenu> itens;
private LayoutInflater inflater;
public SpinnerListMenu(Context context, List<ItemMenu> listaItens) {
this.itens = listaItens;
this.inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return itens.size();
}
@Override
public ItemMenu getItem(int arg0) {
return itens.get(arg0);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public int getItemViewType(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
View row = convertView;
if ((row == null) || (row.getTag() == null)) {
row = inflater.inflate(R.layout.item_menu_celula, null);
holder = new ViewHolder(row);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
ItemMenu atual = getItem(position);
holder.texto.setText(atual.getNomeDaArea());
holder.icone.setBackgroundResource(atual.getIconeMenuFechadoResource());
holder.icone.setScaleType(ScaleType.FIT_CENTER);
return row;
}
private class ViewHolder {
ImageView icone;
TextView texto;
public ViewHolder(View base) {
icone = (ImageView) base.findViewById(R.id.icone);
texto = (TextView) base.findViewById(R.id.texto);
}
}
@Override
public int getViewTypeCount() {
return 0;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isEmpty() {
return itens.size() == 0;
}
@Override
public void registerDataSetObserver(DataSetObserver observer) {
}
@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
final ViewHolderDD holder;
View row = convertView;
if ((row == null) || (row.getTag() == null)) {
row = inflater.inflate(R.layout.item_menu_dropdown_celula, null);
holder = new ViewHolderDD(row);
row.setTag(holder);
} else {
holder = (ViewHolderDD) row.getTag();
}
ItemMenu atual = getItem(position);
holder.texto.setText(atual.getNomeDaArea());
holder.icone.setBackgroundResource(atual.getIconeMenuAbertoResource());
return row;
}
private class ViewHolderDD {
ImageView icone;
TextView texto;
public ViewHolderDD(View base) {
icone = (ImageView) base.findViewById(R.id.icone);
texto = (TextView) base.findViewById(R.id.texto);
}
}
}
In the activity, I set the menu and his adapter with the code:
//This class correctly extends ActionBarActivity
//here we area inside onCreate method
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
ArrayList<ItemMenu> itens = = new ArrayList<ItemMenu>();
//Here I add 2 itens in the arraylist
//...
getSupportActionBar().setListNavigationCallbacks(
new SpinnerListMenu(this, itens), this);
More information to help. When, in my activity, I use an array adapter, like in...
SpinnerAdapter mSpinnerAdapter = ArrayAdapter.createFromResource(this, R.array.action_list,
android.R.layout.simple_spinner_dropdown_item);
It works, the bug doesn't occur.
Does anyone have a clue about what is causing this bug? (Or even if this is a bug from the compatibility lib itself (which is my guess, after looking at the stack trace and changing a lot of properties of my xml))
After posting this issue in the Android's issue tracker I received a much better answer (than my old one) from an Android project member. The right way to correct this is to replace the line
row = inflater.inflate(R.layout.item_menu_dropdown_celula, null);
by the line
row = inflater.inflate(R.layout.item_menu_dropdown_celula, parent, false);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With