-2

So I have This Code, now after i added a smiple action to the button add(setOnClickListener) when I run the app it immidiatley crashes... any idea why?

public class MainActivity extends ActionBarActivity {

int counter;
Button add,sub;
TextView display;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    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) {
            // TODO Auto-generated method stub
            counter++;
            display.setText("Your Total is " + counter);
        }
    });

    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);
    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;
    }
}

}

Lal
  • 14,726
  • 4
  • 45
  • 70
Golan Kiviti
  • 3,895
  • 7
  • 38
  • 63
  • 1
    your button belongs to the fragment layout?? – Raghunandan May 25 '14 at 17:50
  • 1
    As @Raghunandan says - the most likely reason is your buttons and textview are in the `fragment_main.xml` and not in the `activity_main.xml`. You can only use `findViewById(...)` to get references to widgets in the current layout. You need to put the code for getting the buttons and textview into the `Fragment` along with the `OnClickListener`. – Squonk May 25 '14 at 17:53
  • 1
    See http://stackoverflow.com/questions/23653778/nullpointerexception-accessing-views-in-oncreate – laalto May 25 '14 at 17:54
  • yeah i see it now, but i tried to move the buttons to the main_activity.xml but I actually have no idea what to do, since after i do it, it gives me an error... – Golan Kiviti May 25 '14 at 18:00
  • please post the stacktrace, otherwise we can just guess – Patrick May 25 '14 at 18:13
  • Alright i copied the fragment_main.xml to the main_activity.xml and now it works. But I dont understand why it opened the fragment_main.xml, and why i need that for... – Golan Kiviti May 25 '14 at 18:33
  • @user3674127 : There have been changes to the way new projects are created with the most recent Android Development Tools (ADT). It's causing a lot of problems for new Android developers and the changes aren't very popular amongst a lot of people. In short, a new project is forcing the creation of an `Activity` with a `Fragment` whether you want it or not which means you have two choices - either adopt the `Fragment` approach or delete the code related to the `Fragment` and stick with only the `Activity`. Fragments are a good thing but this approach with the latest ADT is confusing. – Squonk May 25 '14 at 18:46
  • When you called setContentView you told it to use activity_main.xml. Then any call to findViewById() will look only into activity_main.xml – Martin Konecny May 25 '14 at 18:47

1 Answers1

0

It's most likely that your line

add = (Button)findViewById(R.id.bAdd);

is returning null. Try verifying this by printing

Log.d("TEST", "is null: " + (add == null));

The next step is to ensure sure that Button with id "bAdd" is actually defined in your activity_main XML. Edit your question to add your XML file activity_main.

Martin Konecny
  • 57,827
  • 19
  • 139
  • 159