Bundle bundle = getArguments()没有任何理由返回null

时间:2015-01-12 12:42:35

标签: java android

我只是面临一个导致我疯狂的问题,我只想在列表之间传递一个值,当我点击列表中的项目时我想将position发送到另一个列表,所以我使用了捆绑,它适用于我,但当我改进我的应用程序使用ViewPagerTabsPagerAdapter而不是旧标签,并在我做了所有的更改后,A NullPointerException特别发生这条线

int SelectedID = bundle.getInt("selectedID",0);

请注意getArguments()返回null

这是我的代码:

应用程序正常工作,直到我点击列表

class WorkoutsFragment extends ListFragment :

@Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id);

        WorkoutListFragment workoutListFrag = new WorkoutListFragment();
        Bundle bundle = new Bundle();
        //WorkoutID.get() to get the real position from database and it works
        bundle.putInt("selectedID", WorkoutID.get(position));
        workoutListFrag.setArguments(bundle);

        Toast.makeText(getActivity(), "l Position "+WorkoutID.get(position), Toast.LENGTH_SHORT).show();

        mCallback.onSwitchToNextFragment();


            // Set the item as checked to be highlighted when in two-pane layout
           list.setItemChecked(position, true);
    }

WorkoutListFragment扩展了ListFragment

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    View v = inflater.inflate(R.layout.fragment_list, container, false);

    // connect object of listview and listview id on xml
    list = (ListView) v.findViewById(android.R.id.list);


    wla = new WorkoutListAdapter(getActivity());

    // get value that passed from previous page
    //getArguments() returns null, i don't know why
    Bundle bundle = getArguments();
    //here is the line of NullPointerException 
    int SelectedID = bundle.getInt("selectedID",0);


    // call asynctask class to get data from database
    new getWorkoutList().execute();
    }

日志

01-12 11:29:44.048: E/AndroidRuntime(2404): FATAL EXCEPTION: main
01-12 11:29:44.048: E/AndroidRuntime(2404): java.lang.NullPointerException
01-12 11:29:44.048: E/AndroidRuntime(2404):     at com.daily.workout.app.WorkoutListFragment.onCreateView(WorkoutListFragment.java:72)
01-12 11:29:44.048: E/AndroidRuntime(2404):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
01-12 11:29:44.048: E/AndroidRuntime(2404):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
01-12 11:29:44.048: E/AndroidRuntime(2404):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
01-12 11:29:44.048: E/AndroidRuntime(2404):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
01-12 11:29:44.048: E/AndroidRuntime(2404):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
01-12 11:29:44.048: E/AndroidRuntime(2404):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)
01-12 11:29:44.048: E/AndroidRuntime(2404):     at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
01-12 11:29:44.048: E/AndroidRuntime(2404):     at android.support.v4.view.ViewPager.populate(ViewPager.java:1012)
01-12 11:29:44.048: E/AndroidRuntime(2404):     at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:523)
01-12 11:29:44.048: E/AndroidRuntime(2404):     at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:495)
01-12 11:29:44.048: E/AndroidRuntime(2404):     at android.support.v4.view.ViewPager.dataSetChanged(ViewPager.java:875)
01-12 11:29:44.048: E/AndroidRuntime(2404):     at android.support.v4.view.ViewPager$PagerObserver.onChanged(ViewPager.java:2690)
01-12 11:29:44.048: E/AndroidRuntime(2404):     at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:37)
01-12 11:29:44.048: E/AndroidRuntime(2404):     at android.support.v4.view.PagerAdapter.notifyDataSetChanged(PagerAdapter.java:276)
01-12 11:29:44.048: E/AndroidRuntime(2404):     at com.daily.workout.app.adapter.TabsPagerAdapter$1.onSwitchToNextFragment(TabsPagerAdapter.java:44)
01-12 11:29:44.048: E/AndroidRuntime(2404):     at com.daily.workout.app.WorkoutsFragment.onListItemClick(WorkoutsFragment.java:108)
01-12 11:29:44.048: E/AndroidRuntime(2404):     at android.support.v4.app.ListFragment$2.onItemClick(ListFragment.java:58)
01-12 11:29:44.048: E/AndroidRuntime(2404):     at android.widget.AdapterView.performItemClick(AdapterView.java:298)
01-12 11:29:44.048: E/AndroidRuntime(2404):     at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
01-12 11:29:44.048: E/AndroidRuntime(2404):     at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
01-12 11:29:44.048: E/AndroidRuntime(2404):     at android.widget.AbsListView$1.run(AbsListView.java:3423)
01-12 11:29:44.048: E/AndroidRuntime(2404):     at android.os.Handler.handleCallback(Handler.java:725)
01-12 11:29:44.048: E/AndroidRuntime(2404):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-12 11:29:44.048: E/AndroidRuntime(2404):     at android.os.Looper.loop(Looper.java:137)
01-12 11:29:44.048: E/AndroidRuntime(2404):     at android.app.ActivityThread.main(ActivityThread.java:5041)
01-12 11:29:44.048: E/AndroidRuntime(2404):     at java.lang.reflect.Method.invokeNative(Native Method)
01-12 11:29:44.048: E/AndroidRuntime(2404):     at java.lang.reflect.Method.invoke(Method.java:511)
01-12 11:29:44.048: E/AndroidRuntime(2404):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-12 11:29:44.048: E/AndroidRuntime(2404):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-12 11:29:44.048: E/AndroidRuntime(2404):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

我解决了这个问题,通过只使用一个实例,我误以为我把setArguments放到了一个实例(WorkoutListFragment w = new WorkoutListFragment())并启动了另一个新实例(WorkoutListFragment wo = new WorkoutListFragment())这个新的instace是空的,它没有任何论据,因为它返回NullPointerException

另一个重要的事情,你必须将bundle参数直接放在你想要启动的类(WorkoutListFragment)后面,这意味着bundle只能连续生存2个类,而且我有3个,A通过了从第一个到第三个的参数并没有起作用,第二个类负责启动第三个(WorkoutListFragment),因为我使用(ListFragments ,ViewPager,etc),所以我设置参数在第二节课。