ListFragment中的Android ListView标题在按下后退按钮时崩溃

时间:2014-07-09 03:46:25

标签: android listview android-fragments

我有一个ListFragment类:

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

第一次创建Fragment时,这很好用。然后我在按下后退按钮时导航回此片段,在Android&lt; 4.4它崩溃了。 logcat没有说什么,我得到的只是Thread [&lt; 1&gt; main](Suspended(异常IllegalStateException))和上面的文档中断public void addHeaderView(View v,Object dad,boolean isSelectable)说:

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

当然,我在KitKat中没有任何问题,但其他任何事情都会崩溃。

帮助?

编辑: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)

2 个答案:

答案 0 :(得分:2)

所以我找到了问题的答案:http://code.neenbedankt.com/note-to-self-listfragment-and-header-views/

当我离开时,ListView永远不会被破坏。所以我必须这样称呼:

@Override
public void onDestroyView() {
    super.onDestroyView();
    setListAdapter(null);
}

去掉适配器并在按下后退按钮时重新创建它。这符合文档中关于&lt; 4.4的说法,但是当我离开时,我不知道如何摆脱这些东西。

答案 1 :(得分:0)

您是在模拟器还是真实设备中尝试它? 我有一些不同类型的代码有同样的问题,而logcat没有显示相关信息,这对我有用: - 窗口&gt;偏好&gt; Android&gt; Logcat&gt; (从工作区中的应用程序监视Logcat消息)将其更改为VERBOSE。