【问题标题】:Android ListView header in ListFragment crashes on back button pressListFragment 中的 Android ListView 标头在按下后退按钮时崩溃
【发布时间】:2014-08-29 23:17:30
【问题描述】:

我有一个 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);
}

当我第一次创建片段时,这很好用。然后,当我在按下后退按钮时导航回此片段时,在 Android main](Suspended(exception 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)

【问题讨论】:

    标签: android listview android-fragments


    【解决方案1】:

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

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

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

    当我按下后退按钮时摆脱适配器并重新创建它。这符合文档中关于

    【讨论】:

      【解决方案2】:

      您是在模拟器还是真实设备中尝试。 我在使用某种不同类型的代码时遇到了同样的问题,并且 logcat 没有显示相关信息,这对我有用:- Window > Preferences > Android > Logcat > (Monitor Logcat Messages from applications in workspace) 将其更改为 VERBOSE。

      【讨论】:

      • 它在模拟器上。真实设备和模拟器上的 Kitkat 都很好。我发现这篇很好的帖子回答了我的问题:code.neenbedankt.com/note-to-self-listfragment-and-header-views
      • 我想我以某种方式回答了这个问题,因为这个问题包含一个段落:- 当我第一次创建片段时,这很好用。然后,当我导航回到“logcat 什么都没说”时,我得到的只是 Thread[ main](Suspended(exception IllegalStateException)) 和 public void addHeaderView(View v, Object dad , boolean isSelectable) 我的回答是在详细模式下运行 logcat,然后会显示错误。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-13
      相关资源
      最近更新 更多