【问题标题】:Android java.lang.IllegalArgumentException: Duplicate key in ArrayMap: nullAndroid java.lang.IllegalArgumentException:ArrayMap 中的重复键:null
【发布时间】:2016-09-03 11:52:22
【问题描述】:

我收到了这个错误

由 java.lang.IllegalArgumentException 引起:重复键 数组映射:空

不在我当前的设备中,但它来自 crashlytics,但我无法在我自己的设备中重新生成此问题尝试了所有可能的方法,但无法在我的设备中遇到同样的错误,下面是我的代码,非常感谢答案。

public class QuestionSwipeFragment extends DialogFragment {

    private View mRootView;
    private int mPosition;
    private List<ResourceModel> mResourceList;
    private QuestionSwipePagerAdapter mQuestionSwipePagerAdapter;
    private ViewPager mPager;

    private int mSelectedChapterId;
    private int mSelectedSectionId;
    private int mSelectedSubSectionId;
    private boolean mSelectedFavourite;

    private boolean mIsFavourite;
    private boolean mIsPagination;

    private int mCurrentPage;
    private int mTotalPage;

    int mResourcePageNo;
    int mMarkId;
    int mDifficulties;

    private int mFragmentType;

    private int mFragType;
    private PaginationParams mPaginationParams = new PaginationParams();

    private static final int RESOURCE_ALL = -1;

    private boolean onDestroy = false;

    private static final String API_TYPE_PAGINATION = "pagination";

    public QuestionSwipeFragment() {
        // Required empty public constructor
    }

    public static DialogFragment newInstance(List<ResourceModel> mResourceList, int id, int fragType, boolean isFavourited, boolean isPaginationRequest,
                                             int chapterId, int sectionId,
                                             int resourcePageNo, int markId,
                                             int difficulties, int currentPage,
                                             int totalPage) {
        Bundle bundle = new Bundle();
        QuestionSwipeFragment fragment = new QuestionSwipeFragment();

        bundle.putParcelableArrayList(Constants.QUESTION_SWIPE_RESOURCE_LIST, (ArrayList<? extends Parcelable>) mResourceList);
        bundle.putInt(Constants.QUESTION_SWIPE_RESOURCE_LIST_POSTION, id);
        bundle.putInt(Constants.QUESTION_SWIPE_TYPE_FRAGMENT, fragType);

        bundle.putBoolean(Constants.QUESTION_SWIPE_ISFAVOURITED, isFavourited);
        bundle.putBoolean(Constants.QUESTION_SWIPE_ISPAGINATIONREQUEST, isPaginationRequest);

        bundle.putInt(Constants.QUESTION_SWIPE_SELECTED_CHAPTER_ID, chapterId);
        bundle.putInt(Constants.QUESTION_SWIPE_SELECTED_SECTION_ID, sectionId);

        bundle.putInt(Constants.QUESTION_SWIPE_RESOURCEPAGE_NO, resourcePageNo);
        bundle.putInt(Constants.QUESTION_SWIPE_MARKID, markId);
        bundle.putInt(Constants.QUESTION_SWIPE_DIFFICULTIES, difficulties);


        bundle.putInt(Constants.QUESTION_SWIPE_CURRENTPAGE, currentPage);
        bundle.putInt(Constants.QUESTION_SWIPE_TOTALPAGE, totalPage);

        fragment.setArguments(bundle);
        return fragment;
    }

    public static DialogFragment newInstance(List<ResourceModel> mResourceList, int position, int fragType,
                                             int selectedChapterId, int selectedSectionId, int selectedSubSectionId, int currentPage, int totalPage) {
        Bundle bundle = new Bundle();
        QuestionSwipeFragment fragment = new QuestionSwipeFragment();
        bundle.putParcelableArrayList(Constants.QUESTION_SWIPE_RESOURCE_LIST, (ArrayList<? extends Parcelable>) mResourceList);
        bundle.putInt(Constants.QUESTION_SWIPE_RESOURCE_LIST_POSTION, position);
        bundle.putInt(Constants.QUESTION_SWIPE_TYPE_FRAGMENT, fragType);

        bundle.putInt(Constants.QUESTION_SWIPE_SELECTED_CHAPTER_ID, selectedChapterId);
        bundle.putInt(Constants.QUESTION_SWIPE_SELECTED_SECTION_ID, selectedSectionId);
        bundle.putInt(Constants.QUESTION_SWIPE_SELECTED_SUBSECTION_ID, selectedSubSectionId);

        bundle.putInt(Constants.QUESTION_SWIPE_CURRENTPAGE, currentPage);
        bundle.putInt(Constants.QUESTION_SWIPE_TOTALPAGE, totalPage);

        bundle.putInt(Constants.QUESTION_SWIPE_CURRENTFRAGMENT, fragType);

        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        handleBundle(getArguments());
        setStyle(DialogFragment.STYLE_NORMAL, R.style.MyMaterialTheme);
    }

//the below method is causing IllegalArgumentException
 private void handleBundle(Bundle bundle) {
          if (bundle != null) {
                if (bundle.containsKey(Constants.QUESTION_SWIPE_RESOURCE_LIST)) {
                    mResourceList = bundle.getParcelableArrayList(Constants.QUESTION_SWIPE_RESOURCE_LIST);
                    mPosition = bundle.getInt(Constants.QUESTION_SWIPE_RESOURCE_LIST_POSTION);
                    mFragType = bundle.getInt(Constants.QUESTION_SWIPE_TYPE_FRAGMENT);

                    mSelectedChapterId = bundle.getInt(Constants.QUESTION_SWIPE_SELECTED_CHAPTER_ID);
                    mSelectedSectionId = bundle.getInt(Constants.QUESTION_SWIPE_SELECTED_SECTION_ID);

                    mSelectedSubSectionId = bundle.getInt(Constants.QUESTION_SWIPE_SELECTED_SUBSECTION_ID);

                    mIsFavourite = bundle.getBoolean(Constants.QUESTION_SWIPE_ISFAVOURITED);
                    mIsPagination = bundle.getBoolean(Constants.QUESTION_SWIPE_ISPAGINATIONREQUEST);

                    mCurrentPage = bundle.getInt(Constants.QUESTION_SWIPE_CURRENTPAGE);
                    mTotalPage = bundle.getInt(Constants.QUESTION_SWIPE_TOTALPAGE);

                    mResourcePageNo = bundle.getInt(Constants.QUESTION_SWIPE_RESOURCEPAGE_NO);
                    mMarkId = bundle.getInt(Constants.QUESTION_SWIPE_MARKID);
                    mDifficulties = bundle.getInt(Constants.QUESTION_SWIPE_DIFFICULTIES);

                    mFragmentType = bundle.getInt(Constants.QUESTION_SWIPE_CURRENTFRAGMENT);
                }
            }
        }

下面是我从适配器开始这个片段的代码

private void setonClickListeners(ResourceQuestionsViewHolder viewHolder, final int position) {
        viewHolder.mContainerView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                DialogFragment questionSwipeFragment = QuestionSwipeFragment.newInstance(getExtractResourceInfo(position).getExtractedList(), getExtractResourceInfo(position).getPosition(), Constants.QB_DETAILS_FRAGMENT, isFavourited, isPaginationRequest, chapterId, sectionId, resourcePageNo, markId, difficulties, mCurrentPage, mTotalPage);
                questionSwipeFragment.show(((QuestionBankActivity) mContext).getSupportFragmentManager(), "dialog");

            }
        });
}

private void setonLongClickListener(SubSectionResourceQuestionsViewHolder viewHolder, final int position) {
        viewHolder.mContainerView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {

                DialogFragment questionBankFragment = QBSubSectionDetailFragment.newInstance(mResourceList, false, position);
                questionBankFragment.show(((SynopsisActivity) mContext).getSupportFragmentManager(), "dialog");

                return true;
            }
        });
    }

【问题讨论】:

  • 它是否真的给了你抛出异常的行号?
  • 是的,它说 197 它是句柄捆绑方法的第一行,我在其中检查捆绑 null,由 java.lang.IllegalArgumentException 引起:ArrayMap 中的重复键:android.util.ArrayMap.validate( ArrayMap.java:550) 在 android.os.Parcel.readArrayMapInternal(Parcel.java:2486) 在 android.os.BaseBundle.unparcel(BaseBundle.java:221) 在 android.os.BaseBundle.containsKey(BaseBundle.java:269 ) 在 com.example.fragments.QuestionSwipeFragment.handleBundle(SourceFile:197)
  • 你能把你的 ResourceModel 类代码粘贴到这里吗?
  • 这个问题解决了吗?我遇到了同样的错误。
  • 这个问题解决了吗,在 crashlytics 中也面临同样的问题?

标签: java android bundle parcelable


【解决方案1】:

当我遇到这个问题时,它实际上是由其他异常引起的

我的问题是由于启动了一个未导出的 Activity

解决方案:

  1. 检查您传递的参数是否有多个 null
  2. 检查是否启动了未设置导出的Activity(用于启动其他应用)
  3. 您可以直接设置对片段的引用,而无需通过 bundle

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-21
    • 2019-01-05
    • 2020-05-11
    • 2018-07-29
    • 2011-04-03
    相关资源
    最近更新 更多