意图碎片化不起作用

Intent to fragment not working

我无法将意图发送到片段。我正在尝试将意图从一个 class 发送到另一个,然后为该片段设置参数。在片段中,我然后想要一个 switch 语句根据参数的值将数据设置到回收器视图。

这是第一个 activity(我只显示了 2 个设置意图的子位置,实际上还有更多):

expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
        @Override
        public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
            if (groupPosition == 0) {
                if (childPosition == 0) {
                    Intent intent = new Intent(getApplicationContext(), WorkoutDaysActivity.class);
                    intent.putExtra("workout", "w29w1");
                    startActivity(intent);
                }
                if (childPosition == 1) {
                  Intent intent = new Intent(getApplicationContext(), WorkoutDaysActivity.class);
                    intent.putExtra("workout", "w29w1");
                    startActivity(intent);
                }

这里是创建片段的地方,也是我尝试设置参数的地方:

public class WorkoutDaysActivity extends BaseActivity{

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.workout_days);
    mToolBar = activateToolbar();
    setUpNavigationDrawer();


    ViewPager vp = (ViewPager) findViewById(R.id.view_pager);
    this.addPages(vp);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
//        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
    tabLayout.setupWithViewPager(vp);
    tabLayout.setOnTabSelectedListener(listener(vp));

}
private void addPages(ViewPager pager) {
    MyFragPagerAdapter adapter = new MyFragPagerAdapter(getSupportFragmentManager());
    Intent intent = getIntent();
    String workout = intent.getStringExtra("workout");
    Bundle args = new Bundle();
    args.putString("workout", workout);

    MondayFragment mondayFragment = new MondayFragment();
    mondayFragment.setArguments(args);
    adapter.addPage(mondayFragment);
    WedFragment wedFragment = new WedFragment();
    wedFragment.setArguments(args);
    adapter.addPage(wedFragment);
    FridayFragment fridayFragment = new FridayFragment();
    fridayFragment.setArguments(args);
    adapter.addPage(fridayFragment);

最后是我尝试使用 switch 语句设置值的三个片段 class 之一:

public class WedFragment extends Fragment {

Bundle args = getArguments();
String workout3 = args.getString("workout");

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.wed_fragment, null);

//        //RECYCLERVIEW
    RecyclerView rv = (RecyclerView) view.findViewById(R.id.mRecyclerWed);
    rv.setLayoutManager(new LinearLayoutManager(this.getActivity()));
    rv.setAdapter(new MyRecyclerAdapter(this.getActivity(), getWedWorkout()));

    return view;


}

private ArrayList<Workout> getWedWorkout() {
    //COLLECTION OF MONDAY WORKOUT

    ArrayList<Workout> workout = new ArrayList<>();
    switch(workout3) {
        case "w29w1" : {
            Workout workouts = new Workout("Bench", "60", "6", "155", false);

            //ADD TO COLLECTION
            workout.add(workouts);

            workouts = new Workout("Bench", "70", "5", "175", false);
            workout.add(workouts);

            break;}
        case "w29w2" :{
            Workout workouts = new Workout("Squat", "60", "6", "155", false);

            //ADD TO COLLECTION
            workout.add(workouts);

            workouts = new Workout("Squat", "70", "5", "175", false);
            workout.add(workouts);

        break;}
    } return workout;

这里是导致应用程序崩溃的错误:

08-03 13:53:11.917 32143-32143/com.bestworkouts.sheikoworkout E/AndroidRuntime: FATAL EXCEPTION: main
                                                                            Process: com.bestworkouts.sheikoworkout, PID: 32143
                                                                            java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bestworkouts.sheikoworkout/com.bestworkouts.sheikoworkout.WorkoutDaysActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
                                                                                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3254)
                                                                                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350)
                                                                                at android.app.ActivityThread.access00(ActivityThread.java:222)
                                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                at android.os.Looper.loop(Looper.java:158)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:7229)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
                                                                             Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
                                                                                at com.bestworkouts.sheikoworkout.Fragments.WedFragment.<init>(WedFragment.java:24)
                                                                                at com.bestworkouts.sheikoworkout.WorkoutDaysActivity.addPages(WorkoutDaysActivity.java:47)
                                                                                at com.bestworkouts.sheikoworkout.WorkoutDaysActivity.onCreate(WorkoutDaysActivity.java:27)
                                                                                at android.app.Activity.performCreate(Activity.java:6876)
                                                                                at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1135)
                                                                                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3207)
                                                                                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3350) 
                                                                                at android.app.ActivityThread.access00(ActivityThread.java:222) 
                                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1795) 
                                                                                at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                at android.os.Looper.loop(Looper.java:158) 
                                                                                at android.app.ActivityThread.main(ActivityThread.java:7229) 
                                                                                at java.lang.reflect.Method.invoke(Native Method) 
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

其中 WedFragment.java:24 是这一行:

String workout3 = args.getString("workout");

WorkoutDaysActivity.java:47 是这一行:

 WedFragment wedFragment = new WedFragment();

和WorkoutDaysActivity.java:27是这一行:

tabLayout.setupWithViewPager(vp);

将这两行放在你的 onCreate 函数中:

public class WedFragment extends Fragment {

Bundle args;
String workout3="";

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    args = getArguments();
    workout3 = args.getString("workout");
    View view = inflater.inflate(R.layout.wed_fragment, null);

    //RECYCLERVIEW
    RecyclerView rv = (RecyclerView) view.findViewById(R.id.mRecyclerWed);
    rv.setLayoutManager(new LinearLayoutManager(this.getActivity()));
    rv.setAdapter(new MyRecyclerAdapter(this.getActivity(), getWedWorkout()));

    return view;
}