程序一运行就异常强制关闭,也不知道错误原因,求助,谢谢XML<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  >
  
<TextView
android:id="@+id/content01"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
/><CheckBox
android:id="@+id/textbox01"
android:text="Done?"
android:layout_below="@id/content01"
/>
<TextView
android:id="@+id/priority01"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_alignRight="@id/textbox01"
/>
    
</RelativeLayout>java代码:public class Lab02Activity extends Activity {
    /** Called when the activity is first created. */
private Button bottom_button;
private ListView list;
private TextView item_content;
private CheckBox item_check;
private TextView item_priority;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        bottom_button = (Button)findViewById(R.id.button01);
        list = (ListView)findViewById(R.id.listview01);
        item_content = (TextView)findViewById(R.id.content01);
        item_check = (CheckBox)findViewById(R.id.textbox01);
        item_priority = (TextView)findViewById(R.id.priority01);
        
        ArrayList<HashMap<String,Object>> listitem = new ArrayList<HashMap<String,Object>>();
        for(int i=0;i<5;i++)
        {
         HashMap<String, Object> map = new HashMap<String, Object>();
         map.put("content", "homework");
         //map.put("done", "true");
         map.put("priority", "normal");
         listitem.add(map);
        }
        
        SimpleAdapter listItemAdapter = new SimpleAdapter(this, listitem, R.layout.listviewitem_layout,new String[]{"content","priority"} , new int[] {R.id.content01,R.id.priority01});
        list.setAdapter(listItemAdapter);
    }
}

解决方案 »

  1.   


    setContentView(R.layout.main);//Acitivty加载的是main.xml
    //下面两个控件不在main.xml中,肯定出错啊!!
    item_content = (TextView)findViewById(R.id.content01);
    item_check = (CheckBox)findViewById(R.id.textbox01);SimpleAdapter listItemAdapter = new SimpleAdapter(this, listitem, R.layout.listviewitem_layout,new String[]{"content","priority"} , new int[] {R.id.content01,R.id.priority01});
    //R.id.priority01不在listviewitem_layout.xml上,肯定不能显示修改:public class Lab02Activity extends Activity {
        /** Called when the activity is first created. */
        private Button bottom_button;
        private ListView list;
        private TextView item_content;
        private CheckBox item_check;
        private TextView item_priority;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            bottom_button = (Button)findViewById(R.id.button01);
            list = (ListView)findViewById(R.id.listview01);
           
            
            ArrayList<HashMap<String,Object>> listitem = new ArrayList<HashMap<String,Object>>();
            for(int i=0;i<5;i++)
            {
                HashMap<String, Object> map = new HashMap<String, Object>();
                map.put("content", "homework");
                //map.put("done", "true");
                map.put("priority", "normal");
                listitem.add(map);
            }
            
            SimpleAdapter listItemAdapter = new SimpleAdapter(this, listitem, R.layout.listviewitem_layout,new String[]{"content","priority"} , new int[] {R.id.content01,R.id.textbox01});
            list.setAdapter(listItemAdapter);
        }
    }