ListFragment与兼容库崩溃

时间:2012-05-22 22:58:01

标签: android android-fragments android-listfragment

我正在使用针对Android 2.2的Android兼容性库。如果我使用ListFragment与数组适配器,它工作正常,但如果我使用simpleCursorAdapter,它会崩溃。这是我的ListFragment代码:

public class Fragment1 extends ListFragment {

    private SQLiteDatabase db;
    private Cursor c;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
      super.onActivityCreated(savedInstanceState);
      DbManager dbm = new DbManager (getActivity(), "mainDb", null, 3);
      db = dbm.getWritableDatabase();
      c = db.rawQuery("SELECT * FROM navegacion", null);
      c.moveToFirst();

      ListAdapter adapter = new SimpleCursorAdapter( getActivity().getApplicationContext(), R.layout.list_item, c, new String[]{"texto"}, new int []{R.id.mainListText}, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
      setListAdapter(adapter);


    }
 }

DbManager是一个扩展SqliteHelper的类。我不明白为什么它不起作用。它曾经与listActivity一起使用。我甚至将标志添加到构造函数中。当我实例化这个片段时,设备会说应用程序已被中断并关闭它。有人知道我做错了吗?

先谢谢你。


编辑。这是来自logcat的堆栈跟踪:

05-22 19:32:17.093: W/dalvikvm(12680): VFY: unable to resolve virtual method 4091: Lcom/actionbarsherlock/internal/nineoldandroids/widget/NineFrameLayout;.onHoverEvent (Landroid/view/MotionEvent;)Z
05-22 19:32:17.103: W/dalvikvm(12680): VFY: unable to resolve virtual method 2878: Landroid/widget/FrameLayout;.getAlpha ()F
05-22 19:32:17.103: W/dalvikvm(12680): VFY: unable to resolve virtual method 2879: Landroid/widget/FrameLayout;.getTranslationY ()F
05-22 19:32:17.103: W/dalvikvm(12680): VFY: unable to resolve virtual method 2882: Landroid/widget/FrameLayout;.setAlpha (F)V
05-22 19:32:17.103: W/dalvikvm(12680): VFY: unable to resolve virtual method 2890: Landroid/widget/FrameLayout;.setTranslationY (F)V
05-22 19:32:17.123: W/dalvikvm(12680): VFY: unable to resolve virtual method 257: Landroid/content/pm/PackageManager;.getActivityLogo (Landroid/content/ComponentName;)Landroid/graphics/drawable/Drawable;
05-22 19:32:17.123: W/dalvikvm(12680): VFY: unable to resolve virtual method 254: Landroid/content/pm/ApplicationInfo;.loadLogo (Landroid/content/pm/PackageManager;)Landroid/graphics/drawable/Drawable;
05-22 19:32:17.173: W/dalvikvm(12680): VFY: unable to resolve virtual method 2660: Landroid/view/ViewGroup;.getAlpha ()F
05-22 19:32:17.173: W/dalvikvm(12680): VFY: unable to resolve virtual method 2666: Landroid/view/ViewGroup;.getTranslationX ()F
05-22 19:32:17.173: W/dalvikvm(12680): VFY: unable to resolve virtual method 2667: Landroid/view/ViewGroup;.getTranslationY ()F
05-22 19:32:17.173: W/dalvikvm(12680): VFY: unable to resolve virtual method 2682: Landroid/view/ViewGroup;.setAlpha (F)V
05-22 19:32:17.173: W/dalvikvm(12680): VFY: unable to resolve virtual method 2686: Landroid/view/ViewGroup;.setTranslationX (F)V
05-22 19:32:17.173: W/dalvikvm(12680): VFY: unable to resolve virtual method 2687: Landroid/view/ViewGroup;.setTranslationY (F)V
05-22 19:32:17.183: W/dalvikvm(12680): VFY: unable to resolve virtual method 5020: Lcom/actionbarsherlock/internal/widget/ActionBarView$HomeView;.onHoverEvent (Landroid/view/MotionEvent;)Z
05-22 19:32:17.183: W/dalvikvm(12680): VFY: unable to resolve virtual method 2881: Landroid/widget/FrameLayout;.onPopulateAccessibilityEvent (Landroid/view/accessibility/AccessibilityEvent;)V
05-22 19:32:17.223: W/dalvikvm(12680): VFY: unable to resolve virtual method 2893: Landroid/widget/HorizontalScrollView;.getAlpha ()F
05-22 19:32:17.223: W/dalvikvm(12680): VFY: unable to resolve virtual method 2894: Landroid/widget/HorizontalScrollView;.setAlpha (F)V
05-22 19:32:17.223: W/dalvikvm(12680): VFY: unable to resolve direct method 2924: Landroid/widget/LinearLayout;.<init> (Landroid/content/Context;Landroid/util/AttributeSet;I)V
05-22 19:32:17.223: W/dalvikvm(12680): VFY: unable to resolve virtual method 2928: Landroid/widget/LinearLayout;.getAlpha ()F
05-22 19:32:17.223: W/dalvikvm(12680): VFY: unable to resolve virtual method 2931: Landroid/widget/LinearLayout;.getTranslationX ()F
05-22 19:32:17.223: W/dalvikvm(12680): VFY: unable to resolve virtual method 2938: Landroid/widget/LinearLayout;.setAlpha (F)V
05-22 19:32:17.223: W/dalvikvm(12680): VFY: unable to resolve virtual method 2944: Landroid/widget/LinearLayout;.setTranslationX (F)V
05-22 19:32:17.243: W/dalvikvm(12680): VFY: unable to resolve static field 2657 (ROOT) in Ljava/util/Locale;
05-22 19:32:17.303: W/dalvikvm(12680): VFY: unable to resolve virtual method 2592: Landroid/view/View;.jumpDrawablesToCurrentState ()V
05-22 19:32:21.583: W/dalvikvm(12680): threadid=1: thread exiting with uncaught exception (group=0x40020ac0)
05-22 19:32:21.593: E/AndroidRuntime(12680): FATAL EXCEPTION: main
05-22 19:32:21.593: E/AndroidRuntime(12680): java.lang.IllegalArgumentException: column '_id' does not exist
05-22 19:32:21.593: E/AndroidRuntime(12680):    at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:314)
05-22 19:32:21.593: E/AndroidRuntime(12680):    at android.support.v4.widget.CursorAdapter.init(CursorAdapter.java:174)
05-22 19:32:21.593: E/AndroidRuntime(12680):    at android.support.v4.widget.CursorAdapter.<init>(CursorAdapter.java:151)
05-22 19:32:21.593: E/AndroidRuntime(12680):    at android.support.v4.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:93)
05-22 19:32:21.593: E/AndroidRuntime(12680):    at android.support.v4.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:89)
05-22 19:32:21.593: E/AndroidRuntime(12680):    at com.kinbit.abTest.Fragment1.onActivityCreated(Fragment1.java:66)
05-22 19:32:21.593: E/AndroidRuntime(12680):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:891)
05-22 19:32:21.593: E/AndroidRuntime(12680):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1080)
05-22 19:32:21.593: E/AndroidRuntime(12680):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:622)
05-22 19:32:21.593: E/AndroidRuntime(12680):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1416)
05-22 19:32:21.593: E/AndroidRuntime(12680):    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:420)
05-22 19:32:21.593: E/AndroidRuntime(12680):    at android.os.Handler.handleCallback(Handler.java:587)
05-22 19:32:21.593: E/AndroidRuntime(12680):    at android.os.Handler.dispatchMessage(Handler.java:92)
05-22 19:32:21.593: E/AndroidRuntime(12680):    at android.os.Looper.loop(Looper.java:123)
05-22 19:32:21.593: E/AndroidRuntime(12680):    at android.app.ActivityThread.main(ActivityThread.java:4627)
05-22 19:32:21.593: E/AndroidRuntime(12680):    at java.lang.reflect.Method.invokeNative(Native Method)
05-22 19:32:21.593: E/AndroidRuntime(12680):    at java.lang.reflect.Method.invoke(Method.java:521)
05-22 19:32:21.593: E/AndroidRuntime(12680):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-22 19:32:21.593: E/AndroidRuntime(12680):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-22 19:32:21.593: E/AndroidRuntime(12680):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:3)

使用ArrayAdapter但不使用SimpleCursorAdapter的原因是因为SimpleCursorAdapter extends CursorAdapterCursorAdapter 无法正常工作不包含名为_id的列。 _id列中的值应该是绑定到ListView的值。

此错误与兼容性库无关。