2012-05-10 45 views
0

In Android應用程序開發傻瓜我在最後一段代碼後出現錯誤。Android 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); 
    } 
} 

回答

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

謝謝你,沒有解決我的問題 – Piggie2009

+0

我仍然認爲我的回答是更好的:P –

+0

這是一個很好的,如果對稱競爭。 (我贏得理由我甚至沒有擁有Android設備,我也沒有寫過任何android代碼?) – andersoj

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