-2

I have a very simple android app being built. I have 2 buttons and 1 textview. I build them graphically in fragment_main.xml. Below is my full codes.

  public class MainActivity extends ActionBarActivity {
        TextView tvOut;
        Button btnOk;
        Button btnCancel;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.fragment_main);

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

            View.OnClickListener oclBtnOk = new View.OnClickListener() {
                public void onClick(View v) {
                    // change text of the TextView (tvOut)
                    tvOut.setText("Button OK clicked");
                }
            };
            btnOk.setOnClickListener(oclBtnOk);
        }


        @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);
            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();
            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 {

            public PlaceholderFragment() {
            }

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

    }

If I comment this part of the codes it works well.

View.OnClickListener oclBtnOk = new View.OnClickListener() {
                    public void onClick(View v) {
                        // change text of the TextView (tvOut)
                        tvOut.setText("Button OK clicked");
                    }
                };
                btnOk.setOnClickListener(oclBtnOk);

Even though I use setContentView(R.layout.main_activity); all my button and text view appears and quite puzzle why even with main_activity my buttons and text view appears well.

1 Answers1

1

You never assign values to your views. Use findViewById to get references to the views with the id's you gave them in your XML layout.

setContentView(R.layout.fragment_main);
// get the views
btnOk = (BUtton) findViewById(R.id.your_button_id);
tvOut = (TextView) findViewById(R.id.another_id);

btnOk.setOnClickListener(...);
Karakuri
  • 38,365
  • 12
  • 84
  • 104
  • Now which layout must I use the main_activity or the fragment_main? Confuse here. Secondly I put this btnOk = (Button) findViewById(R.id.button1); tvOut = (TextView) findViewById(R.id.textView1); yet it crashes. What else could be missing since its such a simple app I am also lost here. – user3754033 Jun 18 '14 at 20:17