0

我正在使用 Android CardsLib在我的片段中生成卡片列表。我已经正确完成了列表,这里没有问题。

现在我正在按照教程(链接到教程)将 MultiChoiceMode 添加到我的列表中,这是使用长按/单击来选择多张卡片。但我得到一个NullPointerException.

这是我的堆栈跟踪:

FATAL EXCEPTION: main
Process: com.mypackage.android.design.appdesgin, PID: 18937
java.lang.NullPointerException
    at it.gmariotti.cardslib.library.internal.multichoice.MultiChoiceAdapterHelperBase.onItemSelectedStateChanged(MultiChoiceAdapterHelperBase.java:293)
    at it.gmariotti.cardslib.library.internal.multichoice.MultiChoiceAdapterHelperBase.onCreateActionMode(MultiChoiceAdapterHelperBase.java:255)
    at it.gmariotti.cardslib.library.internal.CardArrayMultiChoiceAdapter.onCreateActionMode(CardArrayMultiChoiceAdapter.java:131)
    at com.mypackage.android.design.appdesgin.cards.MyCardArrayMultiChoiceAdapter.onCreateActionMode(MyCardArrayMultiChoiceAdapter.java:31)
    at com.android.internal.policy.impl.PhoneWindow$DecorView$ActionModeCallbackWrapper.onCreateActionMode(PhoneWindow.java:3011)
    at com.android.internal.app.ActionBarImpl$ActionModeImpl.dispatchOnCreate(ActionBarImpl.java:909)
    at com.android.internal.app.ActionBarImpl.startActionMode(ActionBarImpl.java:453)
    at android.app.Activity.onWindowStartingActionMode(Activity.java:5005)
    at com.android.internal.policy.impl.PhoneWindow$DecorView.startActionMode(PhoneWindow.java:2636)
    at android.app.Activity.startActionMode(Activity.java:4987)
    at it.gmariotti.cardslib.library.internal.multichoice.MultiChoiceAdapterHelperBase.startActionMode(MultiChoiceAdapterHelperBase.java:239)
    at it.gmariotti.cardslib.library.internal.CardArrayMultiChoiceAdapter.startActionMode(CardArrayMultiChoiceAdapter.java:117)
    at com.mypackage.android.design.appdesgin.fragments.CardLocalBackupsFragment$1.onLongClick(CardLocalBackupsFragment.java:152)
    at it.gmariotti.cardslib.library.view.CardView$5.onLongClick(CardView.java:533)
    at android.view.View.performLongClick(View.java:4481)
    at android.view.View$CheckForLongPress.run(View.java:18425)
    at android.os.Handler.handleCallback(Handler.java:733)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5081)
    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:791)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
    at dalvik.system.NativeStart.main(Native Method)

空指针在这里:

protected void onItemSelectedStateChanged(ActionMode mode) {
    int count = mAdapterView.getCheckedItemCount(); // mAdapterView is NULL

    if (count > 0) {
        Resources res = mAdapterView.getResources();
        String mTitleSelected = res.getQuantityString(R.plurals.card_selected_items, count, count);
        mode.setTitle(mTitleSelected);
    }
}

但是这段代码属于MultiChoiceAdapterHelperBasecardlib代码的一部分。

在堆栈跟踪之后,它会导致这个类(我从教程中复制),MyCardArrayMultiChoiceAdapter它有这个代码(NPE 正在调用 super on onCreateActionMode

public class MyCardArrayMultiChoiceAdapter extends CardArrayMultiChoiceAdapter {

    private ActionMode mActionMode;

    public MyCardArrayMultiChoiceAdapter(Context context, List<Card> cards) {
        super(context, cards);
    }

    @Override
    public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        //It is very important to call the super method
        super.onCreateActionMode(mode, menu); // This is line 31 where it says in my stacktrace

        mActionMode = mode; // to manage mode in your Fragment/Activity

        //If you would like to inflate your menu
        MenuInflater inflater = mode.getMenuInflater();
        inflater.inflate(R.menu.carddemo_multichoice, menu);
        return true;
    }

    @Override
    public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
        return false;
    }

    @Override
    public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
//        if (item.getItemId() == R.id.menu_share) {
//            Toast.makeText(getContext(), "Share;" + formatCheckedCard(), Toast.LENGTH_SHORT).show();
//            return true;
//        }
        if (item.getItemId() == R.id.menu_discard) {
            discardSelectedItems(mode);
            return true;
        }
        return false;
    }

    @Override
    public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked, CardView cardView, Card card) {
        Toast.makeText(getContext(), "Click;" + position + " - " + checked, Toast.LENGTH_SHORT).show();
    }

    private void discardSelectedItems(ActionMode mode) {
        ArrayList<Card> items = getSelectedCards();
        for (Card item : items) {
            remove(item);
        }
        mode.finish();
    }


    private String formatCheckedCard() {

        SparseBooleanArray checked = mCardListView.getCheckedItemPositions();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < checked.size(); i++) {
            if (checked.valueAt(i) == true) {
                sb.append("\nPosition=" + checked.keyAt(i));
            }
        }
        return sb.toString();
    }
}

谁能弄清楚为什么这个空点异常?或者我在卡片列表中实现多选模式做错了什么?

如果需要在此处放置更多代码,请告诉我,我将编辑此问题。

4

2 回答 2

0

除了我已经在使用CardArrayMultiChoiceAdapter.

事实证明,在我的情况下,我使用的是常规ListView而不是 aCardListView是导致问题的原因。

于 2014-07-23T18:40:56.850 回答
0

发现了问题。

我仍在使用CardArrayAdapter包含列表的片段。我从CardArrayAdapterto移动到CardArrayMultiChoiceAdapter它工作完美无瑕:)

所以我有类似的东西(在我的片段上):

private CardArrayAdapter cardArrayAdapter; // comment this one
private MyCardArrayMultiChoiceAdapter cardArrayAdapter; // use this one!

教程中有:

如果您想要一个具有 MultiChoiceMode 内置功能的 CardList,您可以使用 CardArrayMultiChoiceAdapter。

此类扩展 CardArrayAdapter 并保留其所有功能。

于 2014-06-30T15:20:34.700 回答