Android 为什么bundle.getString()对于另一个片段接收的数据会变为null?
总之,我有两个片段。一个是ViewPlacesFragment,另一个是MenuFragment。 我也有一个主要的活动课。内部视图放置碎片有回收服务适配器。 在一个RecycleServiceAdapter类中,我为单击RecycleView项时实现了一个回调,然后它将单击的项Id发送到MainActivity。然后主活动调用MenuFragment来显示它,并将该Id传递给该片段。 除了在MenuFragment中未接收到Id值之外,其他一切都正常工作。我可以在Log.ddebug4、Id中打印正确的Id值,我检查了代码,但没有任何错误。 那么为什么在MenuFragment中接收到的Id值为null呢 回收商地点:Android 为什么bundle.getString()对于另一个片段接收的数据会变为null?,android,kotlin,Android,Kotlin,总之,我有两个片段。一个是ViewPlacesFragment,另一个是MenuFragment。 我也有一个主要的活动课。内部视图放置碎片有回收服务适配器。 在一个RecycleServiceAdapter类中,我为单击RecycleView项时实现了一个回调,然后它将单击的项Id发送到MainActivity。然后主活动调用MenuFragment来显示它,并将该Id传递给该片段。 除了在MenuFragment中未接收到Id值之外,其他一切都正常工作。我可以在Log.ddebug4、Id中
Class RecyclerAdapterAllPlaces(var myReceivedData: PlaceModel?=null, val context:Context,val cellClickCallback: (id:String) -> Unit):RecyclerView.Adapter<RecyclerAdapterAllPlaces.Viewholder>(){
...
override fun onBindViewHolder(holder: Viewholder, position: Int) {
holder.itemView.setOnClickListener {cellClickCallback(myReceivedData!![position]._id)}
}
}
}
菜单碎片:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val bundle = Bundle()
val data = bundle.getString("Place_Id")
Log.d("debug", "it is $data")
if (data != null)
{
P_Id = data
Log.d("debug",P_Id)
}
}
编辑:
我编辑了代码并删除了新的bundle,但返回null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val bundle = this.arguments
if (bundle != null)
{
val data = bundle.getString("Place_Id")
P_Id = data!!
Log.d("debug",P_Id)
}}
您正在创建一个新的捆绑包,而不是使用提供的捆绑包
//val bundle = Bundle() remove this line, that is instantiating a new bundle
val data = bundle?.getString("Place_Id")
你可以这样做,也可以这样做
val data = arguments?.getString("Place_Id")
变量参数是获取额外包的Kotlin方法。是可空的,因此必须使用可空运算符?。实际上,您可以在任何方法上使用参数,例如,如果您在onViewCreated或onCreateView上执行某些操作,那么如果不需要onCreate,您不需要重写它来获取捆绑包。您正在创建一个新捆绑包,而不是使用提供的捆绑包
//val bundle = Bundle() remove this line, that is instantiating a new bundle
val data = bundle?.getString("Place_Id")
你可以这样做,也可以这样做
val data = arguments?.getString("Place_Id")
变量参数是获取额外包的Kotlin方法。是可空的,因此必须使用可空运算符?。您实际上可以在任何方法上使用参数,例如,如果您在onViewCreated或onCreateView上执行某些操作,这样,如果不需要onCreate,您就不需要重写它来获取捆绑包。在callbackInsideFragment方法中,您创建的是目标片段,但在fragmentTransaction中。替换则传递一个新片段实例。在callbackInsideFragment方法中,您创建的是目标片段,但在fragmentTransaction.replace您将传递一个新的片段实例。是正确的,但我想我会详细说明一下。让我们来看看你所发布的代码,稍微清晰地重新排列:
private fun callbackInsideFragment(Id:String) {
val args = Bundle()
args.putString("Place_Id", Id)
val destinationFragment= MenuFragment()
destinationFragment.arguments = args
val fragmentTransaction = getSupportFragmentManager().beginTransaction()
fragmentTransaction.replace(R.id.frame_container, MenuFragment())
fragmentTransaction.commit()
}
在事务中,您正在创建MenuFragment的第二个实例,并将其替换为该实例,而不是正确设置参数的destinationFragment。这意味着args和destinationFragment从未真正使用过,因此您基本上只有以下内容:
private fun callbackInsideFragment(Id:String) {
val fragmentTransaction = getSupportFragmentManager().beginTransaction()
fragmentTransaction.replace(R.id.frame_container, MenuFragment())
fragmentTransaction.commit()
}
只需将replace调用更改为使用destinationFragment,一切都应该正常:
private fun callbackInsideFragment(Id:String) {
val args = Bundle()
args.putString("Place_Id", Id)
val destinationFragment= MenuFragment()
destinationFragment.arguments = args
val fragmentTransaction = getSupportFragmentManager().beginTransaction()
fragmentTransaction.replace(R.id.frame_container, destinationFragment)
fragmentTransaction.commit()
}
是正确的,但我想我应该详细说明一下。让我们来看看你所发布的代码,稍微清晰地重新排列:
private fun callbackInsideFragment(Id:String) {
val args = Bundle()
args.putString("Place_Id", Id)
val destinationFragment= MenuFragment()
destinationFragment.arguments = args
val fragmentTransaction = getSupportFragmentManager().beginTransaction()
fragmentTransaction.replace(R.id.frame_container, MenuFragment())
fragmentTransaction.commit()
}
在事务中,您正在创建MenuFragment的第二个实例,并将其替换为该实例,而不是正确设置参数的destinationFragment。这意味着args和destinationFragment从未真正使用过,因此您基本上只有以下内容:
private fun callbackInsideFragment(Id:String) {
val fragmentTransaction = getSupportFragmentManager().beginTransaction()
fragmentTransaction.replace(R.id.frame_container, MenuFragment())
fragmentTransaction.commit()
}
只需将replace调用更改为使用destinationFragment,一切都应该正常:
private fun callbackInsideFragment(Id:String) {
val args = Bundle()
args.putString("Place_Id", Id)
val destinationFragment= MenuFragment()
destinationFragment.arguments = args
val fragmentTransaction = getSupportFragmentManager().beginTransaction()
fragmentTransaction.replace(R.id.frame_container, destinationFragment)
fragmentTransaction.commit()
}
尝试在MenuFragment中使用伴生对象来创建如下片段实例
companion object
{
fun newInstance(Id: String) : MenuFragment
{
val args = Bundle()
args.putString("Place_Id", placeId)
val fragment = MenuFragment()
fragment.arguments = args
return fragment
}
}
将您的callbackInsideFragment编辑为
并在onCreate中检索数据
尝试在MenuFragment中使用伴生对象来创建如下片段实例
companion object
{
fun newInstance(Id: String) : MenuFragment
{
val args = Bundle()
args.putString("Place_Id", placeId)
val fragment = MenuFragment()
fragment.arguments = args
return fragment
}
}
将您的callbackInsideFragment编辑为
并在onCreate中检索数据
为什么要实例化一个新包?这是否回答了您的问题@Animeshahu tnx,但我将其更改为上面的代码。仍然返回null。同样,您正在向destinationFragment.arguments=args添加参数,但将新实例化的片段传递给片段Transaction fragmentTransaction.replaceR.id.frame_容器,->MenuFragment@AnimeshSahu谢谢您,伙计。我改变了很多代码。我的错误。又是tnx。你为什么要实例化一个新的包?这回答了你的问题吗@Animeshahu tnx,但我将其更改为上面的代码。仍然返回null。同样,您正在向destinationFragment.arguments=args添加参数,但将新实例化的片段传递给片段Transaction fragmentTransaction.replaceR.id.frame_容器,->MenuFragment@AnimeshSahu谢谢您,伙计。我改变了很多代码。我的错误。tnx,但它双向返回null。我还使用了val bundle=this.arguments if bundle!=null{val data=bundle.getStringPlace_Id},没有任何更改,但它双向返回null。我还使用了val bundle=this.arguments if bundle!=null{val data=bundle.getStringPlace_Id},没什么变化谢谢你,是的,他也是对的,就像@AnimeshSahu。我对代码做了很多修改&从另一个页面复制。这导致了误入歧途。谢谢你,是的,他也是对的,就像@AnimeshSahu。我修改了很多代码,从另一个页面复制。这导致了这种误导。