0

テスト値を使用して ListView を実装しようとしています。リスト要素ごとに 2 つの文字列を表示する必要があります。しかし、NullPointerException が発生します。

Adapter を呼び出す ListActivity があります。このアクティビティの最後の行にコメントすると、エラーは発生しないので、アダプターに問題があると推測されます。しかし、Web で解決策を検索しましたが、残念ながら、Java (および OOP) のスキルが非常に低いため、問題を解決できません。どこで間違いを犯したか教えてください。

public class ListActivity extends Activity {

//private final String TAG = ListActivity.class.getSimpleName();
private ListView listMessView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list);

    List<Message> listMessages = new ArrayList<Message>();
    listMessages.add(new Message("London", "aze"));
    listMessages.add(new Message("Rome", "azeeaze"));
    listMessages.add(new Message("Paris", "qsdqsdqsd"));

    listMessView =  (ListView) findViewById(R.id.message_list);
    listMessView.setAdapter(new ListAdaptater(this, R.layout.list_row, listMessages));
    }
}


public class ListAdaptater extends ArrayAdapter<Message>{

  private int resource;
  private LayoutInflater inflater;
  private Context context;

  public ListAdaptater ( Context ctx, int resourceId, List<Message> objects) {

        super( ctx, resourceId, objects );
        resource = resourceId;
        inflater = LayoutInflater.from( context );
        context=ctx;
  }

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

        convertView = ( RelativeLayout ) inflater.inflate( resource, null );

        Message message = (Message) getItem( position );

        TextView txtName = (TextView) convertView.findViewById(R.id.user_name);
        txtName.setText(message.getUserName());

        TextView txtMsg = (TextView) convertView.findViewById(R.id.message);
        txtMsg.setText(message.getMessage());

        return convertView;
  }
  }
4

1 に答える 1

1

フィールドcontextは、初期化される前に使用されます。これを使用してみてください:

  public ListAdaptater ( Context ctx, int resourceId, List<Message> objects) {

        super( ctx, resourceId, objects );
        resource = resourceId;
        inflater = LayoutInflater.from( ctx );
        context=ctx;
  }
于 2013-01-13T18:35:16.183 に答える