LBJ33 LBJ33 - 4 months ago 29
Android Question

Intent to fragment not working

I'm having trouble sending intent to fragment. I'm trying to send intent from one class to another, then setArgument for the fragment. In the fragment I then want a switch statement to set the data to the recycler view based on the value of the arguement.

Here is the first activity(I've only shown 2 child positions where I set the intent, in reality there are many more):

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


Here is where the fragments are created, and where I attempt to set the arguments:

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


And finally here is one of the three fragment classes where I try to use a switch statement to set values:

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;


And here is the error that crashes the app:

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.access$1100(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.access$1100(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) 


Where WedFragment.java:24 is this line:

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


WorkoutDaysActivity.java:47 is this line:

WedFragment wedFragment = new WedFragment();


and WorkoutDaysActivity.java:27 is this line:

tabLayout.setupWithViewPager(vp);

Answer

Put these two line inside your onCreate function :

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