Android:片段无法获得活动
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11743272/
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
Android: Fragment cannot get activity
提问by AndyAndroid
I have an activity which does a fragment transaction
我有一个进行片段交易的活动
DetailFragment newFragment = new DetailFragment();
transaction.replace(R.id.mylist, newFragment);
transaction.addToBackStack(null);
transaction.commit();
that works fine. Now I know within my activity a dynamic string I need to replace in the layout I have in newFragment. I thought that I can call after transaction.commit() just something like
这工作正常。现在我知道在我的活动中我需要在 newFragment 的布局中替换一个动态字符串。我以为我可以在 transaction.commit() 之后调用,就像
newFragment.setMyString("my dynamic value");
And in the newFragment.java I have
在 newFragment.java 我有
public void setMyString(String s)
{
TextView tv = (TextView) getActivity().findViewById(R.id.myobject);
tv.setText(s);
}
The point is that getActivity() returns null. How can I get the context I need to find my layout elements?
关键是 getActivity() 返回 null。如何获取查找布局元素所需的上下文?
Edit:
编辑:
I try to follow the route using a bundle as this seems to be the cleanest way. So I have changed my code:
我尝试使用捆绑包跟踪路线,因为这似乎是最干净的方式。所以我改变了我的代码:
Bundle b = new Bundle();
b.putString("text", "my dynamic Text");
DetailFragment newFragment = new DetailFragment();
transaction.replace(R.id.mylist, newFragment);
transaction.addToBackStack(null);
transaction.commit();
My fragment onCreateView looks like this:
我的 onCreateView 片段如下所示:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.mylayout, container, false);
TextView t = (TextView) v.findViewById(R.id.texttobereplaced);
t.setText(savedInstanceState.getString("text");
}
It seems that savedInstranceState is empty. Where should I find my bundle?
似乎savedInstanceState 是空的。我应该在哪里找到我的捆绑包?
Edit2:
编辑2:
missed the getArguments() in the reply. is working now.
错过了回复中的 getArguments()。现在正在工作。
采纳答案by Jason Robinson
Your Fragment
hasn't attached to the Activity
yet, which also means your layout hasn't been inflated yet (See the Fragment lifecycle). The best solution here would be to add your String
value as an argument to the Fragment
via the Fragment.setArguments(Bundle)
method. This can be retrieved in the receiving Fragment
via the Fragment.getArguments()
method.
您Fragment
尚未附加到Activity
尚未,这也意味着您的布局尚未膨胀(请参阅片段生命周期)。这里最好的解决方案是将您的String
值作为参数添加到Fragment
viaFragment.setArguments(Bundle)
方法。这可以Fragment
通过Fragment.getArguments()
方法在接收中检索。
回答by Alex Lockwood
Make sure you call getActivity()
in or after onAttach()
, as before then it will return null
.
确保你调用getActivity()
in 或 after onAttach()
,和之前一样,它会返回null
。
回答by Android2390
So i am also using the same fragment transaction manager and the only problem i can see with your code is that you define the TextView tv inside your onCreateView method of your newFragment class and then instantiate it as like this :
所以我也使用相同的片段事务管理器,我能看到你的代码的唯一问题是你在你的 newFragment 类的 onCreateView 方法中定义了 TextView tv,然后像这样实例化它:
public class AboutFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.newFragment, container, false);
TextView tv = (TextView) v.findViewById(R.id.textview);
return v;
}
public void setMyString(String s) {
tv.setText(s);
}
}
I know it does'nt make really much sense but that is how i am running my code and it works fine :)
我知道这并没有多大意义,但这就是我运行代码的方式,并且运行良好:)
回答by 8086 Eve
if u need to call getActivity in OnCreateView, move all ur code from onCreateView to onActivityCreate and only left lines
如果您需要在 OnCreateView 中调用 getActivity,请将您的所有代码从 onCreateView 移动到 onActivityCreate 并且只留下几行
View v = inflater.inflate(R.layout.xxxx, container, false);
return v;
in onCreateView
在 onCreateView
but i have a funny situation.
但我有一个有趣的情况。
in My onActivityCreated
在我的 onActivityCreated
Spinner s1 = (Spinner) getActivity().findViewById(R.id.bitlength);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
getActivity(), R.array.bitlengths,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s1.setAdapter(adapter);
Sometime the onActivityCreated wont run (correctly!?) and hence the spinner s1 become empty.
有时 onActivityCreated 不会运行(正确!?),因此微调器 s1 变空。