无法从具有1行1列的CursorWindow读取第0行第1列

时间:2015-08-06 08:54:25

标签: java android android-sqlite

我想在滚动列表View时检索行的id。我收到错误和异常,如 java.lang.IllegalStateException:无法从CursorWindow读取第0行,第1列。确保Cursor在从中访问数据之前已正确初始化。在此行 strDescription = cursor.getString(1);

这是我的活动代码

  class MyListAdapter extends ArrayAdapter<All_Post>
    {
        Context context;
        int layoutResourceId;

        ArrayList<All_Post> data=new ArrayList<All_Post>();

        public MyListAdapter(Context context, int layoutResourceId, ArrayList<All_Post> data) {
            super(context, layoutResourceId, data);
            this.layoutResourceId = layoutResourceId;
            this.context = context;
            this.data = data;

        }

        @Override
        public int getCount()
        {
            return data.size();
        }

        @Override
        public long getItemId(int position)
        {
            return position;
        }


        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View row = convertView;
            Holder holder = null;
            if(row == null)
            {
                LayoutInflater inflater = ((Activity)context).getLayoutInflater();
                row = inflater.inflate(layoutResourceId, parent, false);
                holder = new Holder();
                holder.txt_Descid = (TextView)row.findViewById(R.id.textDescid);
                holder.txtTitle = (TextView)row.findViewById(R.id.txt_Desc);
                holder.txtInitialLetter = (TextView)row.findViewById(R.id.txt_InitialLetter);
                holder.txtRemark = (TextView)row.findViewById(R.id.textRemark);

                holder.imgBtn_Rework = (ImageView)row.findViewById(R.id.imgButtonRework);
                holder.imgBtn_FollowUp = (ImageView)row.findViewById(R.id.imgButtonFoloowUp);
                holder.imgBtn_RateEquipMent = (ImageView)row.findViewById(R.id.imgButtonRateEquipment);
                holder.imgBtn_Camera = (ImageView)row.findViewById(R.id.imgButtonCamera);
                holder.imgBtn_Audio = (ImageView)row.findViewById(R.id.imgButtonAudio);
                holder.imgBtn_FollowInspector = (ImageView)row.findViewById(R.id.imgButtonFollowInspector);

                holder.viewPager = (ViewPager)row.findViewById(R.id.Image_ViewPagers);

                holder.viewPager.setPageMargin(-50);
                holder.viewPager.setHorizontalFadingEdgeEnabled(true);
                holder.viewPager.setFadingEdgeLength(20);
                holder.viewPager.setClipToPadding(false);
                holder.viewPager.setPadding(-5,0,0,0);
                MyPagerAdapter adapter = new MyPagerAdapter(getContext());
                holder.viewPager.setAdapter(adapter);
                holder.viewPager.setCurrentItem(0);


                row.setTag(holder);
            }
            else
            {
                holder = (Holder)row.getTag();
            }

            All_Post all_Post = data.get(position);


            holder.txt_Descid.setText(Integer.toString(all_Post.getID()));
            String strTextDescId = holder.txt_Descid.getText().toString().trim();
            Log.e("strTextDescId "," = "+strTextDescId);
            getActivityObjectId(strTextDescId);

            holder.txtTitle.setText(all_Post.getName());
            String strTxtTitle = holder.txtTitle.getText().toString().trim();
            Log.e("strTxtTitle "," = "+strTxtTitle);


            holder.txtInitialLetter.setText(all_Post.getInitName());
            holder.txtRemark.setText(all_Post.getStrDesc());

            holder.imgBtn_Rework.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    Log.e("Button click ","event is fire");

                }
            });


            return row;
        }
         class Holder
        {
            TextView txt_Descid;
            TextView txtTitle;
            TextView txtInitialLetter;
            TextView txtRemark;
            ViewPager viewPager;

            ImageView imgBtn_Rework;
            ImageView imgBtn_FollowUp;
            ImageView imgBtn_RateEquipMent;
            ImageView imgBtn_Camera;
            ImageView imgBtn_Audio;
            ImageView imgBtn_FollowInspector;

        }
    }
private void getActivityObjectId(String descriptionID)
    {
        try {
            dbhelper = new MyDbHelper(this);
            SQLiteDatabase db=dbhelper.getReadableDatabase();

            Cursor cursor = db.rawQuery("select activityId from ALLPost_Description where " + "descid" + " = ?",
                    new String[]{descriptionID});

            if(cursor != null) {

                cursor.moveToFirst();

                    strDescription = cursor.getString(1);
                    System.out.println(" activityId =" + strDescription);
                    Log.e(" strDescription ", " = " + strDescription);

            }

            cursor.close();
            db.close();

        }

        catch (Exception e)
        {
            // TODO: handle exception
            e.printStackTrace();
        }
    }

这是我的LogCat错误信息

08-06 04:46:25.923    4206-4206/? E/strTxtTitle﹕ = JIBE Admin@AKARI
08-06 04:46:26.003    4206-4206/? E/strTextDescId﹕ = 2
08-06 04:46:26.003    4206-4206/? E/Construct In MyDbHelper﹕ !!!
08-06 04:46:26.043    4206-4206/? E/CursorWindow﹕ Failed to read row 0, column 1 from a CursorWindow which has 1 rows, 1 columns.
08-06 04:46:26.053    4206-4206/? W/System.err﹕ java.lang.IllegalStateException: Couldn't read row 0, col 1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
08-06 04:46:26.073    4206-4206/? W/System.err﹕ at android.database.CursorWindow.nativeGetString(Native Method)
08-06 04:46:26.073    4206-4206/? W/System.err﹕ at android.database.CursorWindow.getString(CursorWindow.java:434)
08-06 04:46:26.073    4206-4206/? W/System.err﹕ at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
08-06 04:46:26.073    4206-4206/? W/System.err﹕ at com.example.tazeen.classnkk.AllPosts_Page.getActivityObjectId(AllPosts_Page.java:627)
08-06 04:46:26.073    4206-4206/? W/System.err﹕ at com.example.tazeen.classnkk.AllPosts_Page.access$100(AllPosts_Page.java:72)
08-06 04:46:26.083    4206-4206/? W/System.err﹕ at com.example.tazeen.classnkk.AllPosts_Page$MyListAdapter.getView(AllPosts_Page.java:483)
08-06 04:46:26.083    4206-4206/? W/System.err﹕ at android.widget.AbsListView.obtainView(AbsListView.java:2177)
08-06 04:46:26.093    4206-4206/? W/System.err﹕ at android.widget.ListView.makeAndAddView(ListView.java:1840)
08-06 04:46:26.093    4206-4206/? W/System.err﹕ at android.widget.ListView.fillDown(ListView.java:675)
08-06 04:46:26.103    4206-4206/? W/System.err﹕ at android.widget.ListView.fillFromTop(ListView.java:736)
08-06 04:46:26.103    4206-4206/? W/System.err﹕ at android.widget.ListView.layoutChildren(ListView.java:1655)
08-06 04:46:26.103    4206-4206/? W/System.err﹕ at android.widget.AbsListView.onLayout(AbsListView.java:2012)
08-06 04:46:26.103    4206-4206/? W/System.err﹕ at android.view.View.layout(View.java:14289)
08-06 04:46:26.123    4206-4206/? W/System.err﹕ at android.view.ViewGroup.layout(ViewGroup.java:4562)
08-06 04:46:26.123    4206-4206/? W/System.err﹕ at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
08-06 04:46:26.123    4206-4206/? W/System.err﹕ at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
08-06 04:46:26.133    4206-4206/? W/System.err﹕ at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
08-06 04:46:26.143    4206-4206/? W/System.err﹕ at android.view.View.layout(View.java:14289)
08-06 04:46:26.143    4206-4206/? W/System.err﹕ at android.view.ViewGroup.layout(ViewGroup.java:4562)
08-06 04:46:26.143    4206-4206/? W/System.err﹕ at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
08-06 04:46:26.153    4206-4206/? W/System.err﹕ at android.view.View.layout(View.java:14289)
08-06 04:46:26.153    4206-4206/? W/System.err﹕ at android.view.ViewGroup.layout(ViewGroup.java:4562)
08-06 04:46:26.153    4206-4206/? W/System.err﹕ at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
08-06 04:46:26.173    4206-4206/? W/System.err﹕ at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
08-06 04:46:26.173    4206-4206/? W/System.err﹕ at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
08-06 04:46:26.173    4206-4206/? W/System.err﹕ at android.view.View.layout(View.java:14289)
08-06 04:46:26.173    4206-4206/? W/System.err﹕ at android.view.ViewGroup.layout(ViewGroup.java:4562)
08-06 04:46:26.173    4206-4206/? W/System.err﹕ at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
08-06 04:46:26.193    4206-4206/? W/System.err﹕ at android.view.View.layout(View.java:14289)
08-06 04:46:26.193    4206-4206/? W/System.err﹕ at android.view.ViewGroup.layout(ViewGroup.java:4562)
08-06 04:46:26.193    4206-4206/? W/System.err﹕ at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1976)
08-06 04:46:26.203    4206-4206/? W/System.err﹕ at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1730)
08-06 04:46:26.203    4206-4206/? W/System.err﹕ at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
08-06 04:46:26.203    4206-4206/? W/System.err﹕ at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
08-06 04:46:26.203    4206-4206/? W/System.err﹕ at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
08-06 04:46:26.223    4206-4206/? W/System.err﹕ at android.view.Choreographer.doCallbacks(Choreographer.java:562)
08-06 04:46:26.223    4206-4206/? W/System.err﹕ at android.view.Choreographer.doFrame(Choreographer.java:532)
08-06 04:46:26.223    4206-4206/? W/System.err﹕ at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
08-06 04:46:26.223    4206-4206/? W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:730)
08-06 04:46:26.223    4206-4206/? W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:92)
08-06 04:46:26.233    4206-4206/? W/System.err﹕ at android.os.Looper.loop(Looper.java:137)
08-06 04:46:26.233    4206-4206/? W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5103)
08-06 04:46:26.243    4206-4206/? W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
08-06 04:46:26.253    4206-4206/? W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:525)
08-06 04:46:26.253    4206-4206/? W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-06 04:46:26.253    4206-4206/? W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-06 04:46:26.253    4206-4206/? W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)

感谢高级

1 个答案:

答案 0 :(得分:3)

尝试改变

 strDescription = cursor.getString(1);

 strDescription = cursor.getString(0);

当您尝试仅检索一个activityId