Android : Android ListView header in ListFragment crashes on back button press

on Tuesday, July 8, 2014


I have a ListFragment class:



@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
inflater.inflate(R.layout.fragment_information, container);
listAdapter = new ArrayAdapter<String>(inflater.getContext(),
android.R.layout.simple_list_item_1, new String[] {
"See the Demo!", "Read More!" });
header = inflater.inflate(R.layout.information_headerview, null);
return super.onCreateView(inflater, container, savedInstanceState);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
TextView tv = (TextView) header.findViewById(R.id.information_header_textview);
tv.setText(Model.Information);
getListView().addHeaderView(header);
setListAdapter(listAdapter);
}


This works fine when I create the Fragment for the first time. Then I when I navigate back to this fragment on back button press, in Android < 4.4 it crashes. the logcat doesnt say anything, all I get is Thread[<1> main](Suspended(exception IllegalStateException)) and a break at documentation above public void addHeaderView(View v, Object dad, boolean isSelectable) saying:



/**
* Add a fixed view to appear at the top of the list. If this method is
* called more than once, the views will appear in the order they were
* added. Views added using this call can take focus if they want.
* <p>
* Note: When first introduced, this method could only be called before
* setting the adapter with {@link #setAdapter(ListAdapter)}. Starting with
* {@link android.os.Build.VERSION_CODES#KITKAT}, this method may be
* called at any time. If the ListView's adapter does not extend
* {@link HeaderViewListAdapter}, it will be wrapped with a supporting
* instance of {@link WrapperListAdapter}.
*
* @param v The view to add.
* @param data Data to associate with this view
* @param isSelectable whether the item is selectable
*/


Of course, I have no problem in KitKat, but anything else crashes.


Help?


edit: logcat:



07-09 03:46:45.000: E/AndroidRuntime(716): FATAL EXCEPTION: main
07-09 03:46:45.000: E/AndroidRuntime(716): java.lang.IllegalStateException: Cannot add header view to list -- setAdapter has already been called.
07-09 03:46:45.000: E/AndroidRuntime(716): at android.widget.ListView.addHeaderView(ListView.java:255)
07-09 03:46:45.000: E/AndroidRuntime(716): at fragments.InformationFragment.onViewCreated(InformationFragment.java:53)
07-09 03:46:45.000: E/AndroidRuntime(716): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:842)
07-09 03:46:45.000: E/AndroidRuntime(716): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1032)
07-09 03:46:45.000: E/AndroidRuntime(716): at android.app.BackStackRecord.popFromBackStack(BackStackRecord.java:697)
07-09 03:46:45.000: E/AndroidRuntime(716): at android.app.FragmentManagerImpl.popBackStackState(FragmentManager.java:1431)
07-09 03:46:45.000: E/AndroidRuntime(716): at android.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:453)
07-09 03:46:45.000: E/AndroidRuntime(716): at android.app.Activity.onBackPressed(Activity.java:2121)
07-09 03:46:45.000: E/AndroidRuntime(716): at com.allgoodpeopleus.evolutionp.MainActivity.onBackPressed(MainActivity.java:82)
07-09 03:46:45.000: E/AndroidRuntime(716): at android.app.Activity.onKeyUp(Activity.java:2099)
07-09 03:46:45.000: E/AndroidRuntime(716): at android.view.KeyEvent.dispatch(KeyEvent.java:2575)
07-09 03:46:45.000: E/AndroidRuntime(716): at android.app.Activity.dispatchKeyEvent(Activity.java:2329)
07-09 03:46:45.000: E/AndroidRuntime(716): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1806)
07-09 03:46:45.000: E/AndroidRuntime(716): at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3327)
07-09 03:46:45.000: E/AndroidRuntime(716): at android.view.ViewRootImpl.handleFinishedEvent(ViewRootImpl.java:3300)
07-09 03:46:45.000: E/AndroidRuntime(716): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2460)
07-09 03:46:45.000: E/AndroidRuntime(716): at android.os.Handler.dispatchMessage(Handler.java:99)
07-09 03:46:45.000: E/AndroidRuntime(716): at android.os.Looper.loop(Looper.java:137)
07-09 03:46:45.000: E/AndroidRuntime(716): at android.app.ActivityThread.main(ActivityThread.java:4424)
07-09 03:46:45.000: E/AndroidRuntime(716): at java.lang.reflect.Method.invokeNative(Native Method)
07-09 03:46:45.000: E/AndroidRuntime(716): at java.lang.reflect.Method.invoke(Method.java:511)
07-09 03:46:45.000: E/AndroidRuntime(716): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-09 03:46:45.000: E/AndroidRuntime(716): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-09 03:46:45.000: E/AndroidRuntime(716): at dalvik.system.NativeStart.main(Native Method)

0 comments:

Post a Comment