0

My App works only in onCreateOptionsMenu(Menu menu) method but not in OnCreate().

Why?

Here it is:

public class MainActivity extends ActionBarActivity {


Button button2;

TextView joke;

Random ran = new Random();


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);

    button2 = (Button) findViewById(R.id.b2);

    joke = (TextView) findViewById(R.id.joke);



    button2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            StringBuilder sb = new StringBuilder();

            ArrayList<Integer> al = new ArrayList<Integer>();
            for(int i = 1; i <= 49; i++)
                al.add(i);

            for(int i = 0; i < 6; i++) {
                int y = al.remove(ran.nextInt(al.size()) );
                sb.append("  " + y + "  ");

            }

            joke.setText(sb);
        }
    });

    return true;
}



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

    public PlaceholderFragment() {
    }

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

} 

So try to Cut & Paste the code inside onCreate() method... you will see the error :(

Vivek Warde
  • 1,936
  • 8
  • 47
  • 77
Tazos87
  • 1
  • 4
  • 1
    What error do you get? Are you sure you have the ids - `b1` and `b2` in the layout `activity_main`? – Gaurav Bhor Jun 08 '14 at 11:10
  • `onCreateOptionsMenu()` is invoked after the fragment transaction has been executed (in `onStart()`) and the fragment layout has become part of the activity view hierarchy. `onCreate()` is too early to find fragment views in the activity hierarchy. http://stackoverflow.com/questions/23653778/nullpointerexception-accessing-views-in-oncreate – laalto Jun 08 '14 at 11:51
  • @GauravBhor | No, there are only on fragment_main... The activity_main is like this: – Tazos87 Jun 09 '14 at 22:30

1 Answers1

1

You should write your code in onCreateView (PlaceholderFragment class):

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);
        button1 = (Button) rootView.findViewById(R.id.b1);
        //...
        return rootView;
    }
Volodymyr Kulyk
  • 6,455
  • 3
  • 36
  • 63