【问题标题】:I couldn't retrieve contacts in android?我无法在android中检索联系人?
【发布时间】:2017-04-16 12:13:04
【问题描述】:

我是安卓的初学者。我想在 ListView 中显示联系人列表。我已经在 activity_main.xml 中有一个 ListView,我在 MainActivity.java 中提到了这一点。但是当我运行程序时出现错误

java.lang.RuntimeException:无法启动活动 组件信息{com.example.yuvi.contactsex/com.example.yuvi.contactsex.MainActivity}: java.lang.RuntimeException: 你的内容必须有一个 ID 的 ListView 属性是'android.R.id.list'

我从网站上得到了这个代码。我不知道在哪里更改代码。任何人都可以帮助我。

  • AndroidManifest.xml

    <uses-permission android:name="android.permission.READ_CONTACTS"/>
    
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
    
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    

  • activity_main.xml

  • MainActivity.java

    public class MainActivity extends ListActivity {
    
    ListView lv;
    Cursor cursor1;
    
    @Override
    public int getSelectedItemPosition() {
        return super.getSelectedItemPosition();
    }
    
    @Override
    public long getSelectedItemId() {
        return super.getSelectedItemId();
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        lv=(ListView)findViewById(R.id.list);
        // create a cursor to query the Contacts on the device to start populating a listview
        cursor1 = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
        startManagingCursor(cursor1);
    
        String[] from = {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER, ContactsContract.CommonDataKinds.Phone._ID}; // get the list items for the listadapter could be TITLE or URI
    
    
        int[] to = {android.R.id.text1, android.R.id.text2}; // sets the items from above string to listview
    
        // new listadapter, created to use android checked template
        SimpleCursorAdapter listadapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, cursor1, from, to);
    
    
        setListAdapter(listadapter);
    
        // adds listview so I can get data from it
        lv = getListView();
        lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    
    
      }
    }
    

【问题讨论】:

    标签: android android-contentprovider android-contacts


    【解决方案1】:

    java.lang.RuntimeException: 你的内容必须有一个 ID 为 ListView 属性是'android.R.id.list'

    当您使用 ListActivity 时,您必须在 xml 中的 id 属性中添加 @android

    <ListView 
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    

    希望这会有所帮助。

    【讨论】:

    • 我得到了输出。谢谢你。另一项帮助。当用户单击列表项时,我需要显示 Toast 消息(用户名)。我可以使用 onItemClickListerner 吗?我是否需要覆盖任何方法?
    • 是的,onItemClickListener 应该可以。 :)
    • 我的应用在我的 Android 模拟器中运行。但它不能在我的移动设备中运行?为什么?我需要修改什么吗?
    • 您可能需要添加权限才能检索联系人列表。这是文档-developer.android.com/training/permissions/requesting.html。不要忘记在清单中添加权限到
    猜你喜欢
    • 1970-01-01
    • 2012-03-12
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 2014-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多