android - Class Cast Exception with Fragment lists and Activities -
i'm inexperienced java , nearing finish of first android app, when worst fear happened , reached error have don't know how interpret.
the basics of app 5 lists, selected drawer menu. each list opens static xml views contents on it. works in portrait smoothly. in landscape, view dualpane, list on left, , contents on right. getting there works fine too. error when go landscape on 1 list, later on try going landscape on list. can please try me figure out happening?
main activity
import android.content.context; import android.content.intent; import android.content.sharedpreferences; import android.content.res.configuration; import android.net.uri; import android.os.bundle; import android.support.v4.app.actionbardrawertoggle; import android.support.v4.app.fragment; import android.support.v4.app.fragmentmanager; import android.support.v4.app.fragmenttransaction; import android.support.v4.view.gravitycompat; import android.support.v4.widget.drawerlayout; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.adapterview; import android.widget.baseadapter; import android.widget.listview; import android.widget.textview; import com.actionbarsherlock.app.sherlockfragmentactivity; import com.actionbarsherlock.view.menu; import com.actionbarsherlock.view.menuitem; public class mainactivity extends sherlockfragmentactivity { fragment rotcfraglist = new rotcfraglist(); fragment afafraglist = new afafraglist(); fragment bmtfraglist = new bmtfraglist(); fragment docsfraglist = new docsfraglist(); fragment extrafraglist = new extrafraglist(); .... *drawer set stuff .... // listview click listener in navigation drawer private class draweritemclicklistener implements listview.onitemclicklistener { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { selectitem(position); } } private void selectitem(int position) { fragmentmanager fm = getsupportfragmentmanager(); fragmenttransaction ft = getsupportfragmentmanager().begintransaction(); view fullframe = findviewbyid(r.id.full_frame); view detailsframe = findviewbyid(r.id.details); view listframe = findviewbyid(r.id.list_frame); // destroy backstack first fm.popbackstack(null, fragmentmanager.pop_back_stack_inclusive); // locate position switch (position) { ///rotc case 0: ft.remove(docsfraglist); ft.remove(extrafraglist); if (fullframe != null && fullframe.getvisibility() == view.visible) { fullframe.setvisibility(view.gone); } if (detailsframe != null && detailsframe.getvisibility() == view.gone) { detailsframe.setvisibility(view.visible); } if (listframe != null && listframe.getvisibility() == view.gone) { listframe.setvisibility(view.visible); } ft.replace(r.id.list_frame, rotcfraglist); break; ///afa case 1: ft.remove(docsfraglist); ft.remove(extrafraglist); if (fullframe != null && fullframe.getvisibility() == view.visible) { fullframe.setvisibility(view.gone); } if (detailsframe != null && detailsframe.getvisibility() == view.gone) { detailsframe.setvisibility(view.visible); } if (listframe != null && listframe.getvisibility() == view.gone) { listframe.setvisibility(view.visible); } ft.replace(r.id.list_frame, afafraglist); break; ///bmt case 2: ft.remove(docsfraglist); ft.remove(extrafraglist); if (fullframe != null && fullframe.getvisibility() == view.visible) { fullframe.setvisibility(view.gone); } if (detailsframe != null && detailsframe.getvisibility() == view.gone) { detailsframe.setvisibility(view.visible); } if (listframe != null && listframe.getvisibility() == view.gone) { listframe.setvisibility(view.visible); } ft.replace(r.id.list_frame, bmtfraglist); break; ///docs case 3: ft.remove(rotcfraglist); ft.remove(afafraglist); ft.remove(bmtfraglist); if (detailsframe != null && detailsframe.getvisibility() == view.visible) { detailsframe.setvisibility(view.gone); } if (fullframe != null && fullframe.getvisibility() == view.gone) { fullframe.setvisibility(view.visible); } if (listframe != null && listframe.getvisibility() == view.visible) { listframe.setvisibility(view.gone); } ft.replace(r.id.full_frame, docsfraglist); break; ///extra case 4: ft.remove(rotcfraglist); ft.remove(afafraglist); ft.remove(bmtfraglist); if (detailsframe != null && detailsframe.getvisibility() == view.visible) { detailsframe.setvisibility(view.gone); } if (fullframe != null && fullframe.getvisibility() == view.gone) { fullframe.setvisibility(view.visible); } if (listframe != null && listframe.getvisibility() == view.visible) { listframe.setvisibility(view.gone); } ft.replace(r.id.full_frame, extrafraglist); break; } ft.commit(); mdrawerlist.setitemchecked(position, true); // title followed position settitle(title[position]); // close drawer mdrawerlayout.closedrawer(mdrawerlist); }
one of list fragments
import com.actionbarsherlock.app.sherlockfragment; import com.actionbarsherlock.app.sherlockfragmentactivity; import com.actionbarsherlock.app.sherlocklistfragment; import android.content.intent; import android.content.res.configuration; import android.os.bundle; import android.support.v4.app.fragmenttransaction; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.arrayadapter; import android.widget.listview; public class bmtfraglist extends sherlocklistfragment { boolean mdualpane; static int mcurcheckposition = 0; public final string [] titles = { "core values", "af mission", "airman's creed", "air force song", "ranks af enlisted", "ranks af officer", "chain of command", "reporting statement", "phonetic alphabet" }; @override public void onactivitycreated(bundle savedinstancestate) { super.onactivitycreated(savedinstancestate); setlistadapter(new arrayadapter<string>(getactivity(), r.layout.simple_list_item_checkable_1, android.r.id.text1, titles)); // check landscape or not view detailsframe = getactivity().findviewbyid(r.id.details); mdualpane = detailsframe != null && detailsframe.getvisibility() == view.visible; getlistview().setchoicemode(listview.choice_mode_single); if (savedinstancestate != null) { mcurcheckposition = savedinstancestate.getint("curchoice", 0); showdetails(mcurcheckposition); } if (mdualpane) { showdetails(mcurcheckposition); } } @override public void onsaveinstancestate(bundle outstate) { super.onsaveinstancestate(outstate); outstate.putint("curchoice", mcurcheckposition); } @override public void onlistitemclick(listview l, view v, int position, long id) { showdetails(position); } // if landscape, replace details. if not, replace list. void showdetails(int position) { fragmenttransaction ft = getactivity().getsupportfragmentmanager().begintransaction(); mcurcheckposition = position; if (mdualpane) { getlistview().setitemchecked(position, true); bmtdetailsfragment details = (bmtdetailsfragment) getactivity().getsupportfragmentmanager().findfragmentbyid(r.id.details); if (details == null || details.getshownindex() != position) { details = bmtdetailsfragment.newinstance(position); ft.replace(r.id.details, details); ft.settransition(fragmenttransaction.transit_fragment_fade); ft.commit(); } } else { getlistview().setitemchecked(position, true); intent intent = new intent(); intent.setclass(getactivity(), detailsactivity.class); intent.putextra("position", position); startactivity(intent); } } public static class detailsactivity extends sherlockfragmentactivity { @override protected void oncreate(bundle savedinstancestate) { settheme(mainactivity.getthemeid()); super.oncreate(savedinstancestate); if (getresources().getconfiguration().orientation == configuration.orientation_landscape) { finish(); return; } if (savedinstancestate == null) { bmtdetailsfragment details = new bmtdetailsfragment(); details.setarguments(getintent().getextras()); getsupportfragmentmanager().begintransaction().add(android.r.id.content, details).commit(); } } } public static class bmtdetailsfragment extends sherlockfragment { public static bmtdetailsfragment newinstance(int position) { bmtdetailsfragment f = new bmtdetailsfragment(); // make index input argument bundle args = new bundle(); args.putint("position", position); f.setarguments(args); return f; } public int getshownindex() { return getarguments().getint("position", 0); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { if (container == null) { return null; } view newcontent = getview(); int position = getshownindex(); switch (position) { case 0: newcontent = inflater.inflate(r.layout.corevalues, container, false); break; case 1: newcontent = inflater.inflate(r.layout.afmission, container, false); break; case 2: newcontent = inflater.inflate(r.layout.airmanscreed, container, false); break; ....more cases..... } return newcontent; } } }
main activity landscape layout
<android.support.v4.widget.drawerlayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <framelayout android:id="@+id/full_frame" android:layout_width="match_parent" android:layout_height="match_parent" /> <relativelayout android:layout_width="match_parent" android:layout_height="match_parent" > <framelayout android:id="@+id/list_frame" android:layout_width="180dp" android:layout_height="match_parent" /> <framelayout android:id="@+id/details" android:layout_width="fill_parent" android:layout_height="match_parent" android:layout_torightof="@id/list_frame" > </framelayout> </relativelayout> <listview android:id="@+id/listview_drawer" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" android:background="#262626" android:choicemode="singlechoice" android:divider="#5c5c5c" android:dividerheight="1dp" /> </android.support.v4.widget.drawerlayout>
logcat
sorry wall of code, i'm lost here. guess it's detailsframe next list, because error pointing @ detailsactivity in each class responsible setting contents in frame. maybe need clear out frame before can load new list new details same place? i'm not sure how go though. whatsoever appreciated!!! don't want see first app fall apart this, feel sick thinking it.
figured out solution! maybe it's not prettiest, works , that's better crashing.
i determined each listfragment had didn't "sharing" contents frame in landscape each other, made each list it's own frame layout put it's fragments into. when new list called, other frame layouts have visibility set "gone" , put in background. when called later, show contents held, bonus.
here's changes in mainactivity
// listview click listener in navigation drawer private class draweritemclicklistener implements listview.onitemclicklistener { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { selectitem(position); } } private void selectitem(int position) { fragmentmanager fm = getsupportfragmentmanager(); fragmenttransaction ft = getsupportfragmentmanager().begintransaction(); view fullframe = findviewbyid(r.id.full_frame); view detailsframerotc = findviewbyid(r.id.details_rotc); view listframe = findviewbyid(r.id.list_frame); view detailsframeafa = findviewbyid(r.id.details_afa); view detailsframebmt = findviewbyid(r.id.details_bmt); // destroy backstack first fm.popbackstack(null, fragmentmanager.pop_back_stack_inclusive); // locate position switch (position) { ///rotc case 0: ft.replace(r.id.list_frame, rotcfraglist); ft.remove(docsfraglist); ft.remove(extrafraglist); if (fullframe != null && fullframe.getvisibility() == view.visible) { fullframe.setvisibility(view.gone); } if (detailsframeafa != null && detailsframeafa.getvisibility() == view.visible) { detailsframeafa.setvisibility(view.gone); } if (detailsframebmt != null && detailsframebmt.getvisibility() == view.visible) { detailsframebmt.setvisibility(view.gone); } if (detailsframerotc != null && detailsframerotc.getvisibility() == view.gone) { detailsframerotc.setvisibility(view.visible); } if (listframe != null && listframe.getvisibility() == view.gone) { listframe.setvisibility(view.visible); } break; ///afa case 1: ft.replace(r.id.list_frame, afafraglist); ft.remove(docsfraglist); ft.remove(extrafraglist); if (fullframe != null && fullframe.getvisibility() == view.visible) { fullframe.setvisibility(view.gone); } if (detailsframerotc != null && detailsframerotc.getvisibility() == view.visible) { detailsframerotc.setvisibility(view.gone); } if (detailsframebmt != null && detailsframebmt.getvisibility() == view.visible) { detailsframebmt.setvisibility(view.gone); } if (detailsframeafa != null && detailsframeafa.getvisibility() == view.gone) { detailsframeafa.setvisibility(view.visible); } if (listframe != null && listframe.getvisibility() == view.gone) { listframe.setvisibility(view.visible); } break; ///bmt case 2: ft.replace(r.id.list_frame, bmtfraglist); ft.remove(docsfraglist); ft.remove(extrafraglist); if (fullframe != null && fullframe.getvisibility() == view.visible) { fullframe.setvisibility(view.gone); } if (detailsframeafa != null && detailsframeafa.getvisibility() == view.visible) { detailsframeafa.setvisibility(view.gone); } if (detailsframerotc != null && detailsframerotc.getvisibility() == view.visible) { detailsframerotc.setvisibility(view.gone); } if (detailsframebmt != null && detailsframebmt.getvisibility() == view.gone) { detailsframebmt.setvisibility(view.visible); } if (listframe != null && listframe.getvisibility() == view.gone) { listframe.setvisibility(view.visible); } break; ///docs case 3: ft.replace(r.id.full_frame, docsfraglist); ft.remove(rotcfraglist); ft.remove(afafraglist); ft.remove(bmtfraglist); if (fullframe != null && fullframe.getvisibility() == view.gone) { fullframe.setvisibility(view.visible); } if (listframe != null && listframe.getvisibility() == view.visible) { listframe.setvisibility(view.gone); } if (detailsframeafa != null && detailsframeafa.getvisibility() == view.visible) { detailsframeafa.setvisibility(view.gone); } if (detailsframebmt != null && detailsframebmt.getvisibility() == view.visible) { detailsframebmt.setvisibility(view.gone); } if (detailsframerotc != null && detailsframerotc.getvisibility() == view.visible) { detailsframerotc.setvisibility(view.gone); } break; ///extra case 4: ft.replace(r.id.full_frame, extrafraglist); ft.remove(rotcfraglist); ft.remove(afafraglist); ft.remove(bmtfraglist); if (fullframe != null && fullframe.getvisibility() == view.gone) { fullframe.setvisibility(view.visible); } if (listframe != null && listframe.getvisibility() == view.visible) { listframe.setvisibility(view.gone); } if (detailsframeafa != null && detailsframeafa.getvisibility() == view.visible) { detailsframeafa.setvisibility(view.gone); } if (detailsframebmt != null && detailsframebmt.getvisibility() == view.visible) { detailsframebmt.setvisibility(view.gone); } if (detailsframerotc != null && detailsframerotc.getvisibility() == view.visible) { detailsframerotc.setvisibility(view.gone); } break; } ft.commit(); mdrawerlist.setitemchecked(position, true); // title followed position settitle(title[position]); // close drawer mdrawerlayout.closedrawer(mdrawerlist); }
and landscape layout
<android.support.v4.widget.drawerlayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <framelayout android:id="@+id/full_frame" android:layout_width="match_parent" android:layout_height="match_parent" /> <relativelayout android:layout_width="match_parent" android:layout_height="match_parent" > <framelayout android:id="@+id/list_frame" android:layout_width="180dp" android:layout_height="match_parent" /> <framelayout android:id="@+id/details_rotc" android:layout_width="fill_parent" android:layout_height="match_parent" android:layout_torightof="@id/list_frame" > </framelayout> <framelayout android:id="@+id/details_afa" android:layout_width="fill_parent" android:layout_height="match_parent" android:layout_torightof="@id/list_frame" > </framelayout> <framelayout android:id="@+id/details_bmt" android:layout_width="fill_parent" android:layout_height="match_parent" android:layout_torightof="@id/list_frame" > </framelayout> </relativelayout> <listview android:id="@+id/listview_drawer" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" android:background="#262626" android:choicemode="singlechoice" android:divider="#5c5c5c" android:dividerheight="1dp" /> </android.support.v4.widget.drawerlayout>
Comments
Post a Comment