【问题标题】:Android for Dummies code errorAndroid for Dummies 代码错误
【发布时间】:2012-05-10 00:11:22
【问题描述】:

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

【问题讨论】:

    标签: java android


    【解决方案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);
      }
    }
    

    【讨论】:

    • 谢谢你解决了我的问题
    • 我仍然认为我的回答更好:P
    • 这是一个很好的,如果对称的,竞争。 (我赢的理由是我什至没有安卓设备,也没有写过任何安卓代码吗?)
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2019-01-11
      • 1970-01-01
      • 2021-05-28
      • 2019-12-10
      • 2016-05-13
      • 2015-03-05
      • 1970-01-01
      • 2016-01-20
      • 2012-09-24
      相关资源
      最近更新 更多