String Concatenation (n00b needing help)

Put your problem here if it does not fit any of the other categories.

String Concatenation (n00b needing help)

Postby SeanPreston » Wed Mar 17, 2010 9:12 pm

hey,

I have created a simply application which on button click displays what the users has written in a text box, although what I want it to do instead of setting the text of the TextView each time the button is pressed, I want it to ADD to it.

For example, I type "hello" then click submit, it will show "hello, I then type "one", it should show:

"helloone"


Heres my code:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. //Get text from message field and store in var
  3.             EditText chat_content = (EditText)findViewById(R.id.txt_chat);  
  4.             String chat_text = chat_content.getText().toString();
  5.            
  6. //Get current text in the TextView - so we can add to it later
  7.             EditText message_one = (EditText)findViewById(R.id.lbl_chat);
  8.             String chat_text_old = message_one.getText().toString();
  9.                
  10.            
  11.             TextView chat_text_final = (TextView)findViewById(R.id.lbl_chat);
  12.             //Set text
  13.             chat_text_final.setText((chat_text)+(chat_text_old)) ;
  14.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4



I get the following error:
Code: Select all
03-17 20:31:14.230: ERROR/AndroidRuntime(25255): Uncaught handler: thread main exiting due to uncaught exception
03-17 20:31:14.260: ERROR/AndroidRuntime(25255): java.lang.ClassCastException: android.widget.TextView
03-17 20:31:14.260: ERROR/AndroidRuntime(25255):     at com.media201.chatbot.chatbot$1.onClick(chatbot.java:28)
03-17 20:31:14.260: ERROR/AndroidRuntime(25255):     at android.view.View.performClick(View.java:2344)
03-17 20:31:14.260: ERROR/AndroidRuntime(25255):     at android.view.View.onTouchEvent(View.java:4133)
03-17 20:31:14.260: ERROR/AndroidRuntime(25255):     at android.widget.TextView.onTouchEvent(TextView.java:6504)
03-17 20:31:14.260: ERROR/AndroidRuntime(25255):     at android.view.View.dispatchTouchEvent(View.java:3672)
03-17 20:31:14.260: ERROR/AndroidRuntime(25255):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
03-17 20:31:14.260: ERROR/AndroidRuntime(25255):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
03-17 20:31:14.260: ERROR/AndroidRuntime(25255):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
03-17 20:31:14.260: ERROR/AndroidRuntime(25255):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:882)
03-17 20:31:14.260: ERROR/AndroidRuntime(25255):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1712)
03-17 20:31:14.260: ERROR/AndroidRuntime(25255):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1202)
03-17 20:31:14.260: ERROR/AndroidRuntime(25255):     at android.app.Activity.dispatchTouchEvent(Activity.java:1987)
03-17 20:31:14.260: ERROR/AndroidRuntime(25255):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1696)
03-17 20:31:14.260: ERROR/AndroidRuntime(25255):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1658)
03-17 20:31:14.260: ERROR/AndroidRuntime(25255):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-17 20:31:14.260: ERROR/AndroidRuntime(25255):     at android.os.Looper.loop(Looper.java:123)
03-17 20:31:14.260: ERROR/AndroidRuntime(25255):     at android.app.ActivityThread.main(ActivityThread.java:4203)
03-17 20:31:14.260: ERROR/AndroidRuntime(25255):     at java.lang.reflect.Method.invokeNative(Native Method)
03-17 20:31:14.260: ERROR/AndroidRuntime(25255):     at java.lang.reflect.Method.invoke(Method.java:521)
03-17 20:31:14.260: ERROR/AndroidRuntime(25255):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
03-17 20:31:14.260: ERROR/AndroidRuntime(25255):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
03-17 20:31:14.260: ERROR/AndroidRuntime(25255):     at dalvik.system.NativeStart.main(Native Method)




Regards,
Sean
SeanPreston
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Mar 17, 2010 9:07 pm

Top

Postby asolanki » Mon Mar 22, 2010 11:05 am

Hi!!!

According to me there is no problem in string concatenation..

try to debug it may be the problem is with textview casting in this line..

TextView chat_text_final = (TextView)findViewById(R.id.lbl_chat);

are you using the proper id?
asolanki
Senior Developer
Senior Developer
 
Posts: 151
Joined: Thu Jan 08, 2009 7:32 am

Postby Boni2k » Mon Mar 22, 2010 2:56 pm

Yes, "java.lang.ClassCastException: android.widget.TextView" means that you are using the wrong ID or you cast it to a wrong widget type.
Boni2k
Experienced Developer
Experienced Developer
 
Posts: 88
Joined: Sun Dec 20, 2009 9:46 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google Feedfetcher and 15 guests