2013-04-30 56 views
2
public class ListPage extends ListActivity 
{ 

ListView months; 
ArrayAdapter my; 
String[] monthlist = {"jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sept", "oct", "nov", "dec"}; 

protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.list_page); 

    months = (ListView)findViewById(R.id.list); 

    my = new ArrayAdapter(this,android.R.layout.simple_list_item_1,monthlist); 

    months.setAdapter(my); 
} 

}如何使用intent启动ListActivity?

我试着推出从另一个活动这个ListActivity,但刚开错误“8月4日至三十○日:如果我改变extends ListActivityextends Activity,该

E/AndroidRuntime(2010): java.lang.RuntimeException: Unable to start activity 
ComponentInfo{com.example.listinflator_demo/com.example.listinflator_demo. 
ListPage}: java.lang.RuntimeException: Your content must have a ListView 
whose id attribute is 'android.R.id.list'" 

:30:45.234代码工作就好了我如何使它工作使用extends ListActivity ??

+1

您的内容必须有一个ListView其id属性为“android.R.id.list”“ – njzk2 2013-04-30 08:47:02

回答

0

ListActivity有一个默认布局,它由屏幕中央的单个全屏列表组成。但是,如果您愿意,可以通过在onCreate()中使用setContentView()设置您自己的视图布局来自定义屏幕布局。要做到这一点,你自己的观点必须包含id为一个ListView对象“@android:ID /列表”(或列表,如果它在代码)

所以在您的list_page.xml添加android:id="@android:id/list"在列表视图

像:

<ListView android:id="@android:id/list" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:background="#00FF00" 
       android:layout_weight="1" 
       android:drawSelectorOnTop="false"/> 

查看此link了解更多详情。这里是example


编辑:

如果你使用ListActivity那么你的代码将是:

public class ListPage extends ListActivity 
{ 


ArrayAdapter my; 
String[] monthlist = {"jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sept", "oct", "nov", "dec"}; 

protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.list_page); 



    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 
    android.R.layout.simple_list_item_1, monthlist); 
    setListAdapter(adapter); 
} 
+0

”个月=(ListView控件)findViewById(R.id.list);“ 现在这行显示一个错误,说”列表不能解决或不是一个字段“ 我现在要做的? – 2013-04-30 08:52:36

+0

接受此为答..... – 2013-04-30 09:28:17

1

你的错误清楚地表明:

E/AndroidRuntime(2010):java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.listinflator_demo/com.example.listinflator_demo.ListPage}:java.lang.RuntimeException:您的内容必须有一个ListView id属性是“android.R.id.list””

确保在XML版式文件:list_page您正在设置为contentView你以ID定义的ListView对象Activitylist

0

我认为你必须使用字符串类型的ArrayAdapter,因为你已经使用字符串阵列来存储月份

ArrayAdapter<string> my = new ArrayAdapter<string>(this,android.R.layout.simple_list_item_1,monthlist); 

PS :: 删除一个ArrayAdapter申报开始,而您对上面有以下变化

相关问题