我是Android的新手。我想创建一个包含3个水平页面的活动。每个页面都有一个网格视图。我使用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.
我搜索了几天但找不到任何帮助来解决它
的人有一个非常相似的问题和
Fragment must be a public static class to be properly recreated from instance state
为什么ArrayListFragment必须是公共静态类,我想每个都必须有一个单独的片段
这里有什么问题,任何人都可以帮我搞清楚
答案 0 :(得分:1)
ArrayListFragment与CategoryActivity在同一个java文件中吗?如果为true,则静态类意味着可以在不创建CategoryActivity实例的情况下创建ArrayListFragment的实例。因此每个类都可以拥有自己的ArrayListFragment实例。