3

我正在编写一个即时消息应用程序,它为每个对话使用一个片段,用户可以横向滑动以在对话之间移动。

尽管对 Stackoverflow 上的类似问题进行了深入研究,但当用户离开对话时,我仍然无法删除 Fragment。

例如,假设他们在对话(和片段)A、B 和 C 中。当 B 关闭时,代码会将屏幕更改为片段 A。这很好,但是当他们向右滚动以获取对话 C 时,应用程序崩溃与以下。

任何人都可以帮忙吗?Fragment 创建和删除代码的相关部分如下。谢谢你。

崩溃细节

>D/AndroidRuntime( 9677): Shutting down VM
W/dalvikvm( 9677): threadid=1: thread exiting with uncaught exception (group=0x41039300)
>E/AndroidRuntime( 9677): FATAL EXCEPTION: main
>E/AndroidRuntime( 9677): java.lang.NullPointerException
>E/AndroidRuntime( 9677):   at android.support.v4.app.Fragment.setUserVisibleHint(Fragment.java:819)
>E/AndroidRuntime( 9677):   at android.support.v4.app.FragmentPagerAdapter.setPrimaryItem(FragmentPagerAdapter.java:130)
>E/AndroidRuntime( 9677):   at android.support.v4.view.ViewPager.populate(ViewPager.java:1066)
>E/AndroidRuntime( 9677):   at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
>E/AndroidRuntime( 9677):   at android.support.v4.view.ViewPager$3.run(ViewPager.java:244)
>E/AndroidRuntime( 9677):   at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
>E/AndroidRuntime( 9677):   at android.view.Choreographer.doCallbacks(Choreographer.java:555)
>E/AndroidRuntime( 9677):   at android.view.Choreographer.doFrame(Choreographer.java:524)
>E/AndroidRuntime( 9677):   at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
>E/AndroidRuntime( 9677):   at android.os.Handler.handleCallback(Handler.java:615)
>E/AndroidRuntime( 9677):   at android.os.Handler.dispatchMessage(Handler.java:92)
>E/AndroidRuntime( 9677):   at android.os.Looper.loop(Looper.java:137)
>E/AndroidRuntime( 9677):   at android.app.ActivityThread.main(ActivityThread.java:4898)
>E/AndroidRuntime( 9677):   at java.lang.reflect.Method.invokeNative(Native Method)
>E/AndroidRuntime( 9677):   at java.lang.reflect.Method.invoke(Method.java:511)
>E/AndroidRuntime( 9677):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
>E/AndroidRuntime( 9677):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
>E/AndroidRuntime( 9677):   at dalvik.system.NativeStart.main(Native Method)

相关代码:

public class ChatActivity extends FragmentActivity
{
.... unrelated code...
private FragmentManager manager = getSupportFragmentManager();
private ConversationsAdapter adapter = new ConversationsAdapter( manager );
.... unrelated code...

class ConversationsAdapter extends FragmentPagerAdapter
{
        private List<Fragment> fragments;

        public ConversationsAdapter( FragmentManager manager ) {
                super( manager );
                fragments = new ArrayList<Fragment>();
        }

        public void addItem()
        {
                Fragment item = new ConversationFragment();
                fragments.add( item );
                adapter.notifyDataSetChanged();
        }

        public void removeItem( int position ) {
                Fragment item = adapter.getItem( position );

                FragmentTransaction transaction = manager.beginTransaction();
                transaction.setTransition( FragmentTransaction.TRANSIT_FRAGMENT_FADE );
                transaction.remove( item );
                transaction.commit();

                fragments.remove( position );
                adapter.notifyDataSetChanged();

                ViewPager pager = (ViewPager) findViewById( R.id.viewpager );
                pager.setCurrentItem( position - 1 );
        }
}
4

1 回答 1

0

您可以使用以下代码以编程方式删除片段

FragmentManager fm = getActivity().getSupportFragmentManager();
fm.popBackStack("NameofBackstack",
FragmentManager.POP_BACK_STACK_INCLUSIVE);
于 2014-05-23T09:56:00.523 回答