我使用片段布局创建了一个Android应用。我遇到了一个问题" 空构造函数"所以我改变了我的代码。
public static final GridFragment newInstance(Item[] gridItems)
{
GridFragment f = new GridFragment();
Bundle bdl = new Bundle(1);
bdl.putParcelableArray("objitem", gridItems);
f.setArguments(bdl);
return f;
}
GridFragment 扩展 SherlockFragment ,我将其用于 HomeActivity ,扩展了 SherlockFragmentActivity
在HomeActivity中我使用了 onResume()而且我添加了代码来绑定片段的所有内容。现在我的问题是这个
的错误 它发生在内存缓慢的某个时候我的意思是当app进入等待模式时。我猜原因是内存不足。但是当我尝试按下菜单按钮并将应用程序发送到后台并重新打开它意味着它进入恢复模式时,我的困惑就是这个。它的工作原理比为什么片段变空?我的意思是它不适用于活动类的简历方法吗?我的item类是实现Parcelable******** CAUSE OF ERROR ********
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pavans.converter/com.pavans.converter.HomeActivity}: java.lang.ClassCastException: android.os.Parcelable[] cannot be cast to com.pavans.converter.model.Item[] at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2077) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104) at android.app.ActivityThread.access$600(ActivityThread.java:134) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:4624) 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:809) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.ClassCastException: android.os.Parcelable[] cannot be cast to com.pavans.converter.model.Item[] at com.pavans.converter.GridFragment.onCreateView(GridFragment.java:55) at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1086) at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1884) at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:566) at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1136) at android.app.Activity.performStart(Activity.java:4489) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2050) ... 11 more
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Bundle bundle = this.getArguments();
gridItems= (Item[]) bundle.getParcelableArray("objitem");// line no 55
View view;
view = inflater.inflate(R.layout.grid_layout, container, false);
mGridView = (GridView) view.findViewById(R.id.gridView);
return view;
}
答案 0 :(得分:0)
您能否尝试迭代Parcelable[]
,然后逐项投放到Item
?
items = bundle.getParcelableArray("objitem");
for (Parcelable item : items) {
mItem = (Item) item;
}
因为您可能无法直接转换parcelable数组。
实际上我更喜欢在Fragment类中编写一个setData()方法,稍后再设置数组。或者也许我会在项目的活动中写一个吸气剂。