如何使用Bundle从Activity to Fragment接收值

时间:2016-02-17 12:21:47

标签: android bundle

  

我正在将值从Activity发送到Fragment。在收到Log cat节目Null Pointer Exception

的值时

发送值代码

 Bundle bundle = new Bundle();
bundle.putString("address",sms.get(position).getmAddress());
bundle.putString("body",sms.get(position).getmBody());
InboxFragment inboxfragment = new InboxFragment();
inboxfragment.setArguments(bundle);
inboxfragment.setArguments(bundle);
  

接收值代码

 Bundle bundle= getArguments();
   String address =  bundle.getString("address");
    String body = bundle.getString("body");
    addresstv.setText(address);
    msgtv.setText(body);
  

Log Cat

02-17 12:31:23.363 19118-19118/com.smsfilter E/AndroidRuntime: FATAL EXCEPTION: main java.lang.NullPointerException
at com.smsfilter.InboxFragment.onCreateView(InboxFragment.java:53)
at android.app.Fragment.performCreateView(Fragment.java:1695)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:885)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057)
at android.app.BackStackRecord.run(BackStackRecord.java:682)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435)
at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
  

OnCreateView()方法代码

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
   //        return super.onCreateView(inflater, container, savedInstanceState);
    view = inflater.inflate(R.layout.messagebox,container,false);
    lvMsg=(ListView)view.findViewById(R.id.lvMsg);
    checkBox=(CheckBox)view.findViewById(R.id.checkBox);
    TextView addresstv = (TextView) view.findViewById(R.id.lblNumber);
     msgtv = (TextView) view.findViewById(R.id.lblMsg);
    sms= new ArrayList<Message>();
    list = new ArrayList<String>();
    populateMessageList();
     Bundle bundle= getArguments();
    //Bundle bundle= getActivity().getIntent().getExtras();
   String address =  bundle.getString("add");
    String body = bundle.getString("body");
    //addresstv.setText(address);
    msgtv.setText(body);    
     return view;

}

6 个答案:

答案 0 :(得分:0)

试试这种方式。

    @Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    String address =  getArguments().getString("address");
    String body = getArguments().getString("body");
    addresstv.setText(address);
    msgtv.setText(body);
}

答案 1 :(得分:0)

您可以将数据包从活动发送到片段,如下所示

Bundle bundle = new Bundle();
bundle.putString("address", "sms.get(position).getmAddress()");
// set Fragment Arguments
Fragment fragment = new Fragment();
fragment.setArguments(bundle);

你可以像下面这样获取它:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    String address= getArguments().getString("address");   
textview.setText(address); 
    return inflater.inflate(R.layout.fragment, container, false);
}

这应该可以正常工作。 THANKYOU

答案 2 :(得分:0)

嘿,来自您的活动

你说得对。

Bundle bundle = new Bundle();
bundle.putString("address",sms.get(position).getmAddress());
bundle.putString("body",sms.get(position).getmBody());
InboxFragment inboxfragment = new InboxFragment();
inboxfragment. setArguments (bundle);

在fragment中,你必须在onCreate()中调用getArguments,你将获得getArguments()null。

所以在Fragment中尝试

String address, body;
TextView msgtv;
@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container,
                          Bundle savedInstanceState) {
     View view = inflater.inflate(R.layout.layout_filename, this, null);
     msgtv = (TextView) view.findViewById(R.id.your_view_if);
     Bundle bundle= getArguments();
     address =  bundle.getString("address");
     body = bundle.getString("body");
     msgtv.setText(body);

    return view;
}

或者您可以在

中使用相同的逻辑
public void onActivityCreated (Bundle savedInstanceState) {
     Bundle bundle= getArguments();
     address =  bundle.getString("address");
     body = bundle.getString("body");
     msgtv.setText(body);
}

这肯定会对你有用。

答案 3 :(得分:0)

无需传递捆绑包。您可以使用getActivity().getIntent().getString()直接检索活动意图。如果您没有将意图传递给活动,则可以实现listener以在片段和活动之间进行通信。

答案 4 :(得分:0)

您在onCreateView

中使用了不同的字符串键

更改

 String address =  bundle.getString("add");

代表

String address =  bundle.getString("address");

答案 5 :(得分:0)

为捆绑添加值时,您正在执行

bundle.putString("address",sms.get(position).getmAddress());

所以你使用的密钥是"address"

所以当你获得正在使用的价值时

String address =  bundle.getString("add");

键不匹配。 在撤消时,将密钥"add"更改为"address",如下所示

String address =  bundle.getString("address");

OR

当您在活动中投入价值时 sms.get(position).getmAddress()方法返回null。 因此,当您将null值设置为textview时,会出现NullPointerException。