英文:
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.appContext
是AAddressActivity
调用Utils.startMap()
方法的活动,appContext
在companion object
中定义如下:
companion object {
lateinit var appContext: Context
}
在AAddressActivity
的onCreate()
方法中初始化为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<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
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<LatLng>
@RequiresApi(Build.VERSION_CODES.TIRAMISU)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_map)
// Error happens here
latLngList = intent.getParcelableArrayListExtra("key", LatLng::class.java)!!
Log.d("longMap", 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 'boolean java.lang.Class.isInterface()' 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论