我正在尝试将一个拖放功能实现到由SQL填充的RecylcerView。我目前正在尝试通过调用以下方法来交换2x列表元素的位置:
我正在调用日志并确认fromPosition和toPosition变量是正确的;所以 为什么我在LogCat中收到null对象引用的错误,如何解决它不为null?
public boolean onItemMove(int fromPosition, int toPosition) {
Log.d(TAG, fromPosition + "");
Log.d(TAG, toPosition + "");
dbHelper.updateOrder(fromPosition, toPosition);
dbHelper.updateOrder(toPosition, fromPosition);
notifyItemMoved(fromPosition, toPosition);
return true;
}
public void updateOrder(int fromPosition, int toPosition) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(SORT_ID, toPosition);
db.update(TABLE_USERdETAIL, cv, "_id="+fromPosition, null);
}
Process: mackinnoninc.packmule, PID: 17423
java.lang.NullPointerException: Attempt to invoke virtual method 'void mackinnoninc.packmule.DbHelper.updateOrder(long, int)' on a null object reference
at mackinnoninc.packmule.ListAdapter.onItemMove(ListAdapter.java:102)
at mackinnoninc.packmule.SimpleItemTouchHelperCallback.onMove(SimpleItemTouchHelperCallback.java:34)
at android.support.v7.widget.helper.ItemTouchHelper.moveIfNecessary(ItemTouchHelper.java:852)
at android.support.v7.widget.helper.ItemTouchHelper.access$100(ItemTouchHelper.java:76)
at android.support.v7.widget.helper.ItemTouchHelper$2.onTouchEvent(ItemTouchHelper.java:376)
at android.support.v7.widget.RecyclerView.dispatchOnItemTouch(RecyclerView.java:2464)
at android.support.v7.widget.RecyclerView.onTouchEvent(RecyclerView.java:2607)
at android.view.View.dispatchTouchEvent(View.java:9303)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2549)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2240)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2555)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2555)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2555)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2555)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2555)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2555)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2254)
at com.android.internal.policy.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2403)
at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1737)
at android.app.Activity.dispatchTouchEvent(Activity.java:2775)
at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:63)
at com.android.internal.policy.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2364)
at android.view.View.dispatchPointerEvent(View.java:9523)
at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4242)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4108)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3654)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3707)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3673)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3799)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3681)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3856)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3654)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3707)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3673)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3681)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3654)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5969)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5908)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5869)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6072)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:192)
at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:183)
at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.j
答案 0 :(得分:1)
在您的代码中,dbHelper
对象为空。你应该先把它初始化。
答案 1 :(得分:0)
这可能是因为你没有初始化DbHelper。构造函数中的某处用它初始化它
private DbHelper dbHelper;
dbHelper = new DbHelper();
答案 2 :(得分:0)
我无法在您的代码中证明这一点。但是因为ErrorStackTrace我说当你在onItemMove()中调用它时,dbHelper对象是未初始化的。你应该在调用时检查dbHelper是否为null。