我有一个包含多个片段的应用程序。 我的第一个片段是HomeFragment,我正在使用viewpager和recycler视图。
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_home, container, false);
ButterKnife.bind(this, view);
((AppCompatActivity) getActivity()).getSupportActionBar().setTitle(Constant.HOME_TITLE);
// view Pager
mPager = (ViewPager) view.findViewById(R.id.pager);
// recycler view
rv_transactions.setHasFixedSize(true);
LinearLayoutManager mLayoutmanager = new LinearLayoutManager(getContext());
rv_transactions.setLayoutManager(mLayoutmanager);
}
在HomeFragment的onStart中,我正在使用asyncTask进行网络调用。在asynctask的compltion我有一个回调onTaskComplete()我在哪里设置我的列表和寻呼机
public void onTaskComplete(JSONObject response){
// some jsonparsing logic, then
TransactionRecyclerViewAdapter adapter = new TransactionRecyclerViewAdapter(cardInfoAndTransactionArrayList.get(0).getTransactions());
Log.d("rv_adapter",""+cardInfoAndTransactionArrayList.get(0).getTransactions());
rv_transactions.setAdapter(adapter);
mPagerAdapter = new MultipleCardPagerAdapter(getChildFragmentManager(), cardInfoAndTransactionArrayList);
Log.d("ArrayList size",""+cardInfoAndTransactionArrayList.size());
Log.d("mPagerAdapter",""+mPagerAdapter);
Log.d("mPager",""+mPager);
mPager.setAdapter(mPagerAdapter);
mPager.addOnPageChangeListener(this);
}
然后有一个片段名称IgFragment从我回到HomeFragment
android.support.v4.app.FragmentManager fManager = getActivity().getSupportFragmentManager();
Boolean isPoped = fManager.popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
HomeFragment homeFragment = HomeFragment.newInstance(mobileNumber, "");
FragmentTransaction transaction = fManager.beginTransaction();
transaction.replace(R.id.fragment_container, homeFragment);
transaction.commit();
app的流程就像HomeFragment - >第1片段 - >第二片段 - > IGFragment - > HomeFragment和我正在添加1& 2 to addToBackStack(null)有后退按钮导航但在IGFragment我正在清理后台堆栈,因为当我在homeFragment时我不想要任何后退导航行为但是清除backstack使viewPager抛出nullpointer异常
logcat output (before exception thrown)
ArrayList size: 2
/mPagerAdapter: com.example.worldline.adapters.MultipleCardPagerAdapter@36efdd11
/mPager: android.support.v4.view.ViewPager{5a27e76 VFED.... ......ID 0,0-1080,486 #7f0f00f8 app:id/pager}
logcat Output (Exception thrown at mPager.setAdapter(mPagerAdapter);)
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
如果我不清除堆栈一切正常但HomeFragment在按下后退按钮时显示多个片段(但是没有异常来自viewpager)但是如果我清除backstack它会抛出空指针,即使arraylist的大小为2(logact输出)在将适配器设置为viewpager之前打印)
MultipleCardPagerAdapter代码
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import com.example.worldline.pojo.CardInfoAndTransactionsPojo;
import com.example.worldline.r_cug.MultipleCardViewPagerFragment;
import java.util.ArrayList;
/**
* Created by sunny.birman on 28-10-2016.
*/
public class MultipleCardPagerAdapter extends FragmentStatePagerAdapter {
private ArrayList<CardInfoAndTransactionsPojo> cardDetails;
private int cardListLength;
public MultipleCardPagerAdapter(FragmentManager fm, ArrayList<CardInfoAndTransactionsPojo> list) {
super(fm);
this.cardDetails= list;
cardListLength = list.size();
}
@Override
public Fragment getItem(int position) {
return MultipleCardViewPagerFragment.newInstance(cardDetails.get(position).getCardNumber(),cardDetails.get(position).getCardBalance(),position,cardListLength);
}
@Override
public int getCount() {
return cardDetails.size();
}
@Override
public int getItemPosition(Object object) {
return PagerAdapter.POSITION_NONE;
}
}