小弟下载了一个源码,读取通讯录中的信息,代码如下。
可是运行有就出现“The application ...has stopped unexpectedly.”
用logcat抓出一段信息,不知道什么意思,不知道怎么改!
Activity01.java
package com.yarin.android.Examples_03_02;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.PhoneLookup;
import android.widget.TextView;
public class Activity01 extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
TextView tv = new TextView(this);
String string = "";
super.onCreate(savedInstanceState);
//得到ContentResolver对象
ContentResolver cr = getContentResolver();
//取得电话本中开始一项的光标
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
//向下移动一下光标
while(cursor.moveToNext())
{
//取得联系人名字
int nameFieldColumnIndex = cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME);
String contact = cursor.getString(nameFieldColumnIndex);
//取得电话号码
int numberFieldColumnIndex = cursor.getColumnIndex(PhoneLookup.NUMBER);
String number = cursor.getString(numberFieldColumnIndex);
string += (contact+":"+number+"\n");
}
cursor.close();
//设置TextView显示的内容
tv.setText(string);
//显示到屏幕
setContentView(tv);
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.yarin.android.Examples_03_02"
android:versionCode="1"
android:versionName="1.0">
<uses-permission
android:name="android.permission.READ_CONTACTS">
</uses-permission>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Activity01"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="6" />
</manifest>
logcat 错误信息
05-24 13:58:18.052: ERROR/CursorWindow(227): Bad request for field slot 0,-1. numRows = 2, numColumns = 17
05-24 13:58:18.052: DEBUG/AndroidRuntime(227): Shutting down VM
05-24 13:58:18.052: WARN/dalvikvm(227): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
05-24 13:58:18.052: ERROR/AndroidRuntime(227): Uncaught handler: thread main exiting due to uncaught exception
05-24 13:58:18.062: DEBUG/KeyguardViewMediator(53): pokeWakelock(5000)
05-24 13:58:18.171: ERROR/AndroidRuntime(227): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.yarin.android.Examples_03_02/com.yarin.android.Examples_03_02.Activity01}: java.lang.IllegalStateException: get field slot from row 0 col -1 failed
05-24 13:58:18.171: ERROR/AndroidRuntime(227): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2481)
05-24 13:58:18.171: ERROR/AndroidRuntime(227): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2497)