gpt4 book ai didi

java - ArrayAdapter 要求资源 ID 为 TextView

转载 作者:行者123 更新时间:2023-11-30 01:31:20 25 4
gpt4 key购买 nike

应用程序运行正常,但是当我点击发送按钮时,模拟器很幸运地停止了,Logcat 中出现了这个错误: java.lang.IllegalStateException: ArrayAdapter 要求资源 ID 为 TextView。

我搜索了这个问题并找到了其他项目的解决方案,而不是与这个项目类似的

主要 Activity (聊天):

package com.example.doctor;

import android.app.Activity;
import android.content.Intent;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.AbsListView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

public class chat extends Activity {
private ChatArrayAdapter adp;
private ListView list;
private EditText chatText;
private Button send;
Intent in;
private Boolean side =false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chat1);

Intent i=getIntent();


send=(Button)findViewById(R.id.btn);
list=(ListView)findViewById(R.id.ListView);
adp=new ChatArrayAdapter(getApplicationContext(),R.layout.chat2);
chatText=(EditText)findViewById(R.id.chat);

chatText.setOnKeyListener(new OnKeyListener(){

@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if((event.getAction()==KeyEvent.ACTION_DOWN)&&(keyCode==KeyEvent.KEYCODE_ENTER))
{

return sendChatMessage();
}

return false;
}

});
send.setOnClickListener(new View.OnClickListener(){

@Override
public void onClick(View v) {
sendChatMessage();


}});

list.setTranscriptMode(AbsListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
list.setAdapter(adp);
adp.registerDataSetObserver(new DataSetObserver() {

public void OnChanged()
{

super.onChanged();
list.setSelection(adp.getcount()-1);
}
} );
}


private boolean sendChatMessage() {

adp.add(new ChatMessage(side,chatText.getText().toString()));
chatText.setText("");
side=!side;
return true;

}
}

聊天数组适配器

package com.example.doctor;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.Layout;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ChatArrayAdapter extends ArrayAdapter<ChatMessage> {

private TextView chatText;
private static List<ChatMessage> MessageList =new ArrayList<ChatMessage> ();

private LinearLayout layout;

public ChatArrayAdapter(Context context, int resource) {
super(context, resource, MessageList);
// TODO Auto-generated constructor stub
}



public void add(ChatMessage object) {

MessageList.add(object);
super.add(object);

}
public int getcount()
{
return this.MessageList.size();
}

public ChatMessage getitem(int index)
{
return this.MessageList.get(index);

}
public View getview(int position,View convertView,ViewGroup parent)
{
View v=convertView;
if(v==null)

{

LayoutInflater inflater= (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v=inflater.inflate(R.layout.chat2, parent, false);
}
layout =(LinearLayout)v.findViewById(R.id.messag1);
ChatMessage messageobj=getitem(position);
chatText=(TextView)v.findViewById(R.id.singlemessage);
chatText.setText(messageobj.message);
chatText.setBackgroundResource(messageobj.left ? R.drawable.pic_a :R.drawable.pic_b);

layout.setGravity(messageobj.left?Gravity.LEFT:Gravity.RIGHT);
return v;

}

public Bitmap decodeToBitmap(byte[]decodebyte)
{
return BitmapFactory.decodeByteArray(decodebyte, 0, decodebyte.length);

}



}

聊天2.xml:

  <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<LinearLayout
android:id="@+id/messag1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />

<TextView
android:id="@+id/singlemessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:background="@drawable/pic_b"
android:paddingLeft="10dip"
android:text="@string/hello"
android:textColor="@android:color/primary_text_light" />


</LinearLayout>

聊天1.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >




<ListView
android:id="@+id/ListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="80dp"
/>

<RelativeLayout
android:id="@+id/form"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:orientation="vertical" >


<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:ems="10"
android:id="@+id/chat"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="@+id/btn"
android:labelFor="@+id/chat"/>

<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/chat"
android:layout_alignParentRight="true"
android:text="@string/send" />

</RelativeLayout>
</RelativeLayout>

最佳答案

chat2.xml 必须包含一个 TextView,而不是一个 LinearLayout。现在您将 TextView 包裹在 LinearLayout 中,因此您将获得异常。

您通过调用 super(...) 在 ChatArrayAdapter 中使用的构造函数或 ArrayAdapter 需要 R.id。 用于 TextView,而不是 LinearLayout

参见 here

关于java - ArrayAdapter 要求资源 ID 为 TextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35719300/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com