user3081157
user3081157

Reputation:

changing editText to textView error

Beginner at android, I did some research and did not come up with an answer - so I am posting on SO. Please keep in mind I have done the usual things such as going to javadoc or other first step problem solutions that most programmers do before asking another programmer for help. Basically, I am trying to learn how to send data between activities, and I understand how to send a message that someone types in (editText), but if I just want to send over data from a textView, this is where I am having issues. My question: what am i doing wrong on this line (I assume my syntax is wrong here, I don't feel the need to post rest of code, but will if it will help).

All I did was switch up "editText1" and "textView1" and although there's no errors in the code, whenever I run the app (the activity switch happens at a button press) and press the button it gives me an android error and closes the app.

//doesnt work
String text = ((EditText)findViewById(R.id.textView1)).getText().toString();

//works
String text = ((EditText)findViewById(R.id.editText1)).getText().toString();

Upvotes: 1

Views: 85

Answers (3)

Subhalaxmi
Subhalaxmi

Reputation: 5707

A request to you as you are new.. Please check the respective id of the view you used in your layout and make sure you are linking to corresponding view with there respective id.

In your case use :

String text_textview = ((TextView)findViewById(R.id.textView1)).getText().toString();

and

String text_edittext = ((EditText)findViewById(R.id.editText1)).getText().toString(); 

Thank you :)

Upvotes: 1

Antrromet
Antrromet

Reputation: 15414

Welcome to SO. A piece of advice, always post crash logs when you are having some trouble with crashes. As per the very few pieces of info that you have provided, it seems that you must be getting a ClassCastException because you are trying to cast a TextView(textView1) to an EditText (editText1). The correct code would be

String text = ((TextView)findViewById(R.id.textView1)).getText().toString();

Upvotes: 1

Nambi
Nambi

Reputation: 12042

Because you are casting the Textview object to EditText that two are different class

android.widget.TextView
android.widget.EditText

Upvotes: 1

Related Questions