ここに同様の質問があることは知っていますが、ユースケースで何をすべきかを知る必要があります。
エラーが発生しました
make sure class name exists, is public, and has an empty constructor that is public
これは、 myがインスタンスであり、インスタンスを返す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