程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> ios-訪問收件箱的信息報錯

ios-訪問收件箱的信息報錯

編輯:編程綜合問答
訪問收件箱的信息報錯

我想訪問收件箱信息,寫了如下代碼:

Uri uriSMSURI = Uri.parse("content://sms/inbox");
    Cursor cur = getContentResolver().query(uriSMSURI, null, null, null,null);
    String sms = "";
    cur.moveToNext();
    sms = cur.getString(0);

然後報出如下錯誤:

01-07 12:31:37.222: E/AndroidRuntime(687): FATAL EXCEPTION: main
01-07 12:31:37.222: E/AndroidRuntime(687): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.inboxshowingapp/com.example.inboxshowingapp.MainActivity}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
01-07 12:31:37.222: E/AndroidRuntime(687):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
01-07 12:31:37.222: E/AndroidRuntime(687):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
01-07 12:31:37.222: E/AndroidRuntime(687):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
01-07 12:31:37.222: E/AndroidRuntime(687):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
01-07 12:31:37.222: E/AndroidRuntime(687):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-07 12:31:37.222: E/AndroidRuntime(687):  at android.os.Looper.loop(Looper.java:123)
01-07 12:31:37.222: E/AndroidRuntime(687):  at android.app.ActivityThread.main(ActivityThread.java:4627)
01-07 12:31:37.222: E/AndroidRuntime(687):  at java.lang.reflect.Method.invokeNative(Native Method)
01-07 12:31:37.222: E/AndroidRuntime(687):  at java.lang.reflect.Method.invoke(Method.java:521)
01-07 12:31:37.222: E/AndroidRuntime(687):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-07 12:31:37.222: E/AndroidRuntime(687):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-07 12:31:37.222: E/AndroidRuntime(687):  at dalvik.system.NativeStart.main(Native Method)
01-07 12:31:37.222: E/AndroidRuntime(687): Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
01-07 12:31:37.222: E/AndroidRuntime(687):  at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
01-07 12:31:37.222: E/AndroidRuntime(687):  at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
01-07 12:31:37.222: E/AndroidRuntime(687):  at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
01-07 12:31:37.222: E/AndroidRuntime(687):  at android.database.CursorWrapper.getString(CursorWrapper.java:135)
01-07 12:31:37.222: E/AndroidRuntime(687):  at com.example.inboxshowingapp.MainActivity.onCreate(MainActivity.java:32)
01-07 12:31:37.222: E/AndroidRuntime(687):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-07 12:31:37.222: E/AndroidRuntime(687):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
01-07 12:31:37.222: E/AndroidRuntime(687):  ... 11 more

最佳回答:


Uri uri = Uri.parse("content://sms/inbox");
Cursor c= getContentResolver().query(uri, null, null ,null,null);
startManagingCursor(c);

得到全部信箱信息:

body = new String[c.getCount()];
number = new String[c.getCount()];

if(c.moveToFirst()){
        for(int i=0;i<c.getCount();i++){
                body[i]= c.getString(c.getColumnIndexOrThrow("body")).toString();
                number[i]=c.getString(c.getColumnIndexOrThrow("address")).toString();
                c.moveToNext();
       }
   }
   c.close();
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved