我這裏有這個類調用該方法設定值訪問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