【发布时间】:2014-04-10 13:56:55
【问题描述】:
我这里有这个类,它调用 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
}
});
}
}
我的目标是在单击按钮将新序列添加到列表后,我可以从
返回上一个菜单R.layout.point_creation to R.layout.listpoints
为了在片段中移动,我通常使用这样的东西:
Fragment fragment = new PointsList();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, fragment)
.commit();
但在里面:
static final void setPoint(View view, int goal)
getActivity().getSupportFragmentManager();
无法从静态上下文中引用,我不知道如何通过使静态类变为非静态来解决它?我有一些在静态类中使用的全局标志(其中有 2 个),因为导出会有点痛苦
public class PointCreation(int something) extends Fragment
是我做不到的。
【问题讨论】:
-
要么使您的方法成为非静态方法,将其传递给 Activity 或 Context 或任何它需要的参数,或者静态缓存
singleton实例,您可以根据需要获取该实例。但是请记住,您不仅必须拥有对象,还必须在实际适当的时间调用它的方法,可能来自适当的线程等。 -
已经让一切都变成了非静态的,现在它就像一个魅力一样工作
标签: java android android-fragments android-fragmentactivity