gpt4 book ai didi

java - 永久删除 Android fragment

转载 作者:太空狗 更新时间:2023-10-29 15:09:01 25 4
gpt4 key购买 nike

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

尽管在 Stackoverflow 上仔细查看了类似问题,但我仍然无法在用户离开对话时删除 fragment 。

例如,假设他们在对话(和 fragment )A、B 和 C 中。当 B 关闭时,代码将屏幕更改为 fragment 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 );
}
}

最佳答案

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

FragmentManager fm = getActivity().getSupportFragmentManager();
fm.popBackStack("NameofBackstack",
FragmentManager.POP_BACK_STACK_INCLUSIVE);

关于java - 永久删除 Android fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18734518/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com