ArrayAdapter要求资源ID为TextView

时间:2016-03-01 09:38:57

标签: java android arrays xml android-arrayadapter

应用程序运行普通但是当我点击按钮发送时,模拟器已经幸运地停止并且在Logcat中出现此错误:        java.lang.IllegalStateException:ArrayAdapter要求资源ID为TextView。

我搜索这个问题,找到了其他项目的解决方案,而不是这个项目的模拟器

mainactivity(聊天):

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;

  }
  }

ChatArrayAdapter

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

   }



    }

chat2.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>

chat1.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>

1 个答案:

答案 0 :(得分:1)

chat2.xml必须包含TextView,而不是LinearLayout。现在你将TextView包装在LinearLayout中,因此你得到了Exception。

您在ArrayAdapter中通过调用ChatArrayAdapter使用的构造函数或super(...期望R.id.TextView,而不是LinearLayout

请参阅here