我想将字符串数据从活动发送到片段,但它总是会出错。 我检查了下面的链接并尝试了它们,但仍然相同。
send data from activity to fragment android studio Send data from activity to fragment in Android
我的代码如下,myActivity.kt(我没有删除注释行,我也尝试过它们)
img_foto1.setOnClickListener {
val bundle = Bundle()
bundle.putString("imgviewname", "img_foto1")
val fraginfo = adsCameraOrGallery()
fraginfo.arguments = bundle
//val imgviewBundle = Bundle()
//imgviewBundle.putString("coba", "img_foto1")
//imgviewBundle.putSerializable("modelassign", "test")
//val fragmentObj = adsCameraOrGallery()
//fragmentObj.setArguments(imgviewBundle)
//val transactionData: FragmentTransaction = supportFragmentManager.beginTransaction()
//transactionData.add(0, fragmentObj, "viewtaskfragment")
//transactionData.commit()
//val transactionData: FragmentTransaction = supportFragmentManager.beginTransaction()
//transactionData.add(0, fraginfo, "viewtaskfragment")
//transactionData.commit()
var dialog = adsCameraOrGallery()
dialog.show(supportFragmentManager,"Choose photo")
}
片段类如下:
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
var args : Bundle = Bundle()
var dataFromActivity = args.getBundle("imgviewname")
var temp = args.getString("imgviewname")
//comingData = this!!.arguments!!.getString("imgviewname")
}
因此从片段来看,以上两行的数据均为空。没有来自活动的数据。
如果我在片段中使用下面的行,则这次我收到“ KotlinNullPointerException”错误。
ComingDataFromActivity = this!!.arguments!!.getString("imgviewname")
我该怎么做? 谢谢。
关于@Mike的建议,这些行情对我有用,更新了帖子。
活动;
img_foto1.setOnClickListener {
val bundle = Bundle()
bundle.putString("imgviewname", "img_foto1")
val fraginfo = adsCameraOrGallery()
fraginfo.arguments = bundle
var dialog = adsCameraOrGallery()
dialog.arguments =bundle
dialog.show(supportFragmentManager,"Choose foto")
}
片段;
var comingData = this!!.arguments!!.getString("imgviewname")
答案 0 :(得分:0)
Bundle bundle = new Bundle();
bundle.putString("key", "test");
// set Fragmentclass Arguments
Fragmentclass fragobj = new Fragmentclass();
fragobj.setArguments(bundle);
以及在片段的onCreateView方法中:
String strtext = getArguments().getString("key");
答案 1 :(得分:0)
尝试使用界面访问数据。 在课堂上您想使用数据的地方
area
class A implement PassDataListener{
String str=null;
//PASS CONTEXT TO OF LISTENER TO CLASS WHERE YOU WANT TO GET DATA
B b=new B(A.this);
@override
public void getString(String text){
this.str=text;
}
}
interface PassDataListener{
void getString(String a);
}
答案 2 :(得分:0)
在您的活动中:-
Intent intent = new Intent(getContext(), SearchContact.class);
Bundle a = new Bundle();
a.putString("key", "text");
intent.putExtra("model1", a);
startActivity(intent);
在您的片段中:-
Intent intent = getIntent();
Bundle bundle = intent.getString("model1");
尝试一下可能会对您有所帮助。
答案 3 :(得分:0)
在您的活动中,
Bundle bundle = new Bundle();
String text = "Here is my text";
bundle.putString("text", text );
FragmentClass fragment = new FragmentClass();
fragment.setArguments(bundle);
transaction.replace(R.id.fragment, fragment);
transaction.commit();
在您的片段中
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
String myText = this.getArguments().getString("text");
}