简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM