Android 尝试从视图中删除片段在 mNextAnim 上给了我 NullPointerException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22489703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Trying to remove fragment from view gives me NullPointerException on mNextAnim
提问by Koeber
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.
我有 3 个片段,一个 NavigationDrawer,一个 MapFragment 和一个用户定义的“MapInfoFragment”。我希望“MapInfoFragment”在某些事件上在 MapFragment 顶部显示为半透明,而在其他事件上消失。我真的不在乎我是否完全删除片段并每次创建一个新片段,或者我只是更改显示的可见性和信息。目前我只是尝试使用 FragmentManager 的 .hide() 函数,但我也尝试过 .remove 和 .detach ,结果相似。
Error:
错误:
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.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>
Offending Code:
违规代码:
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.hide(getFragmentManager().findFragmentById(R.id.map_info));
ft.commit();
Update 1
更新 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.
我更改map_info_layout
为map_info
并且我收到的错误没有变化。我之前已经map_info_layout
定义为 MapInfoFragment 布局 xml 的根 FrameLayout 的 id,所以我认为map_info_layout
并且map_info
指向同一件事。
Update 2: My solution
更新 2:我的解决方案
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.
原来我使用的是MapInfoFragment
扩展android.support.v4.app.Fragment
而不是. 由于我对旧设备的兼容性不感兴趣,因此将所有内容都切换到了。谢谢弗洛。android.app.Fragment
android.app.FragmentTransaction
android.support.v4.app.FragmentTransaction
android.app.
Update 3: General solution
更新 3:通用解决方案
Stop trying to hide null objects
停止尝试隐藏空对象
回答by gak
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.
基本上,您使用空值调用hide()
,remove()
等。
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.
错误并不明显,发生这种情况时堆栈跟踪也不是来自您自己的来源,因此意识到它是什么并不容易。
回答by Christine
It also happens when you call FragmentManager.popBackstack()
without there being a fragment in the backstack.
当您FragmentManager.popBackstack()
在后台堆栈中没有片段的情况下调用时,也会发生这种情况。
回答by howerknea
this is because you add or show or hide a fragment which is null now,try to check if it is null ,before you use it!
这是因为您现在添加或显示或隐藏一个为空的片段,请在使用之前尝试检查它是否为空!
回答by flobacca
My problem was that FragmentManager's getFragments() sometimes returns a list where some fragments are null, and I was removing those null fragments.
我的问题是 FragmentManager 的 getFragments() 有时会返回一个列表,其中某些片段为空,而我正在删除这些空片段。
回答by Muhammad Etisam
Properly call show() and hide()...
正确调用 show() 和 hide()...
private void showFragment(int fragmentIndex, boolean addToBackStack) {
FragmentManager fm = getFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
for (int i = 0; i < fragments.length; i++) {
if (i == fragmentIndex) {
transaction.show(fragments[i]);
} else {
transaction.hide(fragments[i]);
}
}
if (addToBackStack) {
transaction.addToBackStack(null);
}
transaction.commit();
}
回答by Andrea Aranguren
It happended to me only when from one of the fragments in the navigation drawer I showed a DialogFragment by using
只有当我从导航抽屉中的一个片段中显示一个 DialogFragment 时,它才发生在我身上
newFragment.show(getFragmentManager(), "dialog");
If instead I used
如果我使用
newFragment.show(getChildFragmentManager(), "dialog");
this did not happened.
这没有发生。
NOTE: The crash did not happen to me when showing or closing the dialog, but when changing to another fragment from the navigation drawer.
注意:在显示或关闭对话框时我没有发生崩溃,但是当从导航抽屉更改为另一个片段时。