我想从通话记录中计算给定时间段内每个联系人的总通话时长,但我在 while 循环中遇到了这个 NullPointerException:
Cursor c = cr.query(CallLog.Calls.CONTENT_URI, projection, selection, null, null);
if(c.moveToFirst()){
int[] duration = new int[c.getCount()];
String[] name = new String[c.getCount()];
do{
boolean w = false;
int y=0;
do{
if(name[y].equals("")){
name[y]=c.getString(c.getColumnIndex(CallLog.Calls.CACHED_NAME));
duration[y]+=Integer.parseInt(c.getString(c.getColumnIndex(CallLog.Calls.DURATION)));
w = true;
}else if(name[y].equals(c.getString(c.getColumnIndex(CallLog.Calls.CACHED_NAME)))){
duration[y]+=Integer.parseInt(c.getString(c.getColumnIndex(CallLog.Calls.DURATION)));
w = true;
}
y++;
}while(w);
}
if 语句中出现错误,其中if(name[y].equals(""))
游标投影蚂蚁选择似乎工作正常。
这是我的堆栈跟踪:
10-22 07:36:18.429: E/AndroidRuntime(27320): FATAL EXCEPTION: main
10-22 07:36:18.429: E/AndroidRuntime(27320): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dailyreports.ainius/com.dailyreports.ainius.ReportView}: java.lang.NullPointerException
10-22 07:36:18.429: E/AndroidRuntime(27320): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
10-22 07:36:18.429: E/AndroidRuntime(27320): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
10-22 07:36:18.429: E/AndroidRuntime(27320): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-22 07:36:18.429: E/AndroidRuntime(27320): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
10-22 07:36:18.429: E/AndroidRuntime(27320): at android.os.Handler.dispatchMessage(Handler.java:99)
10-22 07:36:18.429: E/AndroidRuntime(27320): at android.os.Looper.loop(Looper.java:123)
10-22 07:36:18.429: E/AndroidRuntime(27320): at android.app.ActivityThread.main(ActivityThread.java:3729)
10-22 07:36:18.429: E/AndroidRuntime(27320): at java.lang.reflect.Method.invokeNative(Native Method)
10-22 07:36:18.429: E/AndroidRuntime(27320): at java.lang.reflect.Method.invoke(Method.java:507)
10-22 07:36:18.429: E/AndroidRuntime(27320): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:874)
10-22 07:36:18.429: E/AndroidRuntime(27320): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:632)
10-22 07:36:18.429: E/AndroidRuntime(27320): at dalvik.system.NativeStart.main(Native Method)
10-22 07:36:18.429: E/AndroidRuntime(27320): Caused by: java.lang.NullPointerException
10-22 07:36:18.429: E/AndroidRuntime(27320): at com.dailyreports.ainius.DataProvider.getTotalCallDuration(DataProvider.java:1359)
10-22 07:36:18.429: E/AndroidRuntime(27320): at com.dailyreports.ainius.DataProvider.getReportData(DataProvider.java:109)
10-22 07:36:18.429: E/AndroidRuntime(27320): at com.dailyreports.ainius.ReportView.onCreate(ReportView.java:139)
10-22 07:36:18.429: E/AndroidRuntime(27320): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-22 07:36:18.429: E/AndroidRuntime(27320): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
10-22 07:36:18.429: E/AndroidRuntime(27320): ... 11 more
我确定它很容易,但我就是想不通。