読者です 読者をやめる 読者になる 読者になる

うさがにっき

読書感想文とプログラムのこと書いてきます

onCreate()内でFragmentのメソッドを呼び出すときの注意点

概要

onCreate()内でFragmentのメソッドを操作するとFragmentが生成されておらず、NullPointerExceptionが発生する場合がある
その対応策をまとめる

詳細

intentからわたされた文字列をFragmentのTextViewに設定したいとする

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        FragmentManager manager = getSupportFragmentManager();
        PlaceholderFragment f = (PlaceholderFragment)manager.findFragmentByTag("main");

        if(f == null) {
            f = new PlaceholderFragment();
            manager.beginTransaction().replace(android.R.id.content, f, "main");
        }

        Intent intent = getIntent();
        String text = intent.getStringExtra("text");

        f.setText(text);

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {
        TextView mTextView;

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }

        @Override
        public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);

            mTextView = (TextView)view.findViewById(R.id.text);
        }

        public void setText(String text) {
            mTextView.setText(text);
        }
    }
}

onCreate内で、FragmentTransactionのcommit()を実行しているが、この時点ではonViewCreate()は呼ばれていない
なのでmTextViewはnullなので、nullPointerExceptionとなる

FragmentのsetArgments()を使い、その後onViewCreated()が呼ばれた時、mTextViewに文字列をセットする
ただし、setTextを使って文字を変えた後、Argumentsの値も書き換えておかないとシステムによって再生成された後最初の文字が表示されてしまうので注意

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = getIntent();
        String text = intent.getStringExtra("text");
        
        FragmentManager manager = getSupportFragmentManager();
        PlaceholderFragment f = (PlaceholderFragment)manager.findFragmentByTag("main");

        if(f == null) {
            f = new PlaceholderFragment();
            // Argumentsを介して値を設定
            Bundle args = new Bundle();
            args.putString("text", text);
            f.setArguments(args);
            
            manager.beginTransaction().replace(android.R.id.content, f, "main");
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {
        TextView mTextView;

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container, false);
            return rootView;
        }

        @Override
        public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);

            mTextView = (TextView)view.findViewById(R.id.text);
            
            String text = getArguments().getString("text", "");
            mTextView.setText(text);
        }

        public void setText(String text) {
            mTextView.setText(text);
            
            // デフォルト値も更新
            Bundle args = new Bundle();
            args.putString("text", text);
        }
    }
}