wood burning stoves 2.0*
The moose likes Android and the fly likes How to retrieve string from EditText? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Mobile » Android
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: 629

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


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

Joined: Oct 12, 2008
Posts: 1369
    
    1

Can you post the exception?


[List of FAQs] | [Android FAQ] | [Samuh Varta]
Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

Actually It only show following attached screen but on Logcat it does not shows exception.
Thanks for quick reply


[Thumbnail for error.jpeg]

Pramod P Deore
Ranch Hand

Joined: Jul 15, 2008
Posts: 629

It shows following error as IllegalArgumentException
Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1369
    
    1

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: 629

my method which send sms is as follows
Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1369
    
    1


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: 629

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.
Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1369
    
    1

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: 629

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: 629


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: 629

Hello everybody!!!

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

Joined: Oct 12, 2008
Posts: 1369
    
    1

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: 629

Thanks Monu.
for your great help.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to retrieve string from EditText?