0

Im trying to make a basic program where you press a button and it adds to an integer. Im having trouble updating the text view.

package com.example.curtis.simpleapplication;

import android.app.Activity; 
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity extends Activity {
    int num;
    TextView numview;

    private void updateNumber(){
        TextView numview = (TextView)findViewById(R.id.Number);
        numview.setText(num);
    }

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

        setupButton();
    }
    private void setupButton() {
        Button button = (Button) findViewById(R.id.Button);
        button.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                num = num + 1;
                numview.setText(num);
            }

        });
    }

Help would be great im trying to get into android and books havent been very helpful

4 Answers 4

3

There are a few mistakes here. First, you are never calling updateNumber() so numviewis never set. Moreover, you have two TextView numView once global in your MainActivity and again in updateNumber(). There is a better way to do this. But first go to activity_main.xml file and within the button tag add the the line android:onClick="onClick" something like this :

<Button
    android:id="@+id/button1"
    ...
    android:onClick="onClick"/>

Next in your MainActivity.java, add the following lines of code :

package com.example.curtis.simpleapplication;

import android.app.Activity; 
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity extends Activity {
     int num = 0;
     TextView numview;

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

         numview = (TextView)findViewById(R.id.Number);
    }

    public void onClick(View view){
        num += 1;
        numview.setText(String.valueOf(num));
    }
}

onClick() will be automatically called when your button is clicked because of the android:onClick property.

Sign up to request clarification or add additional context in comments.

Comments

1

you have two different objects with same name and it would cause error.

one of them is defined in your class (TextView numview;) and the other one is defined in your function update number.

in your onClick method you want to change numView defined in your class and it's not initialized. you have to add change your code :

package com.example.curtis.simpleapplication;

import android.app.Activity; 
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity extends Activity {
    int num;
    TextView numview;


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

        numview = (TextView)findViewById(R.id.Number);
        setupButton();
    }
    private void setupButton() {
        Button button = (Button) findViewById(R.id.Button);
        button.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                num = num + 1;
                numview.setText(num);
            }

        });
    }

Comments

0

Put this code in your onClick function

num = num + 1; updateNumber();

Comments

0

You are never calling updateNumber()

package com.example.curtis.simpleapplication;

import android.app.Activity; 
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class MainActivity extends Activity {
int num=0;
TextView numview;

private void updateNumber(int number){
    numview = (TextView)findViewById(R.id.Number);
    numview.setText(number);
}

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

    setupButton();
}
private void setupButton() {
    Button button = (Button) findViewById(R.id.Button);
    button.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            num = num + 1;
            updateNumber(num);
        }

    });
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.