我正在开发一个应用程序,其中部分名称为 代码用于调用日志 ,其中populate需要两个数组并填充SimpleCursorAdapter应用程序崩溃。当我调试应用程序并检查数组是否已填充但停止创建SimpleCursorAdapter时。
代码:
View rootView = inflater.inflate(all_frag, container, false);
lv=(ListView) rootView.findViewById(R.id.list1);
//code for month dropdown list
ArrayList<String> month = new ArrayList<String>();
int thismonth = Calendar.getInstance().get(Calendar.MONTH)+1;
for (int i =thismonth; i >= thismonth-2; i--) {
SimpleDateFormat month_date = new SimpleDateFormat("MMM");
month.add(Integer.toString(i));
//String name=Integer.toString(i);
month.add(month_date.format(i));
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(rootView.getContext(), android.R.layout.simple_spinner_item,month);
Spinner spinmonth =(Spinner) rootView.findViewById(R.id.month);
spinmonth.setAdapter(adapter);
//code for call logs
Cursor managedCursor = getActivity().getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null, null);
if(managedCursor.moveToFirst()) {
int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
String callType = managedCursor.getString(type);
String callDate = managedCursor.getString(date);
Date callDayTime = new Date(Long.valueOf(callDate));
String dir = null;
int dircode = Integer.parseInt(callType);
switch (dircode) {
case CallLog.Calls.OUTGOING_TYPE:
dir = "OUTGOING";
break;
case CallLog.Calls.INCOMING_TYPE:
dir = "INCOMING";
break;
case CallLog.Calls.MISSED_TYPE:
dir = "MISSED";
break;
}
String[] fromColumns = {CallLog.Calls.CACHED_NAME, CallLog.Calls.NUMBER, CallLog.Calls.DURATION,dir,callDayTime.toString()};
int[] toviews = {R.id.name, R.id.number, R.id.duration,R.id.type,R.id.caltime};
SimpleCursorAdapter adapter1 = new SimpleCursorAdapter(rootView.getContext(), R.layout.all_list, managedCursor, fromColumns, toviews, 0);
lv.setAdapter(adapter1);
}
return rootView;
}
all_list.XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/number"
android:textColor="#ffffffff"
android:layout_above="@+id/type"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/duration"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textColor="#ffffffff" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/caltime"
android:layout_below="@+id/duration"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textColor="#ffffffff" />
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="@android:color/darker_gray"
android:id="@+id/view"
android:layout_below="@+id/type"
android:layout_alignRight="@+id/caltime"
android:layout_alignEnd="@+id/caltime" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/type"
android:layout_below="@+id/caltime"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="#ffffffff" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/name"
android:layout_above="@+id/number"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="#ffffffff" />
</RelativeLayout>
05-14 15:32:37.894 32683-32683/com.nm.uzage E/dalvikvm? >>>>> Normal User
05-14 15:32:37.894 32683-32683/com.nm.uzage E/dalvikvm? >>>>> com.nm.uzage [ userId:0 | appId:10326 ]
05-14 15:32:40.096 32683-32683/com.nm.uzage E/AndroidRuntime? FATAL EXCEPTION: main
Process: com.nm.uzage, PID: 32683
java.lang.ArrayIndexOutOfBoundsException: length=3; index=3
at android.widget.SimpleCursorAdapter.bindView(SimpleCursorAdapter.java:150)
at android.widget.CursorAdapter.getView(CursorAdapter.java:254)
at android.widget.AbsListView.obtainView(AbsListView.java:2780)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1274)
at android.widget.ListView.onMeasure(ListView.java:1186)
at android.view.View.measure(View.java:17594)
at android.widget.RelativeLayout.measureChild(RelativeLayout.java:689)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:473)
at android.view.View.measure(View.java:17594)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1456)
at android.view.View.measure(View.java:17594)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5398)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:17594)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5398)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:17594)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
at android.view.View.measure(View.java:17594)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5398)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:17594)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5398)
at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:453)
at android.view.View.measure(View.java:17594)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5398)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:17594)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5398)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:17594)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5398)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2588)
at android.view.View.measure(View.java:17594)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2308)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1409)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1610)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1267)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6638)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:813)
at android.view.Choreographer.doCallbacks(Choreographer.java:613)
at android.view.Choreographer.doFrame(Choreographer.java:583)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:799)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5635)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
05-14 15:33:21.647 2721-2721/com.nm.uzage E/dalvikvm? >>>>> Normal User
05-14 15:33:21.647 2721-2721/com.nm.uzage E/dalvikvm? >>>>> com.nm.uzage [ userId:0 | appId:10326 ]
05-14 15:45:07.806 8615-8615/com.nm.uzage E/dalvikvm? >>>>> Normal User
05-14 15:45:07.806 8615-8615/com.nm.uzage E/dalvikvm? >>>>> com.nm.uzage [ userId:0 | appId:10326 ]
答案 0 :(得分:2)
您的Cursor
没有足够的列数。这似乎是个问题。请看一下断点,查看managedCursor
个实例。
如果你看一下发生崩溃的sourcecode,你会看到:
String text = cursor.getString(from[i]);
这意味着您的光标可能没有足够的列。
编辑:
根本就是我想的其他地方。正如Anti正确指出您使用的列名不正确。但是,列数组会在SimpleCursorAdapter
构造函数中进行过滤,并且只会获取有效的列名。
因此,不是因为您的Cursor没有足够的列,而是因为您的from
(列)和to
(视图)计数不相等而失败。
答案 1 :(得分:1)
根据您的logcat,您的代码在SimpleCursorAdapter bindView方法()中使用java.lang.ArrayIndexOutOfBoundsException: length=3; index=3
如果你查看source code of that method,那么它将首先获取“to”参数中的元素数量 - 这对应于你的int [] toviews变量 - 它有5个元素
然后它循环遍历游标并尝试通过索引获取5个元素 - 然而这在元素3处失败导致崩溃 - 因为显然光标只有3列。
fromColumns is supposed to contain column names,你试图传入不是列名的字符串(dir和callDayTime)
解决此问题的一种方法是继承SimpleCursorAdapter并覆盖bindView方法 - 您可以将链接的源作为基础并修改它以按您需要的方式工作。
另一种解决方案是创建自己的适配器。