我正在使用Android CardsLib生成片段中的卡片列表。我已正确完成列表,这里没问题。
现在我正在按照教程(link to the tutorial)将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);
}
}
但此代码属于MultiChoiceAdapterHelperBase
,它是cardlib代码的一部分。
在堆栈跟踪之后,它会导致这个类(我从教程中复制),MyCardArrayMultiChoiceAdapter
具有此代码(NPE正在调用super 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();
}
}
任何人都可以找出为什么这个零点异常?或者我在卡片列表中实现多重模式我做错了什么?
如果需要在这里添加更多代码,请告诉我,我将编辑此问题。
答案 0 :(得分:0)
发现问题。
我仍然在我的包含列表的片段上使用CardArrayAdapter
。我从CardArrayAdapter
移到了CardArrayMultiChoiceAdapter
,它完美无瑕地工作:)
所以我有类似的东西(在我的片段上):
private CardArrayAdapter cardArrayAdapter; // comment this one
private MyCardArrayMultiChoiceAdapter cardArrayAdapter; // use this one!
在教程中有:
如果您想要一个内置MultiChoiceMode的CardList 您可以使用CardArrayMultiChoiceAdapter。
此类扩展CardArrayAdapter并保留其所有功能。
答案 1 :(得分:0)
除了我已经使用CardArrayMultiChoiceAdapter
之外,我一直得到同样的例外。
事实证明,我使用常规ListView
代替CardListView
导致了我的问题。