-1

我想从通话记录中计算给定时间段内每个联系人的总通话时长,但我在 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

我确定它很容易,但我就是想不通。

4

4 回答 4

3
String[] name = new String[c.getCount()]; 

此行将创建一个大小数组,c.getCount()但所有索引都将填充null.

并且您正在使用索引 0,而没有对其进行初始化。因此,NPE。

您可以将if条件更改为以下以避免 NPE,从而在第一次迭代中初始化数组:

if(name[y] == null)

如果你能解释你想要达到的目标会更好。我看到你的逻辑有问题。

于 2012-10-22T05:37:01.053 回答
2

如果 name[y] 为 null,则在其上调用诸如“equals()”之类的方法将引发 NPE。通过查看您的代码,我认为您需要将 if 语句更改为

if(name[y] == null || name[y].equals(""))

正如 Azodious 所提到的,在初始化数组后,所有条目都将为空。

于 2012-10-22T05:36:44.797 回答
0

在实际访问 name[y] 之前,检查 indexy是否在name数组范围内。如果存在,那么您可以检查ifthat indexis中的内容null

if(y<name.length() && name[y]!=null)
 { 
   **access name at y index** and do stuffs
 }
于 2012-10-22T05:35:32.503 回答
0
Cursor c = cr.query(CallLog.Calls.CONTENT_URI, projection, selection, null, null);
if(c.moveToFirst()){
    if(c.getCount() > 0){ // i add this additional condition
        int[] duration = new int[c.getCount()];
        String[] name = new String[c.getCount()];
        boolean w = false;
        int y=0;
        do{
            if(name[y] != null && 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);
    }
}
于 2012-10-22T05:45:37.713 回答