我试图获取通话记录但我无法得到它并在android中获取和异常

时间:2016-10-26 08:47:23

标签: android android-studio

我正在尝试使用以下代码从Android手机获取通话记录:

Cursor c = getContentResolver().query(CallLog.Calls.CONTENT_URI,
                null, null, null, CallLog.Calls.DATE + " DESC");

        String num = c.getString(c.getColumnIndex(CallLog.Calls.NUMBER));// for  number

        calllog.setText(num);
        String name= c.getString(c.getColumnIndex(CallLog.Calls.CACHED_NAME));// for name
        String duration = c.getString(c.getColumnIndex(CallLog.Calls.DURATION));// for duration
        int type = Integer.parseInt(c.getString(c.getColumnIndex(CallLog.Calls.TYPE)));// for call type, Incoming or out going.

        String typestr=c.getString(type);
        StringBuilder SB = new StringBuilder();

        while(c != null && c.moveToNext()){
            //String pnum =c.getString(num)

            String callTypeStr="";
            switch(Integer.parseInt(typestr))
            {
                case CallLog.Calls.OUTGOING_TYPE:
                    callTypeStr="OutGoing";
                    break;
                case CallLog.Calls.INCOMING_TYPE:
                    callTypeStr="Incoming";
                    break;

                case CallLog.Calls.MISSED_TYPE:
                    callTypeStr="Missed";
                    break;

            }


            SB.append("Phone Number: "+num);
            SB.append("Name: "+name);
            SB.append("Call Duration: "+duration);
            SB.append("Call Type: "+callTypeStr);
            c.close();
        }

但是当我运行我的Android应用程序时,我的应用程序因以下空异常而停止:

/AndroidRuntime: FATAL EXCEPTION: Process: com.example.acer.fyp, PID:                                     java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.acer.fyp/com.example.acer.fyp.Get_Call_Log}: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 500
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2341)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2393)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1324)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5295)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 500
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:426)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:147)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:61)
at android.database.CursorWrapper.getString(CursorWrapper.java:114)
at com.example.acer.fyp.Get_Call_Log.onCreate(Get_Call_Log.java:39)
at android.app.Activity.performCreate(Activity.java:5264)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2393) 
at android.app.ActivityThread.access$800(ActivityThread.java:151) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1324) 
at android.os.Handler.dispatchMessage(Handler.java:110) 
at android.os.Looper.loop(Looper.java:193) 
at android.app.ActivityThread.main(ActivityThread.java:5295) 
at java.lang.reflect.Method.invokeNative(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:515)

getcoloumnindex中有空,但我不知道如何解决这个问题?

请帮帮我。

由于

1 个答案:

答案 0 :(得分:1)

在遍历Cursor之前使用c.moveToFirst()。