Note that there are some explanatory texts on larger screens.

plurals
  1. POAdded tabs, now crash when orientation changes - Android w/ActionBarSherlock
    primarykey
    data
    text
    <p>Hopefully this isn't a dumb question with an answer right under my nose, because I've been spending a looooong time trying different things to fix it, and it's finally late enough that I'm going to drop this question and finally catch some sleep.</p> <p>I'm building a sample app to work out the kinks in a UI so I can later add it to a real app. The app will be tabs with listfragments, and on list selections it will open a new fragment with text or a picture.</p> <p>Everything works wonderfully in portrait, but I keep getting a crash when I try going to landscape from any tab, or fragment. I'm blaming the tabs, the orientation swap worked great when it was just one list, then I added the tabs and this error started appearing.</p> <p>The way I'm displaying the fragments is by replacing fragments in an xml, as I said it was working fine before the tabs showed up. The orientation switch prompts the need for a landscape xml though, and I guess the problem is with the tabs not reloading properly with the new layout when the orientation changes. I'm still super new, so I'm aware of using onSaveInstanceState but I'm not sure how to go about it correctly, or if the solution is different.</p> <p>Here comes the code.</p> <p><strong>Portrait XML</strong></p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/fragment_layout_support" android:layout_width="match_parent" android:layout_height="match_parent" &gt; ***id.titles includes the listview, which needs to be replaced in portrait*** &lt;fragment android:id="@+id/titles" android:layout_width="match_parent" android:layout_height="match_parent" class=".TitlesFragment" /&gt; &lt;/FrameLayout&gt; </code></pre> <p><strong>Landscape XML</strong></p> <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="16dp" android:layout_marginRight="16dp" android:baselineAligned="false" android:orientation="horizontal" tools:context=".TitlesFragment" &gt; ***listview on the left*** &lt;fragment android:id="@+id/titles" android:name=".TitlesFragment" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" /&gt; ***Details frame for fragments to inflate into on the right*** &lt;FrameLayout android:id="@+id/details" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="3" /&gt; &lt;/LinearLayout&gt; </code></pre> <p><strong>ListFragment Class</strong> (both lists are similar)</p> <pre><code>public class TitlesFragment extends SherlockListFragment implements ActionBar.TabListener { boolean mDualPane; int mCurCheckPosition = 0; private Fragment mFragment; @Override public void onActivityCreated(Bundle savedInstanceState){ super.onActivityCreated(savedInstanceState); setListAdapter (new ArrayAdapter&lt;String&gt; (getActivity(), android.R.layout.simple_list_item_1, android.R.id.text1, ColorList.COLORS)); /* Checking if landscape or not. id.details is unique to landscape */ View detailsFrame = getActivity().findViewById(R.id.details); mDualPane = detailsFrame != null &amp;&amp; detailsFrame.getVisibility() == View.VISIBLE; if (savedInstanceState != null){ mCurCheckPosition = savedInstanceState.getInt("curChoice", 0); } if (mDualPane){ getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE); showDetails(mCurCheckPosition); } } @Override public void onSaveInstanceState(Bundle outState){ super.onSaveInstanceState(outState); outState.putInt("curChoice", mCurCheckPosition); /****** This SaveInstanceState did well at changing the orientation before the tabs were added. * Is there a way to refresh the tabs on the new layout when the orientation changes? */ } @Override public void onListItemClick(ListView l, View v, int position, long id){ showDetails(position); } /* If Landscape, replace details. If not, replace titles, which includes the listview */ void showDetails(int position){ SherlockFragment newContent = new SherlockFragment(); FragmentManager fm = getFragmentManager(); FragmentTransaction ft = fm.beginTransaction(); if(mDualPane){ switch(position){ case 0: newContent = new Blue(); break; case 1: newContent = new Red(); break; case 2: newContent = new Green(); break; case 3: newContent = new Yellow(); break; case 4: newContent = new Purple(); break; } ft.replace(R.id.details, newContent); } else { switch(position){ case 0: newContent = new Blue(); break; case 1: newContent = new Red(); break; case 2: newContent = new Green(); break; case 3: newContent = new Yellow(); break; case 4: newContent = new Purple(); break; }ft.replace(R.id.titles, newContent); } ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE); ft.addToBackStack(null); ft.commit(); return; } /* Tab functionality */ @Override public void onTabSelected(Tab tab, FragmentTransaction ft) { mFragment = new TitlesFragment(); ft.replace(R.id.titles, mFragment); //ft.attach(mFragment); } @Override public void onTabUnselected(Tab tab, FragmentTransaction ft) { //ft.remove(mFragment); } @Override public void onTabReselected(Tab tab, FragmentTransaction ft) { // TODO Auto-generated method stub } } </code></pre> <p>Thanks for your help everyone! I apologize for having to leave after I post this, but I'll return after a few hours of sleep.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload