更新到支持存储库后,
compile('com.android.support:support-v4:26.+') {
force = true
}
compile('com.android.support:appcompat-v7:26.+') {
force = true
}
compile('com.android.support:design:26.+') {
force = true
}
compile('com.android.support:cardview-v7:26.+') {
force = true
}
compile('com.android.support:recyclerview-v7:26.+') {
force = true
}
我得到了这个奇怪的例外。
TransactionDetailsFragment必须是要从实例状态
正确重新创建的公共静态类
这是我的源代码
public class TransactionDetailsViewFragment extends MainFragment implements FragmentWithKeepPrevious, FragmentWithName, FragmentWithBackAction {
private ViewPager mViewPager;
private PagerTitleStrip mPagerTitleStrip;
private View mView;
private int transaclionPosition;
private List<Transaction> cashTransactions;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.fragment_transaction_details_view, container, false);
mViewPager = mView.findViewById(R.id.u_transaction_details_viewpager_3);
mPagerTitleStrip = mView.findViewById(R.id.u_transaction_details_header);
try {
cashTransactions = new ArrayList<>();
if (GlobalClassParameters.transactions != null) {
cashTransactions = GlobalClassParameters.transactions;
}
} catch (ClassCastException e) {
e.printStackTrace();
} catch (IndexOutOfBoundsException e) {
e.printStackTrace();
}
if (cashTransactions != null)
mViewPager.setAdapter(new ScreenSlidePagerAdapter(getChildFragmentManager()));
mPagerTitleStrip.setTextColor(Color.parseColor("#ffffff"));
Bundle bundle = this.getArguments();
if (bundle != null) {
transaclionPosition = bundle.getInt("transactionPosition");
mViewPager.setCurrentItem(transaclionPosition);
}
mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
try {
mViewPager.setCurrentItem(position);
} catch (IndexOutOfBoundsException e) {
e.printStackTrace();
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
return mView;
}
void syncTitles() {
for (int counter = 0; counter < mPagerTitleStrip.getChildCount(); counter++) {
if (mPagerTitleStrip.getChildAt(counter) instanceof TextView) {
TextView textView = ((TextView) mPagerTitleStrip.getChildAt(counter));
UniPAYTypefaceUtils.setLocalisation(textView, getActivity(), TextStyle.LIGHT);
textView.setTextSize(14);
}
}
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
TransactionDetailsFragment transactionDetailsFragment = new TransactionDetailsFragment();
transactionDetailsFragment.setTransaction(cashTransactions.get(position).getTransactionDetails().getTransactionView());
transactionDetailsFragment.setTransactionButtons(cashTransactions.get(position).getTransactionDetails().getTransationButton());
transactionDetailsFragment.setmTransactionPosition(position);
syncTitles();
return transactionDetailsFragment;
}
@Override
public int getCount() {
return cashTransactions.size();
}
@Override
public CharSequence getPageTitle(int position) {
String title = cashTransactions.get(position).getName();
SpannableString whiteSpannable = new SpannableString(title);
return whiteSpannable;
}
}
@Override
public boolean onBack() {
transaclionPosition = 0;
return true;
}
@Override
public int getName() {
return R.string.u_transaction_details;
}
@Override
public Class getBackFragment() {
transaclionPosition = 0;
return HistoryFragment.class;
}
public class TransactionDetailsFragment extends Fragment {
private List<KeyValueParams> mTransaction;
private List<TransationButton> transationButtons;
private int mTransactionPosition = -1;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.view_transaction_details, container, false);
CustomRecyclerView listView = view.findViewById(R.id.u_transaction_details);
if (mTransaction != null) {
final KeyValueRecyclerViewAdapter keyValueAdapter = new KeyValueRecyclerViewAdapter(getActivity(), mTransaction);
listView.setAdapter(keyValueAdapter);
listView.setLayoutManager(new LinearLayoutManager(getActivity()));
keyValueAdapter.notifyDataSetChanged();
}
((TextView) view.findViewById(R.id.t_datetime)).setText(cashTransactions.get(mTransactionPosition).getDatetime());
return view;
}
public void setTransaction(List<KeyValueParams> mTransaction) {
this.mTransaction = mTransaction;
}
public void setTransactionButtons(List<TransationButton> transactionButtons) {
this.transationButtons = transactionButtons;
}
public void setmTransactionPosition(int position) {
this.mTransactionPosition = position;
}
}
}
如您所见,我在Viewpager中使用ViewPager和PagerTitleStrip。这是我的xml代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffffff"
android:clickable="true">
<LinearLayout
android:id="@+id/u_header"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="@+id/u_transaction_details_viewpager_3"
android:layout_width="fill_parent"
android:layout_height="match_parent">
<android.support.v4.view.PagerTitleStrip
android:id="@+id/u_transaction_details_header"
android:layout_width="fill_parent"
android:layout_height="48dp"
android:layout_gravity="top"
android:paddingLeft="@dimen/u_common_margin_left"
android:paddingRight="@dimen/u_common_margin_right"
android:background="@color/u_pink_light"
android:padding="@dimen/u_common_text_size_small"
android:textAlignment="center" />
</android.support.v4.view.ViewPager>
</LinearLayout>
</LinearLayout>
当我试图去我的片段时,我有这个例外 任何人都可以告诉我在我的源代码中出现了什么问题,或者我如何编写代码以便在新的支持库中无异常地运行?
答案 0 :(得分:3)
您应该将TransactionDetailsFragment设置为一个完全独立的Java文件,或者将其设为public static class
,如错误所示。
这与您的Gradle依赖项无关,尽管您应该避免在任何依赖项中使用+