• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Button with OnClickListener error

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Alex Munozz
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I changed the code as you told me and it works.

Thanks for the answers.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic