Android的`intent.getParcelableArrayListExtra`在ArrayList上引发了空指针异常。

huangapple go评论113阅读模式
英文:

Android intent.getParcelableArrayListExtra on ArrayList<LatLng> raises Nullpointer exception

问题

我正在尝试将一个包含LatLng对象的数组列表传递给另一个活动。该方法定义在一个不是活动的Util类中。但是,接收活动总是引发NullpointerException异常。

class Utils {
    companion object {
        fun startMaps(pairs: MutableList<LatLng>){
            var intent = Intent(AAddressActivity.appContext, MapActivity::class.java)
            Log.d("pair", ArrayList(pairs).toString())
            Log.d("pair", ArrayList(pairs)[0].toString())
            intent.putParcelableArrayListExtra("key", ArrayList(pairs))

            AAddressActivity.appContext.startActivity(intent)
        }
    }
}

Log.d打印了不为空的pairs内容。AAddressActivity.appContextAAddressActivity调用Utils.startMap()方法的活动,appContextcompanion object中定义如下:

companion object {
    lateinit var appContext: Context
}

AAddressActivityonCreate()方法中初始化为appContext=this,以允许外部访问AAddressActivity的上下文。

这是接收活动:

class MapActivity : AppCompatActivity(), OnMapReadyCallback {
    lateinit var latLngList: ArrayList<LatLng>
    @RequiresApi(Build.VERSION_CODES.TIRAMISU)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_map)

        // 错误发生在这里
        latLngList = intent.getParcelableArrayListExtra("key", LatLng::class.java)!!
        Log.d("longMap", latLngList[0].toString())
    }
}

错误是:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.androidapplicationproject/com.example.androidapplicationproject.mapUtil.MapActivity}: 
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.Class.isInterface()' on a null object reference

据我所知,LatLng对象实现了Parcelable,所以我不应该担心这个问题。但是为什么会发生错误呢?我该如何修复它?

我刚发现,如果pairs中只有一个元素,那么MapActivity可以成功接收到这个Parcelable,但如果列表中有两个或更多元素,就会引发异常。

英文:

I'm trying to pass an array list of LatLng objects to another activity. The method is defined in a Util class which is not an activity. But the receiving activity always raises NullpointerException

class Utils {
    companion object {
        fun startMaps(pairs: MutableList&lt;LatLng&gt;){
            var intent = Intent(AAddressActivity.appContext, MapActivity::class.java)
            Log.d(&quot;pair&quot;, ArrayList(pairs).toString())
            Log.d(&quot;pair&quot;, ArrayList(pairs)[0].toString())
            intent.putParcelableArrayListExtra(&quot;key&quot;, ArrayList(pairs))

            AAddressActivity.appContext.startActivity(intent)
        }
}

Log.d prints the content of the pairs which are not empty. /// AAddressActivity.appContext where AAddressActivity is the activity calling the Utils.startMap() method, and appContext is defined as

companion object{
        lateinit  var appContext: Context
    }

and initialized in the onCreate() AAddressActivity as appContext=this to allow external access to the context of AAddressActivity.<br>
This is the receiving activity:

class MapActivity : AppCompatActivity(), OnMapReadyCallback {
    lateinit var latLngList: ArrayList&lt;LatLng&gt;
    @RequiresApi(Build.VERSION_CODES.TIRAMISU)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_map)


        // Error happens here
        latLngList = intent.getParcelableArrayListExtra(&quot;key&quot;, LatLng::class.java)!!
        Log.d(&quot;longMap&quot;, latLngList[0].toString())

    }

Error is

    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.androidapplicationproject/com.example.androidapplicationproject.mapUtil.MapActivity}: 
java.lang.NullPointerException: Attempt to invoke virtual method &#39;boolean java.lang.Class.isInterface()&#39; on a null object reference

As far as I know, LatLng object implements Parcelable so I shouldn't worry about that. But why is the error happening? How do I fix it.<br>
<br>
I just found that if there are only ONE element in the pairs then the MapActivity can successfully receive the parcel, but if there are two or more elements in the list, the exception raises.

答案1

得分: 1

I reproduced the issue and since it didn't seem like there was anything wrong with the code, I googled for "parcelablearraylist bug" and found this Reddit thread that had a comment that linked to this bug report where a Google Engineer acknowledges it's a bug.

So to answer the question:

But why is the error happening?

It's a bug with the new methods that take a Class argument.

How do I fix it?

Stick to the old methods that don't take a Class parameter (like getParcelableArrayListExtra) until the bug is fixed or they release a Compat version of the API that will work correctly.

英文:

I reproduced the issue and since it didn't seem like there was anything wrong with the code, I googled for "parcelablearraylist bug" and found this Reddit thread that had a comment that linked to this bug report where a Google Engineer acknowledges it's a bug.

So to answer the question:

> But why is the error happening?

It's a bug with the new methods that take a Class argument.

> How do I fix it.

Stick to the old methods that don't take a Class parameter (like getParcelableArrayListExtra) until the bug is fixed or they release a Compat version of the API that will work correctly.

huangapple
  • 本文由 发表于 2023年4月7日 04:58:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75953723.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定