0

Android app Development for Dummies 中,我在最后一段代码后出现错误。

该类型的方法setListAdapter(ArrayAdapter<String>)未定义ReminderListActivity

代码如下:

package com.dummies.android.taskreminder;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;

public class ReminderListActivity extends Activity {
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.reminder_list);

    String[] items = new String[] { "Foo", "Bar", "Fizz", "Bin"};
    ArrayAdapter<String> adapter =
      new ArrayAdapter<String>(this, R.layout.reminder_row, R.id.text1, items);
    setListAdapter(adapter);
  }
}
4

2 回答 2

1

在我看来Activity它没有,setListAdapter()但它的子类ListActivity有。也许你想要

public class ReminderListActivity extends ListActivity {

反而?这应该可以正常工作:

package com.dummies.android.taskreminder;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;

public class ReminderListActivity extends ListActivity {
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.reminder_list);

    String[] items = new String[] { "Foo", "Bar", "Fizz", "Bin"};
    ArrayAdapter<String> adapter =
      new ArrayAdapter<String>(this, R.layout.reminder_row, R.id.text1, items);
    setListAdapter(adapter);
  }
}
于 2012-05-10T00:22:07.440 回答
1

Activity没有定义方法setListAdapter()。我认为您想扩展ListActivity

public class ReminderListActivity extends ListActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.reminder_list);

        String[] items = new String[] { "Foo", "Bar", "Fizz", "Bin"};
        ArrayAdapter<String> adapter =
            new ArrayAdapter<String>(this, R.layout.reminder_row, R.id.text1, items);
        setListAdapter(adapter);
    }
}
于 2012-05-10T00:22:14.547 回答