Java 在静态方法中访问 getActivity()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22990158/
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
Access getActivity() inside static method
提问by heisenberg
I have this class here which calls the method setPoint
我在这里有这个类,它调用方法 setPoint
public class PointsList extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.listpoints, container, false);
public static class PointCreation extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.point_creation, container, false);
setPoint(view, CREATE);
return view;
}
}
static final void setPoint(View view, int goal) {
final EditText SerialField = (EditText) view.findViewById(R.id.Serial);
if(goal == CREATE) {
Button buttonGuardar = (Button) view.findViewById(R.id.buttonGuardar);
buttonGuardar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String Serial = SerialField.getText().toString();
pointsList.add(new Serial);
//go back to R.layout.listpoints
}
});
}
}
My goal is after I click the button to add the new Serial to the List, I can go back to the previous menu from
我的目标是在单击按钮将新序列添加到列表后,我可以从
R.layout.point_creation to R.layout.listpoints
To move around fragments I generally use something like this:
为了移动片段,我通常使用这样的东西:
Fragment fragment = new PointsList();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, fragment)
.commit();
But inside:
但里面:
static final void setPoint(View view, int goal)
getActivity().getSupportFragmentManager();
cannot be referenced from a static context, and I don't know how to go around it with making the static class non-static? I've some global flags which I use in the static Classes (have 2 of them) that would be a bit painfull to export since
不能从静态上下文中引用,我不知道如何通过使静态类变为非静态来解决它?我在静态类中使用了一些全局标志(其中有 2 个),因为导出会有点痛苦
public class PointCreation(int something) extends Fragment
is something I can't do.
是我做不到的。
采纳答案by Vladimir Petrakovich
You can get the activity from view:
您可以从视图中获取活动:
Activity activity = (Activity)view.getContext()
If you use FragmentActivity (it seems to be so), then cast Context to FragmentActivity (instead of regular Activity) and further you will able to call getSupportFragmentManager()
如果您使用 FragmentActivity(似乎是这样),则将 Context 转换为 FragmentActivity(而不是常规 Activity),然后您将能够进一步调用 getSupportFragmentManager()
FragmentActivity activity = (FragmentActivity)view.getContext();
FragmentManager manager = activity.getSupportFragmentManager();
回答by OFFmind
You can't reference from a static to non-static objects. First thing, that comes to mind is to use singleton pattern for your fragment. In other words add to you fragment singleton snippet:
您不能从静态对象引用到非静态对象。首先,想到的是为您的片段使用单例模式。换句话说,向您添加片段单例片段:
static PointsList instance;
public PointsList getInstace(){
if(instance == null){
instance = new PointsList ();
}
return instance;
}
and in your fragment onCreate
method assign it to the instance:
并在您的片段onCreate
方法中将其分配给实例:
instance = this;
after that you can remove staticmodifier from setPoint method. And call it from any part of your project like PointsList.getInstance().setPoint();
之后,您可以从 setPoint 方法中删除静态修饰符。并从项目的任何部分调用它,例如PointsList.getInstance().setPoint();
p.s. what goals from static you have to use? You should use static
very carefully, many things can be done through singleton instead of using statics.
ps 你必须使用静态的什么目标?你应该static
非常小心地使用,很多事情可以通过单例来完成,而不是使用静态。
回答by Vishal Vaishnav
You can use by below code;
您可以通过以下代码使用;
private static FragmentActivity myContext;
@Override
public void onAttach(Activity activity) {
myContext = (FragmentActivity) activity;
super.onAttach(activity);
}
You can use myContext
as Context
您可以myContext
用作Context