2016-03-01 3 views
0

приложение запускалось обычным, но когда я нажимаю кнопку «отправить», эмулятор успешно остановился, и эта ошибка в Logcat: java.lang.IllegalStateException: ArrayAdapter требует, чтобы идентификатор ресурса был TextView ,ArrayAdapter требует, чтобы идентификатор ресурса был TextView

я искать эту проблему и нашел решение для другого проекта и не similer этого проекта

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> 
+0

вы можете добавить код для класса ChatArrayAdapter и макета чата 2? – Mo1989

+0

показать это снова, добавить его. –

ответ

1

chat2.xml должен содержать TextView, а не LinearLayout. Прямо сейчас у вас есть TextView, завернутый в LinearLayout, поэтому вы получаете Exception.

Конструктор или ArrayAdapter вы используете в ChatArrayAdapter по телефону super(...) ожидает, что R.id. для TextView, не LinearLayout.

См here

+0

содержит как TextView, так и LinearLayout –

+0

по вашим словам .delete LinearLayout !! –

+0

Да. Удалите окружающие макеты. – barq

Смежные вопросы