更新父片段的DialogFragment引发NullPointerException异常 [英] Updating parent Fragment from DialogFragment raises NullPointerException
问题描述
我挣扎了一会儿点击按钮的正后更新片段
从 DialogFragment
。在片段
以编程方式添加到活动
。当点击一个NullPointerException异常引发的对话框中积极的按钮,因为父片段
找不到。
(我不使用 SupportFragments
)
有什么建议?
父活动
:
公共类ResultActivity延伸活动{
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState); 如果(savedInstanceState == NULL){
。getFragmentManager()调用BeginTransaction()加(R.id.activity_result,ResultDetailsOneFragment.newInstance())提交()。
}
}
}
}
的'ResultActivity'的布局:
<?XML版本=1.0编码=UTF-8&GT?;
<的FrameLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:ID =@ + ID / activity_result
工具:上下文=com.myApp.view.ResultActivity
工具:忽略=MergeRootFrame/>
的'ResultDetailsOneFragment'的布局:
<?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
的xmlns:工具=http://schemas.android.com/tools
机器人:ID =@ + ID / fragment_result_details_one
工具:上下文=$ com.myApp.view.ResultActivity ResultDetailsOneFragment>
<! - ... - >
< / RelativeLayout的>
回调接口:
公共接口EditResultDialogListener {
无效onDialogFinish();
}
的 DialogFragment
:
公共类EditDescriptionDialog扩展DialogFragment {
@覆盖
保护无效onDialogPositiveButtonClick(DialogInterface对话,诠释它){
EditResultDialogListener resultDetailsOneFragment
=(EditResultDialogListener)getFragmentManager()findFragmentById(R.id.fragment_result_details_one)。 / *这里的NullPointerException异常* /
resultDetailsOneFragment.onDialogFinish();
}
}
您也越来越空指针异常,因为你正在努力寻找对话片段内的片段。
您需要做的是什么 - >
显示对话框时,设置的目的片段...
dialogFragment.setTargetFragment(ResultDetailsOneFragment.this,MY_REQUEST_ code);
dialogFragment.show(getFragmentManager(),标签);
然后在你的onDialogPositiveClick,
@覆盖
保护无效onDialogPositiveButtonClick(DialogInterface对话,诠释它){
//发送事件回父片段
getTargetFragment()的onActivityResult(MY_REQUEST_ code,Activity.RESULT_OK,NULL);
}
所以在对话框正按一下按钮,您的片段的onActivityResult会被调用。
确保你覆盖的onActivityResult在你的片段类。
I've struggled a while to update a Fragment
from a DialogFragment
after clicking the positive button. The Fragment
is added programmatically to an Activity
. When clicking the positive button in the dialog a NullPointerException raises, because the parent Fragment
could not be found.
(I don't use SupportFragments
.)
Any suggestions?
The parent Activity
:
public class ResultActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction().add(R.id.activity_result, ResultDetailsOneFragment.newInstance()).commit();
}
}
}
}
The layout of 'ResultActivity':
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_result"
tools:context="com.myApp.view.ResultActivity"
tools:ignore="MergeRootFrame" />
The layout of 'ResultDetailsOneFragment':
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment_result_details_one"
tools:context="com.myApp.view.ResultActivity$ResultDetailsOneFragment" >
<!-- ... -->
</RelativeLayout>
The interface for the callback:
public interface EditResultDialogListener {
void onDialogFinish();
}
The DialogFragment
:
public class EditDescriptionDialog extends DialogFragment {
@Override
protected void onDialogPositiveButtonClick(DialogInterface dialog, int which) {
EditResultDialogListener resultDetailsOneFragment
= (EditResultDialogListener) getFragmentManager().findFragmentById(R.id.fragment_result_details_one);
/* here's the NullPointerException */
resultDetailsOneFragment.onDialogFinish();
}
}
You are getting nullpointer exception because you are trying to find the fragment inside the dialog fragment.
What you need to do is ->
Set the target fragment when showing the dialog ...
dialogFragment.setTargetFragment(ResultDetailsOneFragment.this, MY_REQUEST_CODE);
dialogFragment.show(getFragmentManager(), "tag");
Then in your onDialogPositiveClick,
@Override
protected void onDialogPositiveButtonClick(DialogInterface dialog, int which) {
// Send event back to parent fragment
getTargetFragment().onActivityResult(MY_REQUEST_CODE, Activity.RESULT_OK, null);
}
So on dialog positive button click, the onActivityResult of your Fragment will be called. Make sure you override the onActivityResult in your fragment class.
这篇关于更新父片段的DialogFragment引发NullPointerException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!