将数据从 Activity 传递到另一个,然后传递到 ViewPager 中的片段

Passing Data From Activity to another and then passing to fragment in ViewPager

我正在尝试将数据从我的 MainActivty 传递到另一个 ViewPager,并在 Fragment.

中获取该数据

我尝试了很多方法,但应用程序每次都崩溃。

这是我的 类:

我的持有人:

@Override
public void onBindViewHolder(main_rv_holder holder, int position) {
  holder.name.setText(mains.get(position).getName());
  holder.image_main.setImageResource(mains.get(position).getImg());

  //Clicked
  holder.setItemClickListener(new ItemClickListener() {
    @Override
    public void onItemClick(View view, int position) {
      Intent intent = new Intent(context,ViewerActivity.class);
      intent.putExtra("title",mains.get(position).getName());
      intent.putExtra("pos",position);
      context.startActivity(intent);
    }
  });
}

我的 Activity 带寻呼机:

protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_viewer);
  //Toolbar Initialization
  InitializeToolbar();
  //ViewPAGER Initialization
  InitializePager();
  //Floating Action Button
  Fabinitialize();

  //pass data
  Bundle data = new Bundle();
  //Use bundle to pass data
  Form1 fragmentobj = new Form1();

  data.putString("tag","Hello");
  fragmentobj.setArguments(data);
}

ViewPager 中的片段:

public class Form1 extends Fragment {
  String TAG;

  public Form1() {
    TAG = getArguments().getString("tag");
    // Required empty public constructor
  }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment, container, false);
    TextView textView = (TextView) view.findViewById(R.id.text_VIew);
    textView.setText(TAG);

    return view;
  }
}

谢谢。

见下方代码

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment, container, false);
        TextView textView = (TextView) view.findViewById(R.id.text_VIew);
    String title= getActivity().getIntent().getStringExtra("title");
        textView.setText(title);

            return view;
        }
    }