1

In my app there are two fragments (MenuFragment, BasketFragment) and they are located in actionbar tab in navigation mode.

In MenuFragment i hold objects of Product class (Parcelable) in ArrayList in a listview. What i want to do is to carry product to BasketFragment in runtime when i click the item.

I have tried to do that with callback interface and bundle approaches so far and i have always faced with NullPointerException.

Please help me with that. If you would give code example, will be very appreciated.

4

1 回答 1

0

使用回调接口是正确的方法。如果没有看到你得到的 NullPointerException 的堆栈跟踪,我会假设这是因为回调没有正确设置。我认为如果你这样做,onAttach()它应该可以正常工作:

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try{
        mCallback = (BasketCallback)activity;
    }catch(ClassCastException ex){
        throw new ClassCastException(activity.getLocalClassName() + " must implement BasketCallback");
    }
}

然后,您可以从您的片段调用您的回调方法,并让活动使用传递对象到第二个片段getSupportFragmentManager().findFragmentById(R.id.basket_fragment);

于 2013-10-09T02:17:24.553 回答