Application closing for no reason

Vishal Sharma5

New member
Mar 8, 2014
3
0
0
Visit site
Hi guys i started learning android development about 2 days ago.. So was trying out with a basic application to increment and decrement.
Everything is okay but when i'm testing the application even on the emulator, i get a message "Unfortunately, application <something written>"
I tried debugging the code and found that Even after declaring a statement findViewById(), my variables are pointing to null. And i think that's the reason for this error.. heres my code, in MainActivity.java. Please help me out!

Code:
public class MainActivity extends ActionBarActivity {

    TextView sum;
    Button increment;
    Button decrement;
    int total;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        this.findViewById();

        total = 0;

        increment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                total++;
                sum.setText("Sum is "+total);
            }
        });

        decrement.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                total--;
                sum.setText("Sum is "+total);
            }
        });

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

    private void findViewById() {
        sum = (TextView) findViewById(R.id.tv);          // sum points to null
        increment = (Button) findViewById(R.id.inc);  //increment points to null
        decrement = (Button) findViewById(R.id.dec);  // decrement points to null
    }
 

Trending Posts

Forum statistics

Threads
943,836
Messages
6,920,261
Members
3,159,258
Latest member
yusufdontk