0

タブ内にリストビューを実装しようとしています。そこで、カスタム arrayadapter を使用してカスタム リストビューを作成しています。コードは次のとおりです。

タブ アクティビティの作成:

TabHost th = getTabHost();
TabSpec specGroups = th.newTabSpec("Groups");
    specGroups.setIndicator("Groups");
    Intent intentGroups = new Intent(this, GroupsList.class);
    specGroups.setContent(intentGroups);

グループリスト アクティビティ :

public class GroupsList extends Activity {

public String[] ROSTER_LIST = {"Sam", "Bob", "Tabg", "Toushi", "john"};
private ListView ROSTER_LISTVIEW;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.groups_list);

    Log.d("STARTED", "0");

    ROSTER_LISTVIEW = (ListView) findViewById(R.id.listViewFriends);
    Log.d("STARTED", "1");

    ArrayAdapter<String> adapter = new MyAdapter(this,
            android.R.layout.simple_list_item_1, R.id.textViewRosterRow,
            ROSTER_LIST);
    Log.d("STARTED", "2");

    ROSTER_LISTVIEW.setAdapter(adapter);
    Log.d("STARTED", "3");

}

カスタム アダプター クラス (内部クラス) :

private class MyAdapter extends ArrayAdapter<String> {

    public MyAdapter(Context context, int resource, int textViewResourceId,
            String[] ROSTER_LIST) {
        super(context, resource, textViewResourceId, ROSTER_LIST);
        // TODO Auto-generated constructor stub
    }

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

        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        Log.d("ADAPTER", "0");
        View row = inflater
                .inflate(R.layout.friends_list_row, parent, false);
        Log.d("ADAPTER", "1");

        TextView text = (TextView) row.findViewById(R.id.textViewRosterRow);
        Log.d("ADAPTER", "2");
        text.setText(ROSTER_LIST[position]);

        Log.d("ADAPTER", "OK");
        return row;
    }

}

oncreate メソッドLog.d("STARTED", "2");行の内部では、logcat にログが記録され、nullpointerexception がポップされます。内部クラスLog内にある logcat にログはありません。MyAdapter

このコードは、タブなしで正常に実行されます。

私がここで犯した間違いは何ですか?どうすればこれを解決できますか? 前もって感謝します :)

4

1 に答える 1

0

Adapter getView を次のように変更します。

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

        View row = convertView;

        if(row==null){
          LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          Log.d("ADAPTER", "0");
          row = inflater
                .inflate(R.layout.friends_list_row, parent, false);
          Log.d("ADAPTER", "1");
      }
        TextView text = (TextView) row.findViewById(R.id.textViewRosterRow);
        Log.d("ADAPTER", "2");
        text.setText(ROSTER_LIST[position]);

        Log.d("ADAPTER", "OK");
        return row;
    }
于 2012-10-13T03:05:11.960 に答える