0

I'm new to android (not completely new to java). Trying to follow along on a sorta dated tutorial. I can't figure out why setOnClickListener is crashing this app. I know that there is a null pointer exception somewhere, but I'm not sure how to fix it. Also, sidenote, why does my Class extend ActionBarActivity instead of Activity like in all of the examples I've seen so far?

public class StartingPoint extends ActionBarActivity {

    int counter;
    Button add, sub;
    TextView display;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_starting_point);

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

        counter = 0;

        add = (Button) findViewById(R.id.bAdd);
        sub = (Button) findViewById(R.id.bSub);
        display = (TextView) findViewById(R.id.tvDisplay);

        add.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                counter++;
                display.setText("Counter equals" + counter);

            }
        });


    } // 



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.starting_point, 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_starting_point,
                    container, false);
            return rootView;
        }
    }
}
rahul pasricha
  • 931
  • 1
  • 14
  • 35

2 Answers2

0

Your buttons and textview are in fragment layout, not in activity layout so you have to move your code to onCreateView:

public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    int counter=0;

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

        Button add = (Button) rootView.findViewById(R.id.bAdd);
        Button sub = (Button) rootView.findViewById(R.id.bSub);
        TextView display = (TextView) rootView.findViewById(R.id.tvDisplay);

        add.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                counter++;
                display.setText("Counter equals" + counter);
            }
        });

        return rootView;
    }
}

Also, you have to clear your onCreate, just leave:

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

    if (savedInstanceState == null) {
         getSupportFragmentManager().beginTransaction()
          .add(R.id.container, new PlaceholderFragment()).commit();
    }
}
Zoran
  • 1,484
  • 1
  • 10
  • 13
  • This works great with one tweak. Either you have to add final to display or declare display outside of onCreateView. Thanks for the quick response. – user3781123 Jun 26 '14 at 23:40
  • Yes, you are right. You have to add final. I updated answer. Thanks. Do i deserve answer to be accepted? – Zoran Jun 27 '14 at 05:05
0

My psychic powers tell me that your "add" and "sub" buttons are part of the fragment's layout file, not the Activity layout file (which just includes the fragment).

Hence, move this block of code:

    add = (Button) findViewById(R.id.bAdd);
    sub = (Button) findViewById(R.id.bSub);
    display = (TextView) findViewById(R.id.tvDisplay);
    add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            counter++;
            display.setText("Counter equals" + counter);
        }});

to the onCreateView method of PlaceholderFragment.

Alternatively, you can have the layout of the Activity contain all your controls and just get rid of the placeholder fragment.

<opinion>Fragments have their advantages, but the boiler plate PlaceholderFragment generated by eclipse can be annoying, especially for a simple prototyping exercise. </opinion>

selbie
  • 100,020
  • 15
  • 103
  • 173