将数据从适配器发送到Android中的片段。getArguments()返回null
我想使用将数据从适配器发送到Android中的片段。getArguments()返回null,android,Android,我想使用适配器中存在的片段数据。我正在使用setArguments()从Adapter发送数据,并使用getArguments()接收数据 ReversalFragment f1 = new ReversalFragment(); Bundle bundle = new Bundle(); String transId = item.getTxId(); bundle.putString("tId", transId); f1.setArguments(bundle); Bundle argu
适配器中存在的片段数据。我正在使用setArguments()
从Adapter
发送数据,并使用getArguments()
接收数据
ReversalFragment f1 = new ReversalFragment();
Bundle bundle = new Bundle();
String transId = item.getTxId();
bundle.putString("tId", transId);
f1.setArguments(bundle);
Bundle arguments = getArguments();
if(arguments!=null) {
String transId = arguments.getString("tId");
if (transId != null ) {
txView.setText(transId);
}
}
FragmentB f1 = new FragmentB();
Bundle bundle = new Bundle();
String transId = item.getTxId();
bundle.putString("tId", transId);
f1.setArguments(bundle);
但是当我调试它时,我在getArguments
处得到一个nullpointerException
这就是我发送和接收数据的方式
在适配器中,用于发送数据
ReversalFragment f1 = new ReversalFragment();
Bundle bundle = new Bundle();
String transId = item.getTxId();
bundle.putString("tId", transId);
f1.setArguments(bundle);
Bundle arguments = getArguments();
if(arguments!=null) {
String transId = arguments.getString("tId");
if (transId != null ) {
txView.setText(transId);
}
}
FragmentB f1 = new FragmentB();
Bundle bundle = new Bundle();
String transId = item.getTxId();
bundle.putString("tId", transId);
f1.setArguments(bundle);
片段类,以获取数据
ReversalFragment f1 = new ReversalFragment();
Bundle bundle = new Bundle();
String transId = item.getTxId();
bundle.putString("tId", transId);
f1.setArguments(bundle);
Bundle arguments = getArguments();
if(arguments!=null) {
String transId = arguments.getString("tId");
if (transId != null ) {
txView.setText(transId);
}
}
FragmentB f1 = new FragmentB();
Bundle bundle = new Bundle();
String transId = item.getTxId();
bundle.putString("tId", transId);
f1.setArguments(bundle);
有谁能帮助我处理这个异常,以及为什么getArguments()
null
提前感谢。在适配器中的视图上使用can setTag,并在片段中使用GETAG。
有内置的方法可以在setTag()中传递对象
您应该在适配器类中调用fragment类对象。假设您的fragment类名为FragmentB,那么您应该调用FragmentB类对象,因为在您的程序中,它无法找到您要将数据传递到哪里的片段
ReversalFragment f1 = new ReversalFragment();
Bundle bundle = new Bundle();
String transId = item.getTxId();
bundle.putString("tId", transId);
f1.setArguments(bundle);
Bundle arguments = getArguments();
if(arguments!=null) {
String transId = arguments.getString("tId");
if (transId != null ) {
txView.setText(transId);
}
}
FragmentB f1 = new FragmentB();
Bundle bundle = new Bundle();
String transId = item.getTxId();
bundle.putString("tId", transId);
f1.setArguments(bundle);
您没有明确指定片段的名称,
而不是Fragment f1=new Fragment()代码>
编写Your_Fragment_Name f1=新建Your_Fragment_Name()代码>
那就行了。你应该试试另一种方法。在当前方法中,您正在为适配器类中的片段类创建一个对象,并将值分配给该对象,但当您在片段类中时,它会创建一个新实例,并且值实际上为null
因此,您必须尝试以下方法:
在适配器类中放置getArgument()
和setArgument()
方法。
我非常确定您在Fragment类中为Adapter创建了对象,以便将其设置为listview或其他任何内容,此时设置并获取值
比如说
在片段类中:
BaseAdapter adapter = new Adapter(); //constructor called and values set
Bundle args = adapter.getArgument();
现在,无需进行任何进一步的方法调用,就可以获得所需的确切参数
适配器类:
Bundle bundle;
public Adapter(){
bundle = new Bundle();
//put required values
}
public Bundle getArgument(){
return bundle;
}
请注意,bundle变量在适配器类中是全局变量,因为您将从片段创建适配器,这意味着您可以访问片段中的适配器
您可以在适配器中创建public String getString()
,初始化适配器后,您可以在片段中调用此方法作为adapter。getString()
请在将该字符串添加到捆绑包之前检查transId字符串是否为null。如果item.getXid()返回null,则字符串transId也将为null。除此之外,我没有发现任何错误。Bundle arguments=this.getArguments();如果(argument.bundle.containsKey(“tId”){//do stuff}尝试使用此选项,我希望getArguments()不会返回null,而可能是arguments.getString(“tId”);正在将null返回给您。@Kingston-transId字符串不是null。检查过了。@JinalPatel-这也没用。即使我使用了这个.getArguments(),它也会返回null,直到条件满足时才会执行它。我在适配器类中传递数据时使用了fragment类对象。@inkedTechie-实际上我正在这样做,但在写这个问题时编辑了名称。我也会在问题中修改它,等等。@Adheesh在这种情况下,代码似乎没有任何问题。你是如何处理碎片的?如果您使用的是静态片段,这将不起作用。请包括处理碎片的代码。