简体   繁体   English

如何将数据从活动传递到片段..?

[英]how to pass data from activity to fragment..?

Hello everyone i need help in passing data from activity to fragment. 大家好,我需要帮助将数据从活动传递到片段。

im using the this way but getting error of null pointer . 即时通讯使用这种方式,但得到空指针错误。

In main Activity 在主要活动中

 @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.leftfeedback:
                handleChanges();
                break;
        }
    }

   private void handleChanges() {

                FeedBackFragment feedBackFragment =new FeedBackFragment();
                if (feedBackFragment != null) {
                   feedBackFragment.fragmentCommunication(ExtraData);
                } else {
                    Log.i(TAG, "Fragment 2 is not initialized");
                }
    }

in fragment side all given data is coming i checked with log before to set on 在片段方面,所有给定的数据都来了,我在设置之前用日志检查了

   public class FeedBackFragment extends Fragment{

    private static final String TAG ="FeedBackFragment" ;

    View view;

    TextView feedbackEditText;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.feedback_fragemnt, container, false);

        feedbackEditText = (TextView) view.findViewById(R.id.feedbackEditText);


        return view;
    }

    public void fragmentCommunication(String feedBackData) {

    log.i(TAG,feedBackData);
        try {

            JSONObject jsonObject = new JSONObject(feedBackData);

            String message = jsonObject.getString("message");
            if(message.trim()!=null){

            feedbackEditText.setText(message);

             }
           } catch (JSONException e) {
            e.printStackTrace();
        }

    }

}

This could happen if you haven't created the fragment in the right way. 如果您没有以正确的方式创建片段,则可能会发生这种情况。

If you dynamically add the fragment with: 如果您通过以下方式动态添加片段:

getSupportFragmentManager().beginTransaction(). 
              replace(R.id.container, new FeedBackFragment(), YOUR_FRAGMENT_TAG).
              commit();

You can use the following code to communicate with the fragment. 您可以使用以下代码与该片段进行通信。

private void handleChanges() {
  FeedBackFragment feedBackFragment =new FeedBackFragment();

  // you need to use id if you add the fragment via layout.
  //FeedBackFragment feedBackFragment  = (FeedBackFragment)
  getSupportFragmentManager().findFragmentById(R.id.your_feed_back_fragment_id);

  // If you dynamically add the fragment, use tag to find the fragment.  
  FeedBackFragment feedBackFragment  = (FeedBackFragment)
  getSupportFragmentManager().findFragmentByTag(YOUR_FRAGMENT_TAG); 

  if (feedBackFragment != null) {
    feedBackFragment.fragmentCommunication(ExtraData);
  } else {
    Log.i(TAG, "Fragment 2 is not initialized");
  }
}

Read more at Creating and Using Fragments . 创建和使用片段上阅读更多内容。

http://www.androhub.com/android-pass-data-from-activity-to-fragment/在此处检查此链接,您可以找到确切的所需信息并进行更好的澄清

Because of feedBackFragment not create , so Edittext is null 由于feedBackFragment未创建 ,因此Edittext为null

you should attach your feedBackFragment to MainActivity . 您应该将feedBackFragment附加到MainActivity

 FeedBackFragment feedBackFragment;
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.leftfeedback:
                handleChanges();
                break;
        }
    }

    private void handleChanges() {

        if (null == feedBackFragment) {
            feedBackFragment = new FeedBackFragment();
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.add(contentId, feedBackFragment);
            transaction.commit();
        }
        feedBackFragment.fragmentCommunication(ExtraData);
    }

I have changed some of your code try this. 我已经更改了一些代码,请尝试此操作。 It works for you. 它为您工作。

    private void handleChanges() {

         FeedBackFragment feedBackFragment =new FeedBackFragment();
         if (feedBackFragment != null) {
             Bundle bundle = new Bundle();
             bundle.putString("edttext", "ExtraData");
            // set Fragmentclass Arguments
             feedBackFragment.setArguments(bundle);
         } else {
             Log.i(TAG, "Fragment 2 is not initialized");
         }
   }

And in Fragment 并在片段中

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        String strtext = getArguments().getString("edttext");    
        view = inflater.inflate(R.layout.feedback_fragemnt, container, false);

        feedbackEditText = (TextView) view.findViewById(R.id.feedbackEditText);
        feedbackEditText.setText(strtext);

        return view;
    }

try this : 尝试这个 :

private void handleChanges() {
    Bundle bundle = new Bundle();
    bundle.putString("KEY", "string");
    FeedBackFragment fragment = new FeedBackFragment();
    fragment.setArguments(bundle);

    getSupportFragmentManager()
        .beginTransaction()
        .replace(R.id.container, fragment)
        .commit();
}

on your fragment : to get the data : 在您的片段上:获取数据:

Bundle bundle = getArguments();
if (bundle != null) {
    String data = bundle.getString("KEY");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM