ListView和adapter模拟器无法运行

我刚接触安卓,今天学列表视图编写了一个小程序代码如下:
package com.example.jiemian;

import android.R.string;
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class MainActivity extends ListActivity {

EditText et_xm,et_xh,et_csrq;
Button bt_zc,bt_cz;
RadioGroup rg_xb;
ListView lv_xianshi;
RadioButton rb;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    et_xm=(EditText) findViewById(R.id.et_xm);
    et_xh=(EditText) findViewById(R.id.et_xh);
    et_csrq=(EditText) findViewById(R.id.et_csrq);
    bt_zc=(Button) findViewById(R.id.bt_zc);
    bt_cz=(Button) findViewById(R.id.bt_cz);
    rg_xb=(RadioGroup) findViewById(R.id.rg_xb);
    rg_xb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // TODO Auto-generated method stub
            rb=(RadioButton) findViewById(checkedId);
            rb.getText();
        }
    });
    bt_zc.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String xh=et_xh.getText().toString();
            String xm=et_xm.getText().toString();
            String xb=rb.getText().toString();
            String csrq=et_csrq.getText().toString();

            String type[]=new String[]{xh,xm,xb,csrq};
            ArrayAdapter<String> adapter =new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_spinner_item,type);
            setListAdapter(adapter);
        }
    });
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}
结果在模拟器中无法运行,具体错误如下:
03-27 14:26:43.649: E/AndroidRuntime(1335): FATAL EXCEPTION: main
03-27 14:26:43.649: E/AndroidRuntime(1335): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.jiemian/com.example.jiemian.MainActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
03-27 14:26:43.649: E/AndroidRuntime(1335): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
03-27 14:26:43.649: E/AndroidRuntime(1335): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-27 14:26:43.649: E/AndroidRuntime(1335): at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-27 14:26:43.649: E/AndroidRuntime(1335): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-27 14:26:43.649: E/AndroidRuntime(1335): at android.os.Handler.dispatchMessage(Handler.java:99)
03-27 14:26:43.649: E/AndroidRuntime(1335): at android.os.Looper.loop(Looper.java:137)
03-27 14:26:43.649: E/AndroidRuntime(1335): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-27 14:26:43.649: E/AndroidRuntime(1335): at java.lang.reflect.Method.invokeNative(Native Method)
03-27 14:26:43.649: E/AndroidRuntime(1335): at java.lang.reflect.Method.invoke(Method.java:511)
03-27 14:26:43.649: E/AndroidRuntime(1335): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-27 14:26:43.649: E/AndroidRuntime(1335): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-27 14:26:43.649: E/AndroidRuntime(1335): at dalvik.system.NativeStart.main(Native Method)
03-27 14:26:43.649: E/AndroidRuntime(1335): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
03-27 14:26:43.649: E/AndroidRuntime(1335): at android.app.ListActivity.onContentChanged(ListActivity.java:243)
03-27 14:26:43.649: E/AndroidRuntime(1335): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:273)
03-27 14:26:43.649: E/AndroidRuntime(1335): at android.app.Activity.setContentView(Activity.java:1881)
03-27 14:26:43.649: E/AndroidRuntime(1335): at com.example.jiemian.MainActivity.onCreate(MainActivity.java:29)
03-27 14:26:43.649: E/AndroidRuntime(1335): at android.app.Activity.performCreate(Activity.java:5104)
03-27 14:26:43.649: E/AndroidRuntime(1335): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-27 14:26:43.649: E/AndroidRuntime(1335): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-27 14:26:43.649: E/AndroidRuntime(1335): ... 11 more
求大神指点一下这个问题???

4个回答

ListActivity本身是个布局了,你又来activity_main这个布局,肯定报错了。。。

布局文件有吗,listView未实例化,报错在绑定布局,先看布局问题

你布局文件必须有一个listview

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!