【发布时间】:2023-04-08 07:34:01
【问题描述】:
我知道这里有类似的问题,但我需要知道在我的用例中该怎么做。
我得到了错误
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 吗?
【问题讨论】:
标签: android static android-fragments screen-orientation