我寫Antivity
基於Android 2.1檢索合同信息:不能獲得基於合同信息在Android 2.1
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);
}
}
部署.apk
到仿真器和啓動活動後,我得到了以下錯誤:
12-20 12:51:27.183: ERROR/AndroidRuntime(951): FATAL EXCEPTION: main
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
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.access$2300(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: get field slot from row 0 col -1 failed
at android.database.CursorWindow.getString_native(Native Method)
at android.database.CursorWindow.getString(CursorWindow.java:329)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:49)
at android.database.CursorWrapper.getString(CursorWrapper.java:135)
at com.yarin.android.Examples_03_02.Activity01.onCreate(Activity01.java:30)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
... 11 more
這迫使應用程序關閉:
?目的是什麼? – user370305 2011-12-21 07:48:31