我一直将这段代码从蓝牙聊天示例中删除。当它是listview时它工作,现在我试图将其显示为textview,现在根本不起作用。
我只是对代码进行了一些细微的更改,只是为了显示收到的消息而无法发回消息。这些都摆脱了所有的写法。
这是原始代码
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("");
}
这就是我改变它的原因。
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);
这是logcat输出。
11-06 15:35:52.047: E/AndroidRuntime(19780): FATAL EXCEPTION: main
11-06 15:35:52.047:E / AndroidRuntime(19780):java.lang.RuntimeException:无法启动活动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):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2781) 11-06 15:35:52.047:E / AndroidRuntime(19780):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2797) 11-06 15:35:52.047:E / AndroidRuntime(19780):在android.app.ActivityThread.access $ 2300(ActivityThread.java:135) 11-06 15:35:52.047:E / AndroidRuntime(19780):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:2132) 11-06 15:35:52.047:E / AndroidRuntime(19780):在android.os.Handler.dispatchMessage(Handler.java:99) 11-06 15:35:52.047:E / AndroidRuntime(19780):在android.os.Looper.loop(Looper.java:143) 11-06 15:35:52.047:E / AndroidRuntime(19780):在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):引起: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):在android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1197) 11-06 15:35:52.047:E / AndroidRuntime(19780):在android.app.Activity.performStart(Activity.java:3822) 11-06 15:35:52.047:E / AndroidRuntime(19780):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2754)
进行的其他更改是删除handleMessage()方法中的sendMessage()方法和MESSAGE_WRITE大小写。
还删除了从BluetoothChatServices类
写入两个write(buffer [] byte)方法我一直在看这几天,真的无法弄清楚什么是错的。
答案 0 :(得分:1)
查看您的代码,它显示在您的原始代码中,您将R.id.in强制转换为ListView ..在新代码中,您将相同的资源(R.id.in)强制转换为TextView。可能发生的事情是您没有将R.id.in更改为布局文件中的TextView,并且您正在尝试将ListView转换为TextView,这将导致此错误。
错误
java.lang.ClassCastException: android.widget.ListView
应该给你一个很好的提示你正在寻找什么。 ClassCastException意味着您尝试将对象强制转换为无法转换为的类型。因此,最好的办法是寻找铸造物品的位置,以及logcat输出给你的行号。如果你在那里投射某些东西(TextView),你应该验证你正在投射和投射的类型都是正确的。