The problem is that you are referencing a non-final variable
("bodyText") that is outside the scope of your View.OnClickListener()
inner class.

You need to make bodyText final, so that it can be accessed from
inside your anonymous inner class. You do not need to make TESTSTRING
final, though doing so should do no harm (unless later on you try to
re-assign it to another string).

Your code should end up looking something like this:
http://pastebin.com/f4895dad4

Make sure you are familiar with the concepts of the final keyword. It
is important to understand how it affects the variables you apply it
to. In particular, take a look at the "Anonymous Inner Classes"
section on this article:
http://renaud.waldura.com/doc/java/final-keyword.shtml#vars.


On Wed, Jan 27, 2010 at 5:14 PM, André <pha...@hotmail.com> wrote:
> I tried that now but get the same problem.
>
> here is a screen shot of it:
>
> http://www.andreborud.com/android/android-problem-2.jpg
>
> André
>
> On Jan 27, 5:39 pm, Justin Anderson <janderson....@gmail.com> wrote:
>> Why are you using the final keyword?  I think that is the source of your
>> problem.  Remove that and I would be that everything will work out.
>>
>> In java, "final" is more-or-less equivalent to "const" in c++.  Since you
>> are setting this variable every time a button is clicked I don't think you
>> want to set this to final.
>>
>> I am curious though... what exactly is the error that you are getting?
>>
>> ----------------------------------------------------------------------
>> There are only 10 types of people in the world...
>> Those who know binary and those who don't.
>> ----------------------------------------------------------------------
>>
>>
>>
>> On Wed, Jan 27, 2010 at 9:26 AM, André <pha...@hotmail.com> wrote:
>> > Hello,
>>
>> > I'm sure there has been a lot of questions like this here already. But
>> > I haven't been able to find an answer to my question is. I'm still
>> > very new to this so it's probably something very simple.
>> > What I am trying is to save text from a edittext window, it works fine
>> > if I have a pre-written text. Any suggestion on how I should make this
>> > work?
>>
>> > Bellow you can see what I have tried! The row with the stars is what
>> > eclipse doesn't accept.
>>
>> > private EditText bodyText;
>>
>> >   �...@override
>> >    public void onCreate(Bundle savedInstanceState) {
>> >        super.onCreate(savedInstanceState);
>> >        setContentView(R.layout.main);
>>
>> >        bodyText = (EditText) findViewById(R.id.body);
>> >        Button confirmButton = (Button) findViewById(R.id.confirm);
>> >        confirmButton.setOnClickListener(new View.OnClickListener() {
>>
>> >                public void onClick(View view) {
>> >                        try {
>> >                            final String TESTSTRING = new String(bodyText);
>>
>> > ***************************************************************************
>> >  *****
>>
>> >                            FileOutputStream fOut =
>> > openFileOutput("samplefile.txt",
>> > MODE_WORLD_READABLE);
>> >                            OutputStreamWriter osw = new
>> > OutputStreamWriter(fOut);
>>
>> >                            osw.write(TESTSTRING);
>>
>> >                            osw.flush();
>> >                            osw.close();
>>
>> >                        } catch (IOException ioe) {
>> >                            ioe.printStackTrace();
>> >                        }
>> >                }
>>
>> >            });
>> >        }
>> > }
>>
>> > --
>> > You received this message because you are subscribed to the Google
>> > Groups "Android Beginners" group.
>>
>> > NEW! Try asking and tagging your question on Stack Overflow at
>> >http://stackoverflow.com/questions/tagged/android
>>
>> > To unsubscribe from this group, send email to
>> > android-beginners+unsubscr...@googlegroups.com<android-beginners%2Bunsubscr
>> >  i...@googlegroups.com>
>> > For more options, visit this group at
>> >http://groups.google.com/group/android-beginners?hl=en
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Beginners" group.
>
> NEW! Try asking and tagging your question on Stack Overflow at
> http://stackoverflow.com/questions/tagged/android
>
> To unsubscribe from this group, send email to
> android-beginners+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/android-beginners?hl=en
>

-- 
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.

NEW! Try asking and tagging your question on Stack Overflow at
http://stackoverflow.com/questions/tagged/android

To unsubscribe from this group, send email to
android-beginners+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en

Reply via email to