wood burning stoves 2.0*
The moose likes Android and the fly likes Button with OnClickListener error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Mobile » Android
Bookmark "Button with OnClickListener error" Watch "Button with OnClickListener error" New topic
Author

Button with OnClickListener error

Alex Munozz
Greenhorn

Joined: Nov 23, 2012
Posts: 15
Hi,

I want to make a simple android project. An EditText, EditView and a Button are displayed in the screen and after typing a text and clicking the button it is displayed the text typed.

The layout is made and there are no errors. The issue is in the Activity code. Here is the code:



There are two errors in the OnClick function with the same message:

cannot refer to a non-final variable t inside an inner class defined in a different method

After searching in internet about this error I couldn't understand it and how to fix it.

Any clue about what's wrong?

Thanks.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39534
    
  27
It's a lot harder to explain *why* this is a problem, than to explain how to fix it. First, the fix: instead of "TextView t = (TextView) ..." use "final TextView t = (TextView) ".

The problem is that -without the "final"- the value of "t" could change, so it's not clear which object should be used when the OnClickListener is run. Putting the "final" there asserts that the value won't change, so a reference to that object can be stored safely for the OnClickListener to use.


Ping & DNS - updated with new look and Ping home screen widget
Alex Munozz
Greenhorn

Joined: Nov 23, 2012
Posts: 15
Thanks for the answer. Now there are any error but I want the text you type in the EditText is displayed on the screen.

I think the function to do it is SetText(). But when I run the app, I type a text and after clicking on the button I display anything.

How can I do it?

Thanks.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39534
    
  27
If you want to display the text from the EditText, then you need to get the text from the EditText. Currently the code retrieves text from the TextView and then displays that text in the TextView - not a terribly exciting thing to do :-)
Alex Munozz
Greenhorn

Joined: Nov 23, 2012
Posts: 15
I changed the code as you told me but the result is the same: nothing is displayed on the screen.

Here is the new code:



I don't know what is missing. Any ideas?

Thanks.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39534
    
  27
Now you're getting the text from the EditText, and then you're setting the text of the EditText. Not terribly interesting, either :-)

You want to get the text from the EditText, and then set the text of the TextView, right?
Alex Munozz
Greenhorn

Joined: Nov 23, 2012
Posts: 15
I changed the code as you told me and it works.

Thanks for the answers.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Button with OnClickListener error
 
Similar Threads
Httppost Problem
Android code to add two numbers
public void onClick(View v) { // error
SharedPreference not working
Cannot find the problem in the code