尝试从视图中删除片段在 mNextAnim 上给了我 NullPointerException [英] Trying to remove fragment from view gives me NullPointerException on mNextAnim

查看:25
本文介绍了尝试从视图中删除片段在 mNextAnim 上给了我 NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 3 个片段,一个 NavigationDrawer、一个 MapFragment 和一个用户定义的MapInfoFragment".我希望MapInfoFragment"在某些事件的 MapFragment 顶部显示为半透明,而在其他事件中消失.我真的不在乎我是否完全删除片段并每次创建一个新片段,或者我只是更改可见性和显示的信息.目前我只是尝试使用 FragmentManager 的 .hide() 函数,但我也尝试过 .remove 和 .detach ,结果相似.

I've got 3 fragments, one NavigationDrawer, one MapFragment, and one user-defined "MapInfoFragment". I want the "MapInfoFragment" to appear semi-transparent over top of the MapFragment on certain events and disappear on others. I don't really care if I completely remove the fragment and create a new one each time, or I just change the visibility and information displayed. Currently I'm just trying to use FragmentManager's .hide() function, but I've also tried .remove and .detach with similar results.

错误:

03-18 14:28:10.965  24711-24711/com.[packageName].asTest D/AndroidRuntime﹕ Shutting down VM
03-18 14:28:10.965  24711-24711/com.[packageName].asTest E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.[packageName].asTest, PID: 24711
java.lang.NullPointerException: Attempt to write to field 'int android.app.Fragment.mNextAnim' on a null object reference
        at android.app.BackStackRecord.run(BackStackRecord.java:658)
        at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
        at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5017)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)

Activity_main.xml:

Activity_main.xml:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.[packageName].asTest.MainActivity">

    <!-- As the main content view, the view below consumes the entire
         space available using match_parent in both dimensions. -->
    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <fragment xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:map="http://schemas.android.com/apk/res-auto"
            android:id="@+id/map"
            android:name="com.google.android.gms.maps.MapFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            map:cameraZoom="8"
            map:mapType="satellite"
            map:uiRotateGestures="false"
            android:layout_gravity="center_horizontal|top"
        />
    </FrameLayout>

    <fragment android:id="@+id/navigation_drawer"
        android:layout_width="@dimen/navigation_drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:name="com.[packageName].asTest.NavigationDrawerFragment"
        tools:layout="@layout/simple_list_item_1" />

    <fragment android:id="@+id/map_info"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:name="com.[packageName].asTest.MapInfoFragment" />




</android.support.v4.widget.DrawerLayout>

违规代码:

FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.hide(getFragmentManager().findFragmentById(R.id.map_info));
ft.commit();

更新 1

我将 map_info_layout 更改为 map_info 并且我得到的错误没有变化.我以前将 map_info_layout 定义为 MapInfoFragment 布局 xml 的根 FrameLayout 的 id,所以我认为 map_info_layoutmap_info 指向同样的事情.

Update 1

I changed map_info_layout to map_info and there's no change in the error I'm getting. I'd previously had map_info_layout defined as the id for the root FrameLayout of the MapInfoFragment's layout xml, so I think map_info_layout and map_info were pointing to the same thing.

结果我有 MapInfoFragment 扩展 android.support.v4.app.Fragment 而不是 android.app.Fragment 并且我是使用 android.app.FragmentTransaction 而不是 android.support.v4.app.FragmentTransaction.将所有内容都切换到 android.app.,因为我对旧设备的兼容性不感兴趣.谢谢弗洛.

Turned out that I had MapInfoFragment extending android.support.v4.app.Fragment instead of android.app.Fragment and I was using the android.app.FragmentTransaction instead of android.support.v4.app.FragmentTransaction. Switched everything to android.app. since I'm not interested in older device compatibility. Thanks Fllo.

停止尝试隐藏空对象

推荐答案

我知道你已经自己解决了这个问题,但我想解释一下这个错误是如何发生的,因为它刚刚发生在我身上.

I know you've solved this yourself, but I want to explain how this error happens, since it just happened to me.

基本上,您使用空值调用 hide()remove() 等.

Basically you're calling hide(), remove(), etc., with a null value.

错误并不明显,发生这种情况时堆栈跟踪也不是来自您自己的来源,因此了解它是什么并不容易.

The error isn't obvious, and the stack trace doesn't originate from your own sources when this happens, so it's not trivial to realise what it is.

这篇关于尝试从视图中删除片段在 mNextAnim 上给了我 NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆