gpt4 book ai didi

android - Fragment must be a public static class crash error after upgrade repo v9 in android studio 中

转载 作者:行者123 更新时间:2023-11-30 00:57:58 28 4
gpt4 key购买 nike

我是 Android 的新手。我想创建一个包含 3 个水平页面的 Activity 。每个页面都有一个 GridView 。我使用 ViewPager 创建页面并为每个页面使用 GridView。以下是代码

public class CategoryActivity extends ActionBarActivity {

static final int NUM_ITEMS = 3;

MyAdapter mAdapter;

ViewPager mPager;

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

getSupportActionBar().hide();

mAdapter = new MyAdapter(this.getSupportFragmentManager());

mPager = (ViewPager)findViewById(R.id.pager);
mPager.setAdapter(mAdapter);

}

//MyAdapter is adapter for Pager
public class MyAdapter extends FragmentPagerAdapter {

ArrayList<ArrayListFragment> fragments;
public MyAdapter(FragmentManager fm) {
super(fm);

fragments = new ArrayList<>();

for (int i = 0; i < NUM_ITEMS; i++) {
ArrayListFragment f = new ArrayListFragment();

fragments.add(f);
}
}

@Override
public int getCount() {
return NUM_ITEMS;
}

@Override
public Fragment getItem(int position) {

//For each page -> get fragment for that page
ArrayListFragment f = fragments.get(position);

return f;
}
}

//Adapter for each grid view in each Page
public class ImageAdapter extends BaseAdapter {
private Context mContext;
private View parentView;

public ImageAdapter(Context c) {
mContext = c;
}

public ImageAdapter(Context c, View aParentView) {
mContext = c;
parentView = aParentView;
}

public int getCount() {
return mThumbIds.length;
}

public Object getItem(int position) {
return null;
}

public long getItemId(int position) {
return 0;
}

// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
View itemView;
if (convertView == null) {

//I really config view for each item in grid view....
//The problem is not here
//..........
}
return itemView;
}

// references to our images
private Integer[] mThumbIds = {
R.drawable.images, R.drawable.images,
R.drawable.images, R.drawable.images,
R.drawable.images, R.drawable.images,
R.drawable.images, R.drawable.images,
R.drawable.images, R.drawable.images,
R.drawable.images, R.drawable.images
};
}

public class ArrayListFragment extends Fragment {

/**
* When creating, retrieve this instance's number from its arguments.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

/**
* The Fragment's UI is just a simple text view showing its
* instance number.
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.fragment_pager_list, container, false);

GridView gridview = (GridView) v.findViewById(R.id.categridview);

ImageAdapter ia = new ImageAdapter(CategoryActivity.this, gridview);
gridview.setAdapter(ia);

return v;
}


@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

}

}
}

它在 repo v7 中运行良好。但是在我升级到 repo v9 之后,它在启动后出错并崩溃

E/AndroidRuntime: FATAL EXCEPTION: main
Process: <my project process>, PID: 1901
java.lang.IllegalStateException: Fragment <my project package>.CategoryActivity.ArrayListFragment 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:426)
at android.support.v4.app.FragmentPagerAdapter.instantiateItem(FragmentPagerAdapter.java:103)
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:18788)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:901)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:389)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
at android.view.View.measure(View.java:18788)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643)
at android.view.View.measure(View.java:18788)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1216)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
I/Process: Sending signal. PID: 1901 SIG: 9
Application terminated.

我搜索了几天,但找不到任何修复它的帮助有人在

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

Fragment must be a public static class to be properly recreated from instance state

为什么 ArrayListFragment 必须是公共(public)静态类,我想每个都必须有一个单独的 fragment

这里有什么问题,谁能帮我弄清楚

最佳答案

ArrayListFragment 是否与 CategoryActivity 在同一个 java 文件中?如果为真,则静态类意味着可以创建 ArrayListFragment 的实例,而无需创建 CategoryActivity 的实例。所以每个类都可以有自己的 ArrayListFragment 实例。

关于android - Fragment must be a public static class crash error after upgrade repo v9 in android studio 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39853251/

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