[beginner] Button not triggering method

Put your problem here if it does not fit any of the other categories.

[beginner] Button not triggering method

Postby FakeCaker » Tue Feb 15, 2011 6:57 pm

Hi,
i'm quitte new to java (/android), so please don't be hard for me :)

I've got a simple app, wich should take input from EditText, and display it in a TextView after the button is pressed.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package oop.test;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.widget.Button;
  8. import android.widget.EditText;
  9. import android.widget.TextView;
  10.  
  11. public class oopTest extends Activity implements OnClickListener {
  12.        
  13.         public Button buttonEqual;
  14.         public EditText input1;
  15.         public TextView answer;
  16.  
  17.        
  18.     /** Called when the activity is first created. */
  19.        
  20.         @Override
  21.     public void onCreate(Bundle savedInstanceState) {
  22.         super.onCreate(savedInstanceState);
  23.         setContentView(R.layout.main);
  24.        
  25.        
  26.         //getting Id's
  27.        
  28.         input1 = (EditText) findViewById(R.id.input1);
  29.         answer = (TextView) findViewById(R.id.answer);
  30.         buttonEqual = (Button) findViewById(R.id.buttonEqual);
  31.        
  32.         //initialize
  33.         answer.setText("test");
  34.        
  35.         }
  36.              
  37.         //methods
  38.        
  39.         void calculateAnswer(String valA){        
  40.            answer.setText(valA);
  41.      }
  42.        
  43.      
  44.         @Override
  45.         public void onClick(View v) {
  46.                 // TODO Auto-generated method stub
  47.                
  48.        
  49.                 buttonEqual.setOnClickListener(this);  
  50.         calculateAnswer(input1.getEditableText().toString());
  51.          
  52.         }
  53.        
  54.         }
  55.        
  56.        
  57.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


but now when i press the button, nothing will happen, and i get no errors whatsoever.

Could someone please help me? if the code is not clear enough just tell me :D


Thanks!
FakeCaker
Freshman
Freshman
 
Posts: 9
Joined: Tue Feb 15, 2011 6:46 pm

Top

Re: [beginner] Button not triggering method

Postby Shattered9 » Tue Feb 15, 2011 11:27 pm

Hmm, it looks to me that you're doing things a tad backwards. Rather than :

Code: Select all
      @Override
        public void onClick(View v) {
                // TODO Auto-generated method stub

                buttonEqual.setOnClickListener(this); 
        calculateAnswer(input1.getEditableText().toString());


It should be more like :

Code: Select all
      buttonEqual.setOnClickListener(new OnClickListener) {
            @Override
            public void onClick(View v) {
                          calculateAnswer(input1.getEditableText().toString());
            }
      }


Rather than simply pasting code around though, perhaps you should watch this video :

http://www.xtensivearts.com/2009/07/18/ ... g-buttons/

It isn't the exact same as what you're trying, but it provides much of the same concepts and should help you understand the process a bit better. The other video tutorials on that site would likely also be great for you. :) Good luck.
Shattered9
Developer
Developer
 
Posts: 39
Joined: Sat Feb 12, 2011 4:41 pm

Re: [beginner] Button not triggering method

Postby FakeCaker » Wed Feb 16, 2011 8:33 pm

Thank you very much, I watched the first five Tutorials, they are great :D

now i have my code like this:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.   package oop.test;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.widget.Button;
  8. import android.widget.EditText;
  9. import android.widget.TextView;
  10.  
  11. public class oopTest extends Activity implements OnClickListener {
  12.        
  13.         public Button buttonEqual;
  14.         public EditText input1;
  15.         public TextView answer;
  16.  
  17.         //method
  18.         void calculateAnswer(String valA){        
  19.                    answer.setText(valA);
  20.                 }
  21.        
  22.     /** Called when the activity is first created. */
  23.        
  24.         @Override
  25.     public void onCreate(Bundle savedInstanceState) {
  26.         super.onCreate(savedInstanceState);
  27.         setContentView(R.layout.main);
  28.      
  29.        
  30.        
  31.         //initialize
  32.         answer.setText("Nothing Pressed...");
  33.        
  34.        
  35.           buttonEqual.setOnClickListener(new OnClickListener() {
  36.           @Override
  37.           public void onClick(View v) {
  38.                         calculateAnswer(input1.getEditableText().toString());
  39.                                
  40.                         };
  41.        
  42.         });
  43.        
  44.         }}
  45.        
  46.  
Parsed in 0.035 seconds, using GeSHi 1.0.8.4


It force Closed right away.
the only error i'm given is on
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class oopTest extends Activity implements OnClickListener {
Parsed in 0.033 seconds, using GeSHi 1.0.8.4
.
it tells me on oopTest "The type oopTest must implement the inherited abstract method View.OnClickListener.onClick(View)".

It tells me to Either change to abstract class, or add unimplemented method.

please some more help :D


ps: i believe i shouldn't post logcat?
FakeCaker
Freshman
Freshman
 
Posts: 9
Joined: Tue Feb 15, 2011 6:46 pm

Re: [beginner] Button not triggering method

Postby Shattered9 » Wed Feb 16, 2011 10:04 pm

Well, assuming you are using Eclipse like most, simply hovering your mouse over "oopTest" and clicking "Add unimplemented methods" in the box that pops up would solve that error. It is happening because you are implementing OnClickListener and you're required to have a corresponding onClick method when you do, similar to how it is required when you set up the button.setOnClickListener.

However, you should instead just delete the "implements OnClickListener" bit all together. It isn't necessary for what you are trying to do.

Also, it doesn't look like you are assigning your Button, EditText, or TextView to their corresponding objects. You create the objects at the top as you should, but never tell them what they are supposed to hold later on.

Code: Select all
buttonEqual = (Button)findViewById(R.id.yourButtonID);
input1 = (EditText)findViewById(R.id.yourEditTextID);
answer = (TextView)findViewById(R.id.yourTextViewID);


You get the idea I'm sure.

Other than that you should be good to go. I can't quite compile code at the moment to test it myself, but at a glance it seems okay.

Good luck!
Shattered9
Developer
Developer
 
Posts: 39
Joined: Sat Feb 12, 2011 4:41 pm

Re: [beginner] Button not triggering method

Postby FakeCaker » Thu Feb 17, 2011 7:52 pm

I removed the "Implements OnClickListener and it worked :)
i thought it was required to add that if you used ButtonListeners... ohwell...

I was wondering tough what you meant by that last part about my layout declaration (or whatever it's called :P)
it looks correct to me.

Anyways, thanks alot again :)
FakeCaker
Freshman
Freshman
 
Posts: 9
Joined: Tue Feb 15, 2011 6:46 pm

Re: [beginner] Button not triggering method

Postby Shattered9 » Fri Feb 18, 2011 4:58 am

Have you actually ran the code above? I would figure your program would crash and give you a NullPointerException.

Near the top of your code you have :

Code: Select all
        public Button buttonEqual;
        public EditText input1;
        public TextView answer;


Think of this as creating empty boxes that can contain a Button, EditText, and TextView, but at this point they are just empty boxes. You can call methods on these, such as putting the OnClickListener on buttonEqual, but it will be trying to call that method on nothing, or null, thus the "NullPointer" Exception.

To fix this you would just need to put something into this figurative box, by telling that box which view you want it to point to, which is what those few lines I posted earlier would do (After you put in the correct view ID at least).
Shattered9
Developer
Developer
 
Posts: 39
Joined: Sat Feb 12, 2011 4:41 pm

Top

Re: [beginner] Button not triggering method

Postby FakeCaker » Fri Feb 18, 2011 8:37 am

Oh, i know what you mean.
I actually had it already in my code, but somehow it's not in my post :O

Odd, but it's working great now :) tahanxkx
FakeCaker
Freshman
Freshman
 
Posts: 9
Joined: Tue Feb 15, 2011 6:46 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 25 guests