简体   繁体   中英

when Class newInstance is called

I have created One Android app using fragment layout. I faced an issue of " empty constructor " So I have Change my code.


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 is extends SherlockFragment and I used it with HomeActivity which extends SherlockFragmentActivity In HomeActivity I have used onResume() And I added code which binds all stuff for fragment. Now my problem is this
Error

******** 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

It occurs some time when memory is slow I mean when app goes with wait mode. I guess reason is low memory. But my confusion is this when I try to press menu button and send app to background and reopen it mean it goes in resume mode. It works fine than Why fragment become empty? I mean is it not goes with resume method of an activity class? My item class is implements Parcelable
Fragment onCreateView

 @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; } 

Can you please try to iterate through Parcelable[] and then cast items to Item one by one?

items = bundle.getParcelableArray("objitem");
for (Parcelable item : items) {
    mItem = (Item) item;
}

Because you may be not able to cast parcelable array directly.

Actually I would prefer to write a setData() method in Fragment class and set array later. Or maybe I would write a getter in activity for items.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM