我这里有这个类调用该方法设定值访问getActivity()静态方法里面
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();
但我nside:
static final void setPoint(View view, int goal)
getActivity().getSupportFragmentManager();
不能从静态上下文中引用,我不知道如何绕过它使静态类非静态?我有我的静态类使用一些全局标志(其中有2个),这将是一个有点不好受出口,因为
public class PointCreation(int something) extends Fragment
是我做不到的。
要么让你的方法是非静态的,把它作为一个参数传递给一个Activity或Context或者它需要的东西,要么有一个静态缓存的singleton实例,你可以根据需要获得它。但请记住,您不仅必须拥有该对象,还必须在实际适当的时间调用它,方法可能来自适当的线程等。 –
已使所有内容都不是静态的,它现在像一个魅力 – heisenberg