ArrayAdapter要求资源ID为TextView [英] ArrayAdapter requires the resource ID to be a TextView

查看:125
本文介绍了ArrayAdapter要求资源ID为TextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

该应用正常运行,但是当我单击发送"按钮时,仿真器幸运地停止了运行,并且Logcat中出现此错误:java.lang.IllegalStateException:ArrayAdapter要求资源ID为TextView.

the app run ordinary but when i click in button send ,emulator has fortunatly stopped and this error in Logcat: java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView.

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

i search for this problem and found solution for other project and not similer to this project

主要活动(聊天):

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

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:

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:

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>

推荐答案

chat2.xml必须包含TextView,而不是LinearLayout.现在,您已将TextView包裹在LinearLayout中,因此将获得Exception.

chat2.xml must contain a TextView, not a LinearLayout. Right now you have your TextView wrapped in a LinearLayout, therefore you are getting the Exception.

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

The constructor or ArrayAdapter you are using in ChatArrayAdapter by calling super(...) expects the R.id. for a TextView, not LinearLayout.

请参见 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆