gpt4 book ai didi

android - fragment 必须是公共(public)静态类才能从实例状态正确地重新创建?

转载 作者:行者123 更新时间:2023-11-29 16:45:38 24 4
gpt4 key购买 nike

更新到支持库后,

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 must be a public static class to be properly recreated from instance state

这是我的源代码

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>

当我尝试去我的 fragment 时,我有这个异常谁能告诉我我的源代码有什么问题,或者我如何编写代码才能在新的支持库中无异常运行?

最佳答案

您应该将 TransactionDetailsFragment 制作成一个完全独立的 Java 文件,或者将其制作成 public <b>static</b> class ,如错误所述。

这与您的 Gradle 依赖关系无关,尽管您老实说应该避免使用 +在任何依赖中

关于android - fragment 必须是公共(public)静态类才能从实例状态正确地重新创建?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48496931/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com