Android 如何将字符串数据从活动发送到片段

Android 如何将字符串数据从活动发送到片段,android,kotlin,Android,Kotlin,我想将字符串数据从活动发送到片段,但它总是给出错误。 我检查了下面的链接并尝试了它们,但仍然是一样的 我的代码如下所示,myActivity.kt(我没有删除注释行,我也尝试了它们) 片段类如下所示 override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { var args : Bundle = Bundle()

我想将字符串数据从活动发送到片段,但它总是给出错误。 我检查了下面的链接并尝试了它们,但仍然是一样的

我的代码如下所示,myActivity.kt(我没有删除注释行,我也尝试了它们)

片段类如下所示

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”错误

我该怎么做? 谢谢


关于@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")
在Fragment onCreateView方法中:

String strtext = getArguments().getString("key");    

尝试使用接口访问数据。 在要使用数据的类中

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;
}
}
在您的活动中:-

 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");
试试这个可能会对你有帮助。

在你的活动中

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");

 }

不幸的是,没有工作,在“String strtext=getArguments().getString(“key”)”行抛出“KotlinNullPointerException”错误。请尝试使用此参数?.let{String strtext=it.getString(“key”)}无法将此代码转换为Kotlin,Android Studio也不能。我不明白我应该在哪里添加这个参数?在你的片段中,我把这个代码放在onCreateView下。让{String strtext=it.getString(“key”,下称“)}”,但它给出了错误。“需要一个元素”。对不起,我是Android编程的初学者。我已经在使用一个接口将数据从片段发送到活动。对于另一面,我应该创建另一个接口吗?我会尝试,但捆绑不是一种简单的方法吗?使用dialog.show它将在弹出窗口中显示,其中as intent将数据传递给另一个活动/片段,它本身也意味着活动到片段,反之亦然。对话和意图之间没有联系。我认为使用对话意图将有助于您实现目标。感谢您的建议,我也将尝试使用意图方法。但是如果我使用intent,我不能像活动中的窗口一样弹出片段,对吗?我的意思是,如果我使用intent,它将是一个全新的全屏片段,不像dialog?我已经在我的“img_foto1.setOnClickListener”中使用了“dialog.show(supportFragmentManager,“Choose photo”)”,所以我不确定是否可以使用其他intent方法?它抛出了一些ActivityNotFoundException错误。我试图将activity代码转换为Kotlin,但Android Studio无法转换“transaction”类。它抛出“未解析引用”错误。@sleepy reference dis``在您的第一个代码段中,您显示的
对话框
不是您之前设置参数的对话框,
fraginfo
show()
fraginfo
,或者将其更改为在
对话框上设置参数。第二个块不正确,因为您正在那里创建一个新的、空的
Bundle
,而不是检索参数。您在最后一个块中的想法是正确的,但请确保更正第一个,将参数设置在正确的
Fragment
实例上。非常感谢@Mike,您的建议解决了问题。我正在将工作代码添加到我原来的帖子中。
class B{
PassDataListener listener;
public(PassDataListener passDataListener){
this.listener=passDataListener;
}
//PASSING DATA TO CLASS WHERE YOU THIS DATA
String text="abc";
listener.getText(text);
}
 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");
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");

 }