我是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.
片段必须是从实例状态正确重新创建的公共静态类
为什么ArrayListFragment必须是一个公共静态类,我想每个类必须有一个单独的片段
这里有什么问题,有人能帮我弄清楚吗
ArrayListFragment与CategoryActivity在同一个java文件中吗?如果为真,则静态类意味着可以创建ArrayListFragment的实例,而无需创建CategoryActivity的实例。因此每个类都可以有自己的ArrayListFragment实例。
问题内容: 在我看来,创建辅助类时,非公共顶级类和静态嵌套类实际上执行相同的任务。 A.java A.java 除了如何引用它们,在我看来,创建助手类的两种方法之间几乎没有什么区别。它可能主要归结为偏好。有人看到我想念的东西吗?我想有些人会争辩说,每个源文件最好有一个类,但是从我的角度来看,在同一源文件中有一个非公共的顶级类似乎更干净,更有条理。 问题答案: 在这两个示例中,每个源文件都没有一个类
如何调用从公共静态空白到非静态的公共空白碎片类?或者有另一种方法在(listViewHolder.dot.setOnClickListener(new View.OnClickListener()和公共无效搜索3())
在我升级到gae 1.7.6后,dev_appserver崩溃了,因为我有带德语元音的文件夹。
问题内容: 我写了以下代码: 我收到错误消息: 第一行错误。为什么会发生,我该如何解决?请注意,我的项目名称为,程序包名称为,类名称如代码中所述。 编辑 -仅公开一个类或两个类均为默认类后,出现错误“选择不包含主类型”。现在我该怎么办? 问题答案: 如果文件包含顶级(非嵌套)类,则其名称与该公共类相同。因此,如果您有类似的课程,则需要将其放置在文件中。因此, 我们在一个文件中不能有两个公共类。 如
问题内容: 和之间有什么区别? 例如 : 要么 问题答案: 尽管您 确实 希望遵守约定,但在功能上没有什么区别(字节码将 完全相同 ),请访问JLS-8.3.1。场修饰符: __ 这将是奇怪的,看.. 我也建议您访问checkstyle。 编辑: 从同一页面链接到该部分: http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls