我知道这里有类似的问题,但我需要知道在我的用例中该怎么做。
我得到了错误
make sure class name exists, is public, and has an empty constructor that is public
这是因为我inner Fragment Class
需要成为static
并返回一个实例。我有我的屏幕处理,setScreens() method
当我将它设置为静态时它会中断,如果我想从静态片段中调用它,我必须这样做。
因为我使用GlobalState
的是扩展应用程序,用于保存我的选择并且在访问时显示错误R.id.
我需要这个,因为我需要访问不同类中的数据!
这是主课的摘录,我稍后需要用 4 个片段来做。
//imports ...
public class MainScreen extends FragmentActivity{
GlobalState globalState;
String statute,section;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GlobalState globalState = (GlobalState)getApplication();
try {
statute = globalState.getStatute();
section = globalState.getSection();
}catch (NullPointerException e){}
setScreens();
}
public void setScreens(){
GlobalState globalState = (GlobalState)getApplication();
try {
statute = globalState.getStatute();
section = globalState.getSection();
}catch (NullPointerException e){e.printStackTrace();}
int i = getLowestSelection();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
//setting up for Landscape
//if (findViewById(R.id.fragtwo) != null) ...
//setting up for portrait
if (findViewById(R.id.fragone) != null) {
if (i == 0){
StatuteScreen statuteScreenFragment = new StatuteScreen(); //External Class, no problem
transaction.replace(R.id.fragone,statuteScreenFragment);
}
else if (i == 1){
SectionsScreen sectionsScreenFragment = new SectionsScreen();//Inner Class throws the error
transaction.replace(R.id.fragone,sectionsScreenFragment);
}
}
transaction.addToBackStack(null);
transaction.commit();
}
public class SectionsScreen extends Fragment{
String title;
ArrayList<DetailData> dataList;
ListView listView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.sections,container,false);
listView = (ListView)view.findViewById(R.id.lvSections);
DataBaseHelper dataBaseHelper = new DataBaseHelper(getActivity());
globalState = (GlobalState)getActivity(). getApplication();
try {
title = globalState.getStatute();
} catch (NullPointerException e){};
if(title != null){
dataList = dataBaseHelper.getDetailsNames(title);
dataBaseHelper.close();
listView.setAdapter(new SectionsAdapter(getActivity(),dataList));
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
globalState.setSection(dataList.get(i).getAbsch());
setScreens();
}
});
}
return view;
}
}
}
我怎样才能让它工作,所以它不会在方向改变时崩溃?我可以从静态方法获取Globalstate
和获取吗?R.id