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

时间:2016-12-09 07:08:37

标签: android listview android-fragments page-fragments

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

我使用这种方式,但得到空指针的错误。

主要活动

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

    }

}

5 个答案:

答案 0 :(得分:1)

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

如果您动态添加片段:

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了解详情。

答案 1 :(得分:0)

http://www.androhub.com/android-pass-data-from-activity-to-fragment/ 在这里查看此链接,您可以更清楚地找到您想要的内容

答案 2 :(得分:0)

由于 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);
    }

答案 3 :(得分:0)

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

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

并在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;
    }

答案 4 :(得分:0)

试试这个:

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