我有困難發送意圖片段。我試圖將意圖從一個類發送到另一個類,然後爲片段發送setArgument。在片段中,我需要一個switch語句根據參數的值將數據設置到回收站視圖。意圖片段不工作
這是第一個活動(我只出在哪裏設置的意圖,在現實中有更多的孩子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語句設定值中的一個:
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.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)
哪裏WedFragment.java:24是這一行:
String workout3 = args.getString("workout");
WorkoutDaysActivity.java:47是這一行:
WedFragment wedFragment = new WedFragment();
and WorkoutDaysActivity.java:27 is this line:
tabLayout.setupWithViewPager(vp);
是具有另一個切入點您的活動?我的意思是,另一個你沒有設置「鍛鍊」額外的意圖。到目前爲止找不到任何錯誤,請檢查您錯過的字符串。 –
下面的答案工作!必須切換Fragment類中的一些代碼! – LBJ33