首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

<<android应用开发揭秘>>其中一个例子异常

2012-03-27 
android应用开发揭秘其中一个例子错误Java codepublic class ListViweActivity extends Activity {Lin

<<android应用开发揭秘>>其中一个例子错误

Java code
public class ListViweActivity extends Activity {    LinearLayout m_LinearLayout = null;    ListView m_ListView = null;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        m_LinearLayout = new LinearLayout(this);        m_LinearLayout.setOrientation(LinearLayout.VERTICAL);        m_LinearLayout.setBackgroundColor(Color.BLACK);                m_ListView = new ListView(this);        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,                                            LinearLayout.LayoutParams.WRAP_CONTENT);        m_LinearLayout.addView(m_ListView, params);        setContentView(m_LinearLayout);        //获取数据库Phones的Cursor        Cursor cur = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);        startManagingCursor(cur);                ListAdapter adapter = new SimpleCursorAdapter(this,                // 定义List中每一行的显示模板                // 表示每一行包含两个数据项                android.R.layout.simple_list_item_2,                // 数据库的Cursor对象                cur,                // 从数据库的NAME和NUMBER两列中取数据                new String[] { PhoneLookup.DISPLAY_NAME, PhoneLookup.NUMBER },                // 与NAME和NUMBER对应的Views                new int[] { android.R.id.text1, android.R.id.text2 });       m_ListView.setAdapter(adapter);            }}

运行的时候,这ListAdapter adapter = new SimpleCursorAdapter(...) Caused by: java.lang.IllegalArgumentException: column 'number' does not exist
是什么原因呢

[解决办法]
2.0之后,获取联系人信息的URI不同了。。

ListAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_2, cur, 
new String[] { PhoneLookup.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER },
new int[] { android.R.id.text1, android.R.id.text2 });

热点排行