0

カスタム アダプターを使用したリスト ビューがあります。リスト ビューには、さまざまな XML レイアウトを持つさまざまな要素があります。それらの1つはこれです:

<?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>

アダプターには、次のような getView メソッドがあります。

@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;
  }

antistatic.spinnerwheelご覧のとおり、このプロジェクトandroid-spinnerwheelから呼び出される特別なピッカー ホイールを使用しています。

問題は、これAbstractWheel wheel = (AbstractWheel) v.findViewById(R.id.wP_wheel);が常に null ポインターになることです。IDは見つかりました。

私は何を間違っていますか?

4

1 に答える 1

1

リスト項目ビューはリサイクルされているため、convertView は行 2->x で null ではありませんが、前の行で膨張したレイアウトが含まれています。したがって、最初のレイアウトが最初に作成され、次に 2 行目にある場合、レイアウトに AbstractWheel が含まれていないelseため、句がクラッシュします。list_row_config_picker私の言いたいことを理解していただければ幸いです。

チェックを外すことができるif (v == null)ので、レイアウトは常に膨張します。

于 2013-07-19T20:55:10.183 に答える