1. Vishal Sharma5's Avatar
    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
        }
    03-08-2014 10:24 AM
  2. Vishal Sharma5's Avatar
    IS there no one who could help me out??
    03-09-2014 07:22 AM
  3. Alex_Mervan's Avatar
    thank you
    03-10-2014 04:36 AM
  4. Vishal Sharma5's Avatar
    thank you?
    03-10-2014 10:08 AM

Similar Threads

  1. What Phone to buy for LTE that works on Straight Talk?
    By robertp039 in forum General Help and How To
    Replies: 1
    Last Post: 03-08-2014, 07:04 PM
LINK TO POST COPIED TO CLIPBOARD