1

ここに同様の質問があることは知っていますが、ユースケースで何をすべきかを知る必要があります。
エラーが発生しました

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

4

1 に答える 1

0

あなたはこれをすべて間違っています。

Fragments のドキュメント、特に「アクティビティとの通信」セクションを読む時間をとることを強くお勧めします。Application クラスの使用方法には意味がありません。

クラス名が存在し、パブリックであり、パブリックである空のコンストラクターがあることを確認してください

空のパブリック コンストラクターが表示されません。内部クラスを静的として宣言しないと、Activity への参照が保持され、リークやその他の問題が発生します。

SectionsScreen独自のファイルにリファクタリングするか、適切なコールバック パターンを使用することで、この全体を回避できます。

于 2013-06-27T11:44:59.117 に答える