0

I am new to android development and have been doing some practices. I did some research and debugging and believe i found the source of the problem. Which i believe is this portion here. My aim is to create two buttons that add 1 and deduct 1 upon click and displaying the sum.

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

        }
    });//Take it as one liner
    sub.setOnClickListener(new View.OnClickListener() {
        //using the id of the buttons
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            counter--;
            Display.setText("Your total is " + counter);

        }
    });//Take it as one liner

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

But i cannot seem to figure out what's wrong. I am very sure that the problem lies with the onClickListener as it only crashes when i implement the code and i am very sure i used the correct id.

i put my button id under strings. Am i wrong to do that?

leppie
  • 109,129
  • 16
  • 185
  • 292
Muzamir
  • 88
  • 1
  • 8

2 Answers2

2

I am not exactly sure what your problem here is, but I can suggest an alternative which I find to be more consistent and reliable.

Instead of using an onClickListener, which I personally find a hassle to set up and use, you can set up an equivalent bit of code in your XML file. This will work whenever your object is defined and has code in your XML file, such as a TextView or and ImageView. It also works with buttons.

In your set of XML code for your button, add these two lines of code.

android:Clickable="true"
android:onClick="methodYouWantCalled"

The first line makes the object, in this case the button, clickable. The second line tells the program what method to call when it is clicked. It will call the method with the name in the quotes that is in the corresponding class. Just make sure your class is set up like:

public static void methodYouWantCalled(View view){

So for your add button, your XML code you are adding would look like:

android:Clickable="true"
android:onClick="add"

and you would need to have this code in the corresponding class:

public static void add(View view){

  counter++;
  Display.setText("Your total is "+ counter);
}

As for you putting your button ID under strings, I would just put the id into the actual XML code.

android:id="@+id/=yourIdHere"

This is the safest way, your ID for that object is what is in quotes. So, your final XML code for the add button should be:

<Button android:id="@+id/bAdd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add One" <!-- Or whatever your text is -->
        android:Clickable="true"
        android:onClick="add"
/>

And your final java for the add method is the same as above. You can edit these for each of your buttons.

If the app is still crashing after you try this, make sure the code inside your listeners/methods are correct. We could help more if we had the logcat/errors that you see when it crashes.

Hope this helps!

EDIT


The problem you are having is that you cannot put an entire method inside another. Super.onCreate() is its own method, that runs just like
 private static void main(String[] args){

The reason you are getting that error is because you are not able to put a method there. Instead of putting it here:

public class yourActivity extends Activity {


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.yourLayout);

        public static void add(View view){
            //code
            }

}   
}

put it here:

public class yourActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.yourLayout);


}   

        public static void add(View view){
            //code
            }

}

Once it is outside of the onCreate Method, there is no reason to call it anywhere, because the XML code does that for you.

As for the View v/View view, it dosen't matter which one you use, as long as you always use the same one.

Hope this helps!

  • So i put the methods under the onCreate class. However, an error occured on the method. **void is an invalid type for add**... and on another part. if i were to use add and sub methods. Then the parameter, 'view' have to be different. because they are using the same local variable. – Muzamir May 25 '14 at 05:42
  • The methods can't be under onCreate, they need to be their own method. They need to be after onCreate is finished. I will add an example to my answer shortly. – SplatFace Development May 25 '14 at 10:34
0

use:

Display.setText("Your total is "+ Integer.toString(counter));

Likewise:

Display.setText("Your total is " + Integer.toString(counter));
kgandroid
  • 5,219
  • 5
  • 34
  • 65
  • 2
    It's better if you explain *why* your code solves the problem, see [answer]. It's not my area, but ain't this a typo: `toSting` -> `toString`? – brasofilo May 25 '14 at 06:09
  • @brasofolio..I think the code is self explanatory. And also I guess that you are mature enough to understand that toString() was a typing mistake. – kgandroid May 25 '14 at 06:39
  • Well, your answer showed up in the Low Quality Posts review... I just provided a hint on how to write nice, useful, answers that atract upvotes, possibly marked as the correct one and that are not displayed as LQP... Good luck! – brasofilo May 25 '14 at 06:42