package com.example.chapter4_ui;import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.widget.ListAdapter;
import android.widget.SimpleCursorAdapter;public class SimpleCursorAdapters extends ListActivity{
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);

//获得通讯录联系人游标对象Cursor
Cursor c=getContentResolver().query(People.CONTENT_URI, null, null, null, null);
startManagingCursor(c);

//实例化列表适配器
ListAdapter adapter=new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1,
c,
new String[] {People.NAME},
new int[] {android.R.id.text1});
setListAdapter(adapter);
}}

解决方案 »

  1.   

    贴错误提示
    使用logcat查看错误的源头
      

  2.   

    在manifest文件添加权限  
    <uses-permission android:name="android.permission.READ_CONTACTS" />
      

  3.   

    代码问题很多...需要继续努力啊
    1、onCreate中不宜做阻塞的数据查询
    2、数据查询应该是异步的
    3、联系人数据查询没必要查询所有数据,根据需求,看你下文只需要一个name,那么查询语句应该是
     Cursor c=getContentResolver().query(People.CONTENT_URI, new String[]{People.NAME}, null, null, null);
      

  4.   


    我按照你的第三条改了下,结果又不能运行了,第二个参数还是的改成null但是运行出来没有显示任何内容,不知道是为什么
      

  5.   

    如果是刚刚学习ANdroid开发的建议你先学习基础啊
      

  6.   

    妹子 就是有优势 没有log  还有这么多人给你分析 
      

  7.   


    我看的《Android核心技术与实例详解》这本书,书上有一些例子,我就照着敲,不知道怎么学起,于是就抓了一本书看呢~
      

  8.   


    我就是在Android手机上运行的啊、、、
      

  9.   

    四个问题
    1. 没有加载layout 
    ListActivity需要一个id为@android:list 的 ListView
    所以需要新建一个布局文件如下:(以文件名为activity_main.xml为例)<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity" >    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world" />
        
    <ListView android:id="@id/android:list" 
        android:layout_width="fill_parent" 
        android:layout_height="0dip" 
        android:layout_weight="1" 
        android:drawSelectorOnTop="false" 
        /> 
    </LinearLayout>
    然后在super.onCreate(savedInstanceState);下加上
    setContentView(R.layout.activity_main); //参数改为自己布局文件的ID2. 2.x版本安卓通讯录数据库的结构比例子中的复杂
    例子中的android.provider.Contacts.People 类已经不存在了
    所以获得通讯录联系人Cursor的语句改为
    Cursor c=getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    实例化列表适配器的语句改为
    ListAdapter adapter=new SimpleCursorAdapter(this,
                    android.R.layout.simple_list_item_1,
                    c,
                    new String[] {ContactsContract.Contacts.DISPLAY_NAME_PRIMARY},
                    new int[] {android.R.id.text1});解决如上两个问题已经可以看见联系人了 但是还有问题3. Cursor对象没有释放 要在重载的onDestory()中调用c.close();方法4. startManagingCursor(Cursor c)方法 和 SimpleCursorAdapter (Context context, int layout, Cursor c, String[] from, int[] to)构造函数 都是被弃用的
    如4楼所说 因为这两个方法都是是阻塞的  需要用 CursorLoader进行异步的查询 否则将会导致UI的无响应 甚至ANR错误
      

  10.   

    全代码 只解决到问题3
    MainActivity.java
    package com.examples.listacrivity;//打错字了。。 t和r太近了什么的=。=import android.app.ListActivity;
    import android.database.Cursor;
    import android.os.Bundle;
    import android.provider.ContactsContract;
    import android.view.Menu;
    import android.widget.ListAdapter;
    import android.widget.SimpleCursorAdapter;public class MainActivity extends ListActivity {
    Cursor c;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //获得通讯录联系人游标对象Cursor
            c=getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
            startManagingCursor(c);
            //实例化列表适配器
            ListAdapter adapter=new SimpleCursorAdapter(this,
                    android.R.layout.simple_list_item_1,
                    c,
                    new String[] {ContactsContract.Contacts.DISPLAY_NAME_PRIMARY},
                    new int[] {android.R.id.text1});
            setListAdapter(adapter);
    }
    public void onDestory(){
    super.onDestroy();
    c.close();
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
    }}res/layout/activity_main.xml<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity" >    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/hello_world" />
        
    <ListView android:id="@id/android:list" 
        android:layout_width="fill_parent" 
        android:layout_height="0dip" 
        android:layout_weight="1" 
        android:drawSelectorOnTop="false" 
        /> 
    </LinearLayout>
      

  11.   

    你继承listviewactivity就不用加载一个默认的main_layout布局文件?这个很长时间我没碰这个了,所以有点记不得了!
    如果是使用的listviewactivity带的呢,你确实要按照22楼的做些更正!
      

  12.   


    现在终于可以运行出结果啦!!!!
    但是加了setContentView()之后反而无法运行了,去了这句才能运行,不知道为嘛呢~
    之前貌似是因为你说的那个People过期了好啦~都怪我看的书版本太老了~~~
    谢谢你咯~~~终于解决了~弄了快大半个月了~菜鸟伤不起噢~
      

  13.   


    照着书敲了,然后不懂的类和方法我都会查API这样可以么~~~
      

  14.   

    没LOG竟然有这么多人回贴,果然是小女子三个字给力,猿猴们的女神!