Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/jenkins/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Studio中的switch语句不工作_Android_Variables_Switch Statement - Fatal编程技术网

Android Studio中的switch语句不工作

Android Studio中的switch语句不工作,android,variables,switch-statement,Android,Variables,Switch Statement,我试图简化我的代码,尽可能减少变量,当我的应用程序运行且应用程序关闭时,我在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.su

我试图简化我的代码,尽可能减少变量,当我的应用程序运行且应用程序关闭时,我在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();
    }
}

将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();
    }
}
package com.example.chris.sunil_gupta;
导入java.util.ArrayList;
导入java.util.Date;
导入java.util.List;
导入android.app.Activity;
导入android.content.Context;
导入android.database.Cursor;
导入android.os.Bundle;
导入android.provider.CallLog;
导入android.widget.ListView;
公共类MainActivity扩展了活动{
私有列表=新的ArrayList();
私有上下文=null;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
列表视图列表视图;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
上下文=这个;
listview=(listview)findViewById(R.id.listview\u CallData);
getCallDetails();
CustomAdapter=新的CustomAdapter(MainActivity.this,列表);
setAdapter(适配器);
}
public void getCallDetails()
{
//cursor1获取调用日志中的所有项,并从最新的调用向下排列它们
游标cursor1=getContentResolver().query(
CallLog.Calls.CONTENT_URI,null,null,null,CallLog.Calls.DATE+“DESC”);
//看起来calllog数据库中的所有单元格值都是整数
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);
//在这里声明一些新变量;我们将把整数转换成
字符串电话号码;
字符串callDate;
字符串调用持续时间;
日期调用日期时间;
//遍历数据库中的所有行,并将值转换为字符串或其他内容
while(cursor1.moveToNext())
{
phoneNumber=cursor1.getString(数字);
callDate=cursor1.getString(日期);
callDateTime=新日期(Long.valueOf(callDate));
callDuration=cursor1.getString(持续时间);
//字符串cType将为我们提供传出、传入或丢失的文本
字符串cType=null;
开关(整数.parseInt(类型))
{
案例CallLog.Calls.OUTGOING_类型:
cType=“外出”;
打破
案例CallLog.Calls.INCOMING_类型:
cType=“传入”;
打破
案例CallLog.Calls.MISSED_类型:
cType=“MISSED”;
打破
}
CallData CallData=新的CallData(cType、phoneNumber、callDateTime、callDuration);
list.add(calldata);
}
游标1.close();
}
}

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

这条线在while循环之外。当光标位于无效位置(-1)时,它将尝试读取列的值,因为尚未调用
moveToNext()
。返回到查找列索引和提取while循环中的调用类型

另外,您应该使用
cursor.getInt()

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

谢谢Karakuri,我现在不在Android Studio工作。我们将在稍后查看它,如果它有效,肯定会将其标记为正确。为什么它在第一种情况下有效,而在第二种情况下无效?”int type=cursor1.getColumnIndex(CallLog.Calls.type);'也在while循环之外。@因为在第一种情况下,您只查找列索引,而在第二种情况下,您还试图使用
getString()
从该列检索值。
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();
    }
}
int callType = cursor.getInt(typeIndex);
switch (callType) {
    // etc
}