从DialogFragment访问以托管Activity的视图

时间:2018-12-26 15:32:13

标签: android android-dialogfragment

我遇到了这样的问题。在我的应用程序中,我有一个活动(MobileActivity),其中包含一些视图(Spinners,TextViews和Button)。当用户单击按钮时,我需要打开一个对话框(ConfirmDialog)。在此对话框中,我需要从MobileActivity的视图中显示一些数据(请参见下面的代码)。有时在Play控制台中,我会看到带有NullPointerException的崩溃报告(我在代码中标记了该行)。 问题出在哪里?

谢谢!

我认为,问题可能出在Activity的生命周期方法中。我尝试了以下操作:

  • 在模拟器上启动我的应用,然后打开DialogFragment
  • 按下主页按钮
  • 在adb中,杀死了我的进程(adb kill)
  • 从后台应用重新打开我的应用

它没有崩溃。那么,问题不在后台吗?

来自MobileActivity的

代码,它将打开我的DialogFragment

ConfirmDialog newFragment = new ConfirmDialog();
newFragment.show(getFragmentManager(), "Confirmation"); 

来自ConfirmDialog的代码

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        // Get the layout inflater
        LayoutInflater inflater = getActivity().getLayoutInflater();
            View v = null;
        Spinner s;
            v = inflater.inflate(R.layout.confirm_mobile_layout, null);
        builder.setView(v);
        //next row marked in stacktrace as problem row
        //Spinner with Id=acn_debit_mobile is placed on MobileActivity
        s = getActivity().findViewById(R.id.acn_debit_mobile);         
        Spinner b = getActivity().findViewById(R.id.biller_mobile);
            //other code
            return builder.create();
 }

以及Play控制台中的stacktrace

java.lang.RuntimeException: 
 at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2814)
 at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2892)
 at android.app.ActivityThread.handleRelaunchActivity (ActivityThread.java:4763)
 at android.app.ActivityThread.-wrap18 (Unknown Source)
 at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1621)
 at android.os.Handler.dispatchMessage (Handler.java:106)
 at android.os.Looper.loop (Looper.java:171)
 at android.app.ActivityThread.main (ActivityThread.java:6635)
 at java.lang.reflect.Method.invoke (Native Method)
 at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:547)
 at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:823)

Caused by: java.lang.NullPointerException: 
 at ua.grant.smart.ConfirmDialog.onCreateDialog (ConfirmDialog.java:121)
 at android.app.DialogFragment.onGetLayoutInflater (DialogFragment.java:406)
 at android.app.Fragment.performGetLayoutInflater (Fragment.java:1325)
 at android.app.FragmentManagerImpl.moveToState (FragmentManager.java:1279)
 at android.app.FragmentManagerImpl.moveFragmentToExpectedState (FragmentManager.java:1562)
 at android.app.FragmentManagerImpl.moveToState (FragmentManager.java:1623)
 at android.app.FragmentManagerImpl.dispatchMoveToState (FragmentManager.java:3032)
 at android.app.FragmentManagerImpl.dispatchActivityCreated (FragmentManager.java:2984)
 at android.app.FragmentController.dispatchActivityCreated (FragmentController.java:178)
 at android.app.Activity.performCreate (Activity.java:7090)
 at android.app.Activity.performCreate (Activity.java:7075)
 at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1215)
 at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2767)

我添加了onAttach和onDetach方法来存储对我的主机Activity的引用(下面的代码),并在ConfirmDialog类的所有位置将getActivity()替换为mActivity。会解决这个问题吗?

mActivity Activity;

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    mActivity = activity;
}

@Override
public void onDetach() {
    super.onDetach();
    mActivity = null;
}

1 个答案:

答案 0 :(得分:1)

保持活动实例并在片段分离时销毁它是避免NullPointerException

的好方法
mActivity Activity;

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    mActivity = activity;
}

@Override
public void onDetach() {
    super.onDetach();
    mActivity = null;
}

但是您可能需要检查两件事。

  1. 您为什么不对片段使用相同的方法,mActivityonCreateDialog中的位置。顺便说一句,在进行任何交易之前,请检查mActivity的null。

enter image description here

  1. 使用yourDialogFragment.show()明确运行此功能:

enter image description here

ft.commit()安排此事务的提交。提交不会立即发生。它将在下一次线程就绪时安排为主线程上的工作进行安排。因此,我们无法保证任何事情,例如,它可能导致IllegalStateException

怎么样:commitAllowingStateLoss()。但是在使用前请仔细阅读文档。看看this

  1. 您为什么不扩大onCreateView上的视图? (您不必在onCreateDialog中这样做)

enter image description here