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

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

问题描述

我有3个片段,一是NavigationDrawer一MapFragment,和一个用户自定义MapInfoFragment。我想MapInfoFragment出现半透明在某些事件MapFragment的顶部,并消失在其他人。我真的不关心,如果我完全删除片段,并创建一个新的每一次,还是我只是改变显示的可见性和信息。目前,我只是想用FragmentManager的.hide()函数,但我也试着卸下摆臂和.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>

犯规code:

Offending Code:

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

更新1

我改变了 map_info_layout map_info 键,有一个在我收到的错误没有变化。我想previously了 map_info_layout 定义为ID为MapInfoFragment的布局XML的根目录下的FrameLayout,所以我觉得 map_info_layout map_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。,因为我不感兴趣的年长设备的兼容性。由于Fllo,我决不会想通了这一点,如果它仍然指向了错误的ID。

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 to Fllo, I never would have figured that out if it was still pointing to the wrong id.

推荐答案

我知道你已经解决了这个事情,但我想解释这个错误发生,因为它只是发生在我身上。

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

基本上你调用隐藏()删除()等,用一个空值。

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.

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

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