Question

I've got a list view with a custom adapter. In the list view are different elements with different XML layouts. One of them is this:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/com.example.myapp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >    

    <include layout="@layout/list_row_top" />

    <antistatic.spinnerwheel.WheelHorizontalView 
        android:id="@+id/wP_pollrate"
        app:visibleItems="4"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"/>

</LinearLayout>

In the adapter i have the getView method which looks like this:

@Override
  public View getView(int position, View convertView, ViewGroup parent) {

    View v = convertView;
    ConfigItem c = configItems.get(position);

    if(c instanceof ConfigFilePicker) {
      if (v == null) {
         LayoutInflater vi = (LayoutInflater)appContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);       
         v = vi.inflate(R.layout.list_row_config_picker, null);
      }      
      TextView topLabel = (TextView) v.findViewById(R.id.tV_list_row_top_label);
      TextView configFileName = (TextView) v.findViewById(R.id.tV_list_row_config);

      topLabel.setText(c.getTopLabel());
      configFileName.setText(((ConfigFilePicker) c).getChoosenFileName());
    }

    else if(c instanceof ConfigPollrate) {
      if (v == null) {
         LayoutInflater vi = (LayoutInflater)appContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);       
         v = vi.inflate(R.layout.list_row_config_pollrate, null);
      }
      TextView topLabel = (TextView) v.findViewById(R.id.tV_list_row_top_label);
      topLabel.setText(c.getTopLabel());      

      /** Setting adapter for picker wheel */
      AbstractWheel wheel = (AbstractWheel) v.findViewById(R.id.wP_wheel);
      ArrayWheelAdapter<String> wheelAdapter =
          new ArrayWheelAdapter<String>(this.appContext, ((ConfigPollrate) c).getValues());

      /** Setting layout and finally the adapter to the view */
      wheelAdapter.setItemResource(R.layout.wheel_text_centered_dark_back);
      wheelAdapter.setItemTextResource(R.id.text);
      wheel.setViewAdapter(pollrateAdapter);

    }

    return v;
  }

As you see i am using a special picker wheel called antistatic.spinnerwheel from this project android-spinnerwheel.

The problem is that this AbstractWheel wheel = (AbstractWheel) v.findViewById(R.id.wP_wheel); always results in a null pointer. The id is found tho'.

What i am doing wrong?

Was it helpful?

Solution

Your list item views are recycling, so convertView is not null in row 2->x but contains layout inflated in row before. So in case first layout is created at the beginning and then in second row it goes to else clause it will crash because layout list_row_config_picker doesn't contain AbstractWheel. Hope you understand what I mean.

You can remove if (v == null) check, so layout will be always inflating.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top