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();

Answers


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

android.widget.TextView
android.widget.EditText

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();

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


Need Your Help

Which browsers and versions support directly passing common array-like objects to fn.apply()

javascript function cross-browser arguments

Which browsers and versions support (Firefox, IE, Chrome, Safari, iOS Safari, and Android) the following JavaScript Function apply() feature:

Mapping Absolute / Relative (Local) Paths to Absolute URLs

php url path relative-path absolute-path

I need a fast and reliable way to map an absolute or relative local path (say ./images/Mafalda.jpg) to it's corresponding absolute URL, so far I've managed to come up with this: