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

how to pass data from activity to fragment..?

大家好,我需要帮助将数据从 activity 传递到片段。

我正在使用这种方式,但出现空指针错误。

主要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");
                }
    }

片段侧 所有给定的数据都来了我在设置

之前检查了日志
   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();
        }

    }

}

http://www.androhub.com/android-pass-data-from-activity-to-fragment/ 检查这个 link 在这里你可以找到你想要的更好的清晰度

因为feedBackFragment没有创建,所以Edittext为null

您应该将您的 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);
    }

如果您没有以正确的方式创建片段,可能会发生这种情况。

如果您动态添加片段:

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

您可以使用以下代码与片段进行通信。

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

Creating and Using Fragments 阅读更多内容。

我已经更改了您的一些代码试试这个。它适合你。

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

在片段中

 @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;
    }

试试这个:

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();
}

在您的片段上:获取数据:

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