Android Studio 中的 switch 语句不起作用

switch statement in Android Studio not working

我正在尝试简化我的代码,尽可能减少变量,但当我的应用程序运行并关闭时,我在 logcat 中不断收到此错误:

02-14 00:13:10.279  32478-32478/com.example.chris.sunil_gupta W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x415998e0)
02-14 00:13:10.289  32478-32478/com.example.chris.sunil_gupta E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.chris.sunil_gupta/com.example.chris.sunil_gupta.MainActivity}: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 500

有人能告诉我我的代码有什么问题吗?这段代码工作完美,但我想我可以稍微简化一下:

package com.example.chris.sunil_gupta;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.CallLog;
import android.widget.ListView;

public class MainActivity extends Activity {



    private List <CallData>list = new ArrayList<CallData>();
    private Context context=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        ListView listview;

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        context=this;

        listview=(ListView)findViewById(R.id.ListView_CallData);

        getCallDetails();
        CustomAdapter adapter = new CustomAdapter(MainActivity.this, list);
        listview.setAdapter(adapter);
    }

    public void getCallDetails()
    {


        //        cursor1 gets all the items in the calllog and arranges them from newest call down
        Cursor cursor1 = getContentResolver().query(
                CallLog.Calls.CONTENT_URI, null, null, null, CallLog.Calls.DATE + " DESC");

//looks like all the cell values in the calllog database are integers
        int number = cursor1.getColumnIndex( CallLog.Calls.NUMBER );
        int type = cursor1.getColumnIndex( CallLog.Calls.TYPE );
        int date = cursor1.getColumnIndex( CallLog.Calls.DATE);
        int duration = cursor1.getColumnIndex( CallLog.Calls.DURATION);

//declare some new variables here; we're going to convert the integers into these
        String callType;
        String phoneNumber;
        String callDate;
        String callDuration;
        Date callDateTime;

//go through all the rows in the db and convert the values to strings or whatever
        while (cursor1.moveToNext())
        {

            phoneNumber = cursor1.getString(number);
            callType = cursor1.getString(type);
            callDate = cursor1.getString(date);

            callDateTime = new Date(Long.valueOf(callDate));

            callDuration = cursor1.getString(duration);

//            the string cType will give us text of either outgoing, incoming or missed
            String cType = null;


            int cTypeCode = Integer.parseInt(callType);

            switch(cTypeCode)
            {
                case CallLog.Calls.OUTGOING_TYPE:
                    cType = "OUTGOING";
                    break;

                case CallLog.Calls.INCOMING_TYPE:
                    cType= "INCOMING";
                    break;

                case CallLog.Calls.MISSED_TYPE:
                    cType = "MISSED";
                    break;
            }

            CallData calldata=new CallData(cType, phoneNumber, callDateTime, callDuration);
            list.add(calldata);
        }

        cursor1.close();
    }
}

这段代码给出了错误。基本上我已经改变了:

int type = cursor1.getColumnIndex( CallLog.Calls.TYPE);

String type = cursor1.getString(cursor1.getColumnIndex(CallLog.Calls.TYPE));

将 cTypeCode 变量替换为:

switch (Integer.parseInt(type))

这是给我错误的代码:

package com.example.chris.sunil_gupta;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.CallLog;
import android.widget.ListView;

public class MainActivity extends Activity {



    private List <CallData>list = new ArrayList<CallData>();
    private Context context=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        ListView listview;

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        context=this;

        listview=(ListView)findViewById(R.id.ListView_CallData);

        getCallDetails();
        CustomAdapter adapter = new CustomAdapter(MainActivity.this, list);
        listview.setAdapter(adapter);
    }

    public void getCallDetails()
    {


        //        cursor1 gets all the items in the calllog and arranges them from newest call down
        Cursor cursor1 = getContentResolver().query(
                CallLog.Calls.CONTENT_URI, null, null, null, CallLog.Calls.DATE + " DESC");

//looks like all the cell values in the calllog database are integers
        int number = cursor1.getColumnIndex( CallLog.Calls.NUMBER );
//        int type = cursor1.getColumnIndex( CallLog.Calls.TYPE );
       String type = cursor1.getString(cursor1.getColumnIndex(CallLog.Calls.TYPE));
        int date = cursor1.getColumnIndex( CallLog.Calls.DATE);
        int duration = cursor1.getColumnIndex(CallLog.Calls.DURATION);


//declare some new variables here; we're going to convert the integers into these

        String phoneNumber;
        String callDate;
        String callDuration;
        Date callDateTime;

//go through all the rows in the db and convert the values to strings or whatever
        while (cursor1.moveToNext())
        {

            phoneNumber = cursor1.getString(number);

            callDate = cursor1.getString(date);

            callDateTime = new Date(Long.valueOf(callDate));

            callDuration = cursor1.getString(duration);

//            the string cType will give us text of either outgoing, incoming or missed
            String cType = null;

            switch (Integer.parseInt(type))
            {
                case CallLog.Calls.OUTGOING_TYPE:
                    cType = "OUTGOING";
                    break;

                case CallLog.Calls.INCOMING_TYPE:
                    cType= "INCOMING";
                    break;

                case CallLog.Calls.MISSED_TYPE:
                    cType = "MISSED";
                    break;
            }

            CallData calldata=new CallData(cType, phoneNumber, callDateTime, callDuration);
            list.add(calldata);
        }

        cursor1.close();
    }
}

String type = cursor1.getString(cursor1.getColumnIndex(CallLog.Calls.TYPE));

这一行在您的 while 循环之外。当游标处于无效位置 (-1) 时,它将尝试读取列的值,因为 moveToNext() 尚未被调用。返回查找列索引并在 while 循环中提取调用类型。

此外,您应该改用 cursor.getInt()

int callType = cursor.getInt(typeIndex);
switch (callType) {
    // etc
}