ClassNotFoundException in android.util.ArrayMap in

2020-07-27 03:36发布

问题:

I was developing an Android app and in one of the sections, I pass data using an Intent from a DatePicker dialog fragment back to the fragment that created it using getTargetFragment().onActivityResult(int targetRequestCode, int resultCode, Intent intent). I am adding three integers (date, month and year) extracted from the Calendar fragment to the intent being passed. And then I read this intent within the onActivityResult() method of the fragment that created it. Everything works as it should and all is fine and dandy and the world is a perfect place to live in.

While I was trying to debug a silly error that I had made, I noticed something interesting. When I expand the content of the intent, I find that my data is in a bundle as expected. But I see that there is another object within the intent called android.util.ArrayMap with size 3.

This size seems to correspond to the number of data that I put in the intent (I tested it by adding additional dummy parameters). when I expand the contents of the ArrayMap, I see that it throws a ClassNotFoundException. Why would it do this. The key type is String and the value type is Integer (I am putting an int but I don't think it matters as I added in String for key and value and it still threw the exception). I expanded the ArrayMap details but it did not seem to make sense.

My questions are:

  • Why is this ClassNotFoundException happening?
  • What is the need for the ArrayMap in the first place?

回答1:

your putExtra values are saved inside Bundle's internal data structure , which happens to be ArrayMap

Here is its definition, declared in Bundle.java source

    // Invariant - exactly one of mMap / mParcelledData will be null
    // (except inside a call to unparcel)

    /* package */ ArrayMap<String, Object> mMap = null;

I guess it converts your int to a more generic Object class.

ClassNotFoundException could be IDE/debug window issue, as there is not any other custom class here for this data structure. Also, you are expected to use package-name prefix in your name, as mentioned here

The name must include a package prefix, for example the app com.android.contacts would use names like "com.android.contacts.ShowAll".