2014-10-01 60 views
-2

獲取遊標error.i'm新手到android所以需要一個help.cursor顯示索引5請求的CursorIndexOutOfBoundsException的運行時異常,大小爲5. 在這個下面的程序中,我已經實現了AndroidSlidingUpPanel-主。遊標值顯示正確,但當我嘗試向上滾動滑塊然後拋出遊標錯誤。在android的運行時光標錯誤

Context mContext; 
    Cursor mCursor; 
    boolean movetonext = false; 
    private LayoutInflater inflater; 

    DownloadedFileListAdapter(Context m, Cursor c) { 
     mContext = m; 
     mCursor = c; 
    } 

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

    @Override 
    public Object getItem(int position) { 
     return mCursor.moveToNext(); 
    } 

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

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     if (inflater == null) 
      inflater = (LayoutInflater) mContext 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     if (convertView == null) 
      convertView = inflater.inflate(R.layout.display_file_list, null); 

     TextView fileName = (TextView) convertView.findViewById(R.id.downloaded_file_name); 
     TextView fileCreatedOn = (TextView) convertView.findViewById(R.id.file_path_created_on); 

     fileName.setText("" + mCursor.getString(0)); 
     fileCreatedOn.setText("" + mCursor.getString(2)); 

     getItem(position); 

     return convertView; 
    } 

P.S:

10-01 12:17:26.268: E/AndroidRuntime(22538): android.database.CursorIndexOutOfBoundsException: Index 5 requested, with a size of 5 
10-01 12:17:26.268: E/AndroidRuntime(22538): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:426) 
10-01 12:17:26.268: E/AndroidRuntime(22538): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136) 
10-01 12:17:26.268: E/AndroidRuntime(22538): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50) 
10-01 12:17:26.268: E/AndroidRuntime(22538): at com.example.socialapp.DownloadedFileListAdapter.getView(DownloadedFileListAdapter.java:56) 
+0

在你的'getItemId'方法除了返回0,返回'position'。 – GrIsHu 2014-10-01 07:00:44

+0

你是否知道[Simple] CursorAdapter? – pskink 2014-10-01 07:07:19

+0

@GrlsHu它仍然顯示相同的問題 – user3721186 2014-10-01 07:27:40

回答

0

試試這個

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

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    if (inflater == null) { 
     inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    }else{ 
     if (convertView == null){ 
      convertView = inflater.inflate(R.layout.display_file_list, null); 
     }else{ 

     TextView fileName = (TextView) convertView.findViewById(R.id.downloaded_file_name); 
     TextView fileCreatedOn = (TextView) convertView.findViewById(R.id.file_path_created_on); 

     fileName.setText("" + mCursor.getString(0)); 
     fileCreatedOn.setText("" + mCursor.getString(2)); 

     getItem(position); 
     } 
    } 

    return convertView; 
} 
+0

我的應用程序崩潰,一旦我放置上述代碼 – user3721186 2014-10-01 07:29:09

+0

你能分享日誌嗎? – 2014-10-01 07:39:41

+0

10-01 13:12:22.907 E/AndroidRuntime(31951):致命例外:主 10-01 13:12:22.907:E/AndroidRuntime(31951):進程:com.sothree.slidinguppanel.demo,PID: 31951 10-01 13:12:22.907:E/AndroidRuntime(31951):java.lang.NullPointerException 10-01 13:12:22.907:E/AndroidRuntime(31951):\t at android.widget.AbsListView.obtainView在Android.widget.ListView.makeAndAddView(ListView.java:1790)012-10-01 13:12:22.907:E/AndroidRuntime(31951):\t AbsListView.java:2277) 10-01 13:12:22.907/AndroidRuntime(31951):\t at com.sothree.slidinguppanel.SlidingUpPanelLayout.onLayout(SlidingUpPanelLayout.java:698) – user3721186 2014-10-01 07:43:33