ArrayAdapter requires the resource ID to be a TextView

久未见 提交于 2021-02-08 08:12:16

问题


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

mainactivity(chat):

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:


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.

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

See here



来源:https://stackoverflow.com/questions/35719300/arrayadapter-requires-the-resource-id-to-be-a-textview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!