switch语句不工作

问题描述:

我想简化我的代码,对变量在可能降低,并且我一直在logcat中收到此错误时,我的应用程序运行,并且应用程序关闭:switch语句不工作

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 
} 
+0

感谢Karakuri,我不与Android工作室的时刻。稍后再看,并且如果它有效,肯定标记它是正确的。为什么它在第一种情况下工作,而不是第二种情况? 'int type = cursor1.getColumnIndex(CallLog.Calls.TYPE);'也在while循环之外。 – CHarris

+1

@ChristopheHarris因为在第一种情况下,您只查找列索引,但在第二种情况下,您还尝试使用'getString()'从该列检索值。 – Karakuri