gpt4 book ai didi

java - Android IllegalStateException : Fragment null must be a public static class to be properly recreated from instance state

转载 作者:行者123 更新时间:2023-12-04 23:28:20 24 4
gpt4 key购买 nike

我从我的 android 应用程序中收到以下异常:

FATAL EXCEPTION: main
java.lang.IllegalStateException: Fragment null must be a public static class to be properly recreated from instance state.
at android.support.v4.app.BackStackRecord.doAddOp(BackStackRecord.java:435)
at android.support.v4.app.BackStackRecord.add(BackStackRecord.java:420)
at android.support.v4.app.FragmentStatePagerAdapter.instantiateItem(FragmentStatePagerAdapter.java:123)
at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:1006)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1154)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1088)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1614)
at android.view.View.measure(View.java:15525)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:15525)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:15525)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:389)
at android.view.View.measure(View.java:15525)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15525)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:15525)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2438)
at android.view.View.measure(View.java:15525)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
at android.view.Choreographer.doCallbacks(Choreographer.java:562)
at android.view.Choreographer.doFrame(Choreographer.java:532)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5227)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
at dalvik.system.NativeStart.main(Native Method)

不幸的是,该异常没有引用我的代码。一旦我开始一个带有 fragment 的 Activity ,它就会出现。我认为以下是我的代码的相关部分:
public class MonthViewActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_month);

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(new FragmentStatePagerAdapter(getSupportFragmentManager()) {
@Override
public Fragment getItem(final int position) {
return new Fragment() {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.month_view, container, false);
return rootView;
}
};
}

@Override
public int getCount() {
return 12;
}
});
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}

@Override
public void onPageSelected(int position) {
Month month = Year.getCurrent().getMonth(position);
TextView month_name = (TextView) findViewById(R.id.month_name);
month_name.setText(month.getName());
}

@Override
public void onPageScrollStateChanged(int state) {
}
});
viewPager.setCurrentItem(getIntent().getExtras().getInt("month"));

if (getIntent().getExtras().getInt("month") == 0) {
// The OnPageChangeListener doesn't do it for the first one
Month month = Year.getCurrent().getMonth(0);
TextView month_name = (TextView) findViewById(R.id.month_name);
month_name.setText(month.getName());
}
}
}

当我使用 Android Studio 1.5.1 时,我的应用程序没有抛出这个异常。它是在我升级到 Android Studio 2.1.3 后才出现的。

其他人似乎也有类似的问题: Fragment must be a public static class to be properly recreated from instance state
但是,我在那里找不到解决方案。

最佳答案

android支持lib 24.2.0及以上版本的问题我们可以使用
安卓支持库 23.0.0
退房:Fragment must be a public static class to be properly recreated from instance state

关于java - Android IllegalStateException : Fragment null must be a public static class to be properly recreated from instance state,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39558697/

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