Solved! I had to move the EditText bodyText; line outside of onClick and onCreate and add private infront of it. Hope this can help someone else in the future!
On Jan 29, 6:35 pm, André <pha...@hotmail.com> wrote: > Still I haven't been able to make this work? > > I have changed that way to make the file a little bit to a way that > has worked fine for me when instead of using a variable on out.write > (VAR); I just wrote out.write("Hello World"); The later one works > fine. > But when I try to get my text I want out of EditText nothing happens. > Not even the pre defined file is written. Can any one explain why? > Here is the source code for it: > > Button confirmButton = (Button) findViewById(R.id.confirm); > confirmButton.setOnClickListener(new View.OnClickListener() { > > public void onClick(View view) { > try { > EditText bodyText; > bodyText = (EditText) findViewById(R.id.body); > String TESTSTRING = > bodyText.getText().toString(); > > File root = Environment.getExternalStorageDirectory > (); > if (root.canWrite()){ > File textFile = new File(root, "index.html"); > FileWriter textWriter = new FileWriter > (textFile); > BufferedWriter out = new BufferedWriter > (textWriter); > out.write(TESTSTRING); > out.close(); > } > } catch (IOException e) { > Log.e(TAG, "Could not write file " + e.getMessage > ()); > } > } > > }); > > André > > On Jan 28, 1:31 pm, Sean Hodges <seanhodge...@googlemail.com> wrote: > > > > > Make sure you sign your APK before trying to use it on your phone. > > >http://developer.android.com/intl/fr/guide/publishing/app-signing.html > > > On Thu, Jan 28, 2010 at 11:48 AM, André <pha...@hotmail.com> wrote: > > > That worked fine! > > > Thank you! > > > > Now my only problem is that my HTC Tattoo doesn't want to install the > > > apk when I want to test it! =( > > > > André > > > > On Jan 28, 11:19 am, Sean Hodges <seanhodge...@googlemail.com> wrote: > > >> Sorry, I really wasn't on the ball yesterday :) > > > >> What you want is: > > > >> String TESTSTRING = bodyText.getText().toString(); > > > >> Just for a little background: > > > >> Your bodyText object is what's called a View in Android, it contains a > > >> lot more info than just the contents of the text field (such as its > > >> size and appearance). To get the text in the field you need to call > > >> bodyText.getText(), which will return the current text value wrapped > > >> in an object called an Editable. > > > >> The reason it returns an Editable instead of a String is fairly > > >> subtle, but if you understand the concept of "immutable" objects you'd > > >> have an idea to its purpose, the API describes it in more > > >> detail:http://developer.android.com/intl/fr/reference/android/text/Editable.... > > > >> Ultimately, your OutputStreamWriter needs a String to work, which we > > >> can get by calling the toString() method. > > > >> Hope it helps. > > > >> On Wed, Jan 27, 2010 at 7:08 PM, André <pha...@hotmail.com> wrote: > > >> > Thank you. > > > >> > I have tried to do what you wrote. Not sure if I did it right because > > >> > I still get a problem. When I put String TESTSTRING = bodyText.getText > > >> > (); inside of the onClick method I get a red line under > > >> > bodyText.getText(). If i hold my mouse over it suggests me to change > > >> > String TESTSTRING do Editable TESTSTRING. If i do that osw.write is > > >> > wrong and wants to change TESTSTRING to an int. > > > >> > Any suggestions? > > > >> > André > > > >> > On Jan 27, 6:36 pm, Sean Hodges <seanhodge...@googlemail.com> wrote: > > >> >> Ah, something I missed before... > > > >> >> To get the text out of your EditText view, you want to do this: > > > >> >> String TESTSTRING = bodyText.getText(); > > > >> >> The String() class does not accept EditText objects, you need to > > >> >> retrieve the string that is contained inside your bodyText object. > > > >> >> On Wed, Jan 27, 2010 at 5:31 PM, Sean Hodges > > > >> >> <seanhodge...@googlemail.com> wrote: > > >> >> > 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 > > > > -- > > > 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 > > ... > > read more » -- 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