The moose likes Android and the fly likes How to retrieve string from EditText? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Mobile » Android
Reply Bookmark "How to retrieve string from EditText?" Watch "How to retrieve string from EditText?" New topic
Author

How to retrieve string from EditText?

Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 611

I am writing one application in that I am taking input from user and sending it through sms, but my problem is that I am not able to get that string(i.e. which is given from user as a input )

suppose I have following in the main.xml file


It have one TextField, one EditText and one Button.
Now when user write input to EditText then that String I want to send through the sms.

my Test.java contains


Now when I run this and if I supplied input to form as "Pramod Deore" but when I I click on ok button it throws Exception

This message was edited 1 time. Last update was at by Pramod P Deore


Life is easy because we write the source code.....
Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1355

Can you post the exception?


[List of FAQs] | [Android FAQ] | [My Blog]
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 611

Actually It only show following attached screen but on Logcat it does not shows exception.
Thanks for quick reply
[Thumb - error.jpeg]
 Filename error.jpeg [Disk] Download
 Description
 Filesize 172 Kbytes
 Downloaded:  73 time(s)

Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 611

It shows following error as IllegalArgumentException
Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1355

I suspect, you have not pasted the piece of code that is causing that exception. I do not see SmsManager.sendTextMessage being called anywhere in your code.
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 611

my method which send sms is as follows
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 611

If I had write



instead of


Then I get message successfully,
i.e username is not converted into String but how?
Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1355


Does the API permit any of the addresses to be null? What does the documentation say?
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 611

But when I pass second argument as "Pramod Deore" i.e. String then it runs successfully. But if I pass username (i.e which is get from EditText) then it throws Exception.

This message was edited 1 time. Last update was at by Pramod P Deore

Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1355

But when I pass second argument as "Pramod Deore" i.e. String then it runs successfully. But if I pass username (i.e which is get from EditText) then it throws Exception.

Have you tried to print the value you read from the EditText and see what you are getting? Is it null? Is it empty? Are you passing what you are actually reading to the sendSMS() method?


Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 611

Hello , today I write new application
My Test1.java is like


AndroidManifest file is



and main.xml is

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<LinearLayout
android:id="@+id/widget28"
android:layout_width="252px"
android:layout_height="340px"
androidrientation="vertical"
android:layout_x="36px"
android:layout_y="31px"
>
<TextView
android:id="@+id/widget29"
android:layout_width="226px"
android:layout_height="44px"
android:text="Name"
>
</TextView>
<EditText
android:id="@+id/name"
android:layout_width="225px"
android:layout_height="44px"
android:text="EditText"
android:textSize="18sp"
>
</EditText>
<TextView
android:id="@+id/widget31"
android:layout_width="223px"
android:layout_height="40px"
android:text="Mob No"
>
</TextView>
<EditText
android:id="@+id/no"
android:layout_width="226px"
android:layout_height="48px"
android:textSize="18sp"
>
</EditText>
<Button
android:id="@+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ok"
>
</Button>
</LinearLayout>
</AbsoluteLayout>



But when I run application in the form it shows value of first EditText as EditText. and second fileld which is Mob no is blank.
when I pressed on ok button . The Logcat shows output as





Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 611


Have you tried to print the value you read from the EditText and see what you are getting? Is it null? Is it empty? Are you passing what you are actually reading to the sendSMS() method?


I had print values but I get null in both textfield.
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 611

Hello everybody!!!

Why EditText returns null instead of String even after calling getText.toString();
Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1355

Because you are printing their values inside onCreate(). The View is not drawn until the window receives focus.
You should copy the following inside the button click callback method(inside onClick of Ok button):


Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 611

Thanks Monu.
for your great help.
 
 
subject: How to retrieve string from EditText?
 
developer file tools

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture