值后的onActivityResult设置为null [英] values are set to null after onActivityResult
问题描述
我有一个自定义XML布局的 DialogFragment
类。
在onCreateView()我得到引用一个按钮。你点击它来选择文件。
当它被点击两件事情发生:
1)它采用了充气布局添加另一个XML布局到当前
2)初始化三个变量的值的ListView
, ListArray
, SimpleAdapter
,而这些都是在类的顶部声明成员变量。
3)然后调用 getActivity()。startActivityForResult()
I have a DialogFragment
class with a custom xml layout.
In onCreateView() I get reference to a button. You click it to choose file.
When it's clicked two things happen:
1) It uses a layout inflater to add another xml layout into the current one
2) It initializes three variables to their values ListView
, ListArray
, SimpleAdapter
, and these are member variables declared at the top of the class.
3) Then it calls getActivity().startActivityForResult()
问题:当我回来到这个活动到dialogfragment上述三个变量设置为空
Problem: The three variables described above are set to null when I come back into this activity to the dialogfragment.
下面是发生了什么事的详细的版本:
Here is a detailed version of what's happening:
//before calling startActivityForResult()
I/NULLTEST SIMPLEADAPTER﹕ android.widget.SimpleAdapter@64cf1348
I/NULLTEST ATTACHMENTSLISTVIEW﹕ android.widget.ListView{64d1c2e0 VFED.VC. ......I. 0,0-0,0 #7f090014 app:id/attachmentsListView}
I/NULLTEST ATTACHMENTSLISTARRAY﹕ [{a_date_added=Added: Nov 1, a_name=My test file 0}, {a_date_added=Added: Nov 2, a_name=My test file 1}, {a_date_added=Added: Nov 3, a_name=My test file 2}]
// Inside onActivityResult()
I/NULLTEST SIMPLEADAPTER ONRESULT﹕ null
I/NULLTEST ATTACHMENTSLISTVIEW ONRESULT﹕ null
I/NULLTEST ATTACHMENTSLISTARRAY ONRESULT﹕ []
这整个code是被称为在 DialogFragment
。正如你可以在上面看到之前调用 startActivityForResult()
三个变量 SimpleAdapter
,的ListView
和的ArrayList
设置为一定的价值。他们是所有成员变量因此可以在这个类的任何地方调用。
在 DialogFragment
的 onCreateView()
方法,我得到参考对话框的按钮部分并附加 setOnClickListener()
的按钮。点击该按钮后,上述三个变量初始化为他们的价值观和我使用 Log.i
来记录值。按钮的onClick()
又叫 getActivity()startActivityForResult();
让我选择一个文件,从而打开一个完全不同的活动
。我选择文件和主机的的onActivityResult()
方法活动
被称为这对于我而言是 MainActivity.java
。从这里将数据传递到 DialogFragment
这样:
This whole code is being called inside a DialogFragment
. As you can see above before calling startActivityForResult()
the three variables SimpleAdapter
, ListView
, and ArrayList
are set to some value. They are all member variables thus can be called anywhere in this class.
Inside onCreateView()
method of DialogFragment
I get reference to a button part of the dialog and attach a setOnClickListener()
to the button. When this button is clicked, the three variables described above are initialized to their values and I use Log.i
to log the values. The button onClick()
also calls the getActivity().startActivityForResult();
to allow me to choose a file thus opening a completely different Activity
. I choose the file and the onActivityResult()
method of the host Activity
is called which for my case is MainActivity.java
. From here the data is passed onto the DialogFragment
as such:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
现在我有内部访问数据的 DialogFragment
而在的onActivityResult
的 DialogFragment
不是活动
,三个变量上面所述设置为null,但他们只在按钮上点击初始化,但是这将要求 startActivityForResult()
等。我不明白他们为什么重新设置为NULL。我失去了一些重要的事情吗?由于这个我不能说,例如 simpleAdapter.notifyDataSetChanged()
。更新我的列表视图的这种具体的方式工作,如果我这样做,在活动
,而不是 DialogFragment
,但我需要做的这里面一个 DialogFragment
。同时检索从另一个活动的数据后更新列表视图中的任何其他方式都欢迎。我只是需要一种方式,当我选择更多的数据来更新列表视图。
Now I have access to the data inside the DialogFragment
but in the onActivityResult
of the DialogFragment
not the Activity
, the three variables described above are set to null but they are only initialized on the button click but that would call startActivityForResult()
and so on. I do not understand why they are set back to null. Am I missing something important here? Due to this I cannot call for example simpleAdapter.notifyDataSetChanged()
. This exact way of updating my listview works if I do this in an Activity
instead of a DialogFragment
but I need to do this inside a DialogFragment
. Also any other ways of updating listview after retrieving data from another activity are welcome. I just need a way to update that listview when I select more data.
最后更新
我想道歉,为愚蠢的,我是做的高度。在我的托管活动
是 MainActivity.java
,当此活动
会叫的onActivityResult()
我要创建对话框片段作为这样的一个新实例:新MyDialogFragment()的onActivityResult()
,显然这就是为什么没有你的家伙方法工作作为 onCreateView
不叫这个时候。我有零钱新MyDialogFragment()
到previously初始化,我实际显示一切工作现在对话片段。我将关闭这个问题。
I would like to apologize to for the height of stupidity that I was making. In my hosting Activity
which is MainActivity.java
, when this Activity
would call onActivityResult()
I would create a new instance of the dialog fragment as such: new MyDialogFragment().onActivityResult()
and obviously this is why none of your guys methods worked as onCreateView
wasn't called this time. I have change new MyDialogFragment()
to the previously initialized dialog fragment that I am actually displaying and everything works now. And I will close this question.
推荐答案
检查是否请求code是正确code来回请求。你可以要求另一code
Check if the request code is the correct code fro request. You may requesting another code
这篇关于值后的onActivityResult设置为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!