I’ve been basing this code off the Bluetooth Chat example. It worked when it was listview and now am trying to display this as textview and doesn’t work at all now.
I’ve only made minor changes to the code just to display the message recevied and not been able to send message back. These are getting rid of all write methods.
This is the orignal code
private void setupChat() {
Log.d(TAG, "setupChat()");
// Initialize the array adapter for the conversation thread
mConversationArrayAdapter = new ArrayAdapter<String>(this, R.layout.message);
mConversationView = (ListView) findViewById(R.id.in);
mConversationView.setAdapter(mConversationArrayAdapter);
// Initialize the compose field with a listener for the return key
mOutEditText = (EditText) findViewById(R.id.edit_text_out);
mOutEditText.setOnEditorActionListener(mWriteListener);
// Initialize the send button with a listener that for click events
mSendButton = (Button) findViewById(R.id.button_send);
mSendButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Send a message using content of the edit text widget
TextView view = (TextView) findViewById(R.id.edit_text_out);
String message = view.getText().toString();
sendMessage(message);
}
});
// Initialize the BluetoothChatService to perform bluetooth connections
mChatService = new BluetoothChatService(this, mHandler);
// Initialize the buffer for outgoing messages
mOutStringBuffer = new StringBuffer("");
}
This is what I changed it to.
private void setupChat()
{
Log.d(TAG, "setupChat()");
mIn = (TextView) findViewById(R.id.in);
// Initialize the array adapter for the conversation thread
// Initialize the BluetoothChatService to perform bluetooth connections
mChatService = new BluetoothChatService(this, mHandler);
This is the logcat output.
11-06 15:35:52.047: E/AndroidRuntime(19780): FATAL EXCEPTION: main11-06 15:35:52.047: E/AndroidRuntime(19780): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.BluetoothChat/com.example.android.BluetoothChat.BluetoothChat}: java.lang.ClassCastException: android.widget.ListView
11-06 15:35:52.047: E/AndroidRuntime(19780): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2781)
11-06 15:35:52.047: E/AndroidRuntime(19780): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2797)
11-06 15:35:52.047: E/AndroidRuntime(19780): at android.app.ActivityThread.access$2300(ActivityThread.java:135)
11-06 15:35:52.047: E/AndroidRuntime(19780): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2132)
11-06 15:35:52.047: E/AndroidRuntime(19780): at android.os.Handler.dispatchMessage(Handler.java:99)
11-06 15:35:52.047: E/AndroidRuntime(19780): at android.os.Looper.loop(Looper.java:143)
11-06 15:35:52.047: E/AndroidRuntime(19780): at android.app.ActivityThread.main(ActivityThread.java:4914)
11-06 15:35:52.047: E/AndroidRuntime(19780): at java.lang.reflect.Method.invokeNative(Native Method)
11-06 15:35:52.047: E/AndroidRuntime(19780): at java.lang.reflect.Method.invoke(Method.java:521)
11-06 15:35:52.047: E/AndroidRuntime(19780): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-06 15:35:52.047: E/AndroidRuntime(19780): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-06 15:35:52.047: E/AndroidRuntime(19780): at dalvik.system.NativeStart.main(Native Method)
11-06 15:35:52.047: E/AndroidRuntime(19780): Caused by: java.lang.ClassCastException: android.widget.ListView
11-06 15:35:52.047: E/AndroidRuntime(19780): at com.example.android.BluetoothChat.BluetoothChat.setupChat(BluetoothChat.java:141)
11-06 15:35:52.047: E/AndroidRuntime(19780): at com.example.android.BluetoothChat.BluetoothChat.onStart(BluetoothChat.java:116)
11-06 15:35:52.047: E/AndroidRuntime(19780): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1197)
11-06 15:35:52.047: E/AndroidRuntime(19780): at android.app.Activity.performStart(Activity.java:3822)
11-06 15:35:52.047: E/AndroidRuntime(19780): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2754)
Other changes made was to remove the sendMessage() method and the MESSAGE_WRITE case in the handleMessage() method.
Also removed the write the two write(buffer[] byte) methods from BluetoothChatServices class
I’ve been looking at this for few days and really can’t figure out whats wrong.
Looking at your code it shows that in your original code you have R.id.in being cast as a ListView.. in your new code you have the same resource (R.id.in) being cast to a TextView. Likely what’s happening is that you haven’t changed R.id.in to be a TextView in your layout file and you are trying to cast a ListView to a TextView which will cause this error.
The error
Should give you a good hint of what you’re looking for. ClassCastException means that you tried to cast an object into a type that it cannot be cast to. So your best bet would be to look for where you are casting things, along with the line number the logcat output gave you. If you are casting something there (the TextView) you should verify that both the type you are casting from and casting to are correct.