Android处理程序消息和列表视图
这是我的错误:Android处理程序消息和列表视图,android,listview,message-queue,message,handler,Android,Listview,Message Queue,Message,Handler,这是我的错误: *** Uncaught remote exception! (Exceptions are not yet supported across processes.) android.util.AndroidRuntimeException: { what=1008 when=368280372 } This message is already in use. at android.os.MessageQueue.enqueueMessage(MessageQueue.jav
*** Uncaught remote exception! (Exceptions are not yet supported across processes.)
android.util.AndroidRuntimeException: { what=1008 when=368280372 } This message is already in use.
at android.os.MessageQueue.enqueueMessage(MessageQueue.java:171)
at android.os.Handler.sendMessageAtTime(Handler.java:457)
at android.os.Handler.sendMessageDelayed(Handler.java:430)
at android.os.Handler.sendMessage(Handler.java:367)
at android.view.ViewRoot.dispatchAppVisibility(ViewRoot.java:2748)
我试图创建一个由自定义列表项填充的listview,每个列表项都有多个视图,每个视图都附加了一个onclick侦听器。当按下此onClickListener时,它会向处理程序发送一条消息,其中包含what和arg1参数。
单击其中一个“我的元素”将激发启动新活动的意图。
单击另一个按钮表示干杯。
当这些组合按下时,我得到上面的错误。也就是说,单击文本以激发意图,(然后按back),然后单击图像以显示烤面包,然后当您再次单击文本以激发意图时,我将获得FC
这是下面的代码,我试图尽可能多地去除一些瑕疵,以找出错误的根源:
如果您想跳到什么是重要的,请查看ConversationAdapter.class中的onClickListener,以及它们如何与StartPage.class交互
package com.handler.test;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class DetailsPage extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.i("Test", "Details Page");
}
}
Android清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.handler.test"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".StartPage"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".DetailsPage"
android:label="DetailsPage"
>
</activity>
</application>
<uses-sdk android:minSdkVersion="3" />
</manifest>
对话.课堂:
package com.handler.test;
import java.io.Serializable;
public class Conversation implements Serializable {
private static final long serialVersionUID = -437261671361122258L;
private String status;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
ConversationAdapter.class:
package com.handler.test;
import java.util.ArrayList;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class ConversationAdapter extends ArrayAdapter<Conversation> {
public static final int PROF_ICON = 0;
public static final int MESSAGE = 1;
private Context mContext;
private Handler mHandler;
private ArrayList<Conversation> mItems;
private int mXmlId;
private LinearLayout detailsOfConv;
private ImageView iconImage;
public ConversationAdapter(Context context, int textViewResourceId, ArrayList<Conversation> items, Handler handler) {
super(context, textViewResourceId, items);
this.mContext = context;
this.mItems = items;
this.mXmlId = textViewResourceId;
this.mHandler = handler;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(mXmlId, null);
}
final Message m = new Message();
m.arg1 = position;
Conversation c = mItems.get(position);
if (c != null) {
iconImage = (ImageView) v.findViewById(R.id.icon);
if (iconImage != null) {
iconImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
m.what = PROF_ICON;
mHandler.sendMessage(m);
}
});
}
detailsOfConv = (LinearLayout) v.findViewById(R.id.details);
if(detailsOfConv != null){
detailsOfConv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
m.what = MESSAGE;
mHandler.sendMessage(m);
}
});
}
}
return v;
}
}
package com.handler.test;
导入java.util.ArrayList;
导入android.content.Context;
导入android.os.Handler;
导入android.os.Message;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.ArrayAdapter;
导入android.widget.ImageView;
导入android.widget.LinearLayout;
公共类ConversationAdapter扩展了ArrayAdapter{
公共静态最终int PROF_图标=0;
公共静态最终int消息=1;
私有上下文;
私人经理人;
私有数组列表;
私有int mXmlId;
私人线路布置详图;
私有图像视图图像;
公共会话适配器(上下文上下文、int textViewResourceId、ArrayList项、处理程序){
super(上下文、textViewResourceId、项);
this.mContext=上下文;
this.ms=项目;
this.mXmlId=textViewResourceId;
this.mHandler=handler;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视图v=转换视图;
如果(v==null){
LayoutInflater vi=(LayoutInflater)mContext.getSystemService(Context.LAYOUT\u INFLATER\u SERVICE);
v=vi.充气(mXmlId,空);
}
最终消息m=新消息();
m、 arg1=位置;
对话c=mItems.get(位置);
如果(c!=null){
iconImage=(ImageView)v.findViewById(R.id.icon);
if(iconImage!=null){
iconImage.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
m、 什么=教授图标;
mHandler.sendMessage(m);
}
});
}
detailsOfConv=(LinearLayout)v.findviewbyd(R.id.details);
if(detailsOfConv!=null){
detailsOfConv.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
m、 什么=信息;
mHandler.sendMessage(m);
}
});
}
}
返回v;
}
}
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
>
<ListView
android:id="@+id/android:list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:cacheColorHint="#00000000"
/>
</LinearLayout>
inbox_row.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="?android:attr/listPreferredItemHeight"
android:padding="6dip">
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="6dip"
android:src="@drawable/icon" />
<LinearLayout
android:id="@+id/details"
android:orientation="vertical"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="fill_parent">
<TextView
android:id="@+id/toptext"
android:textColor="#99FF66"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:singleLine="true"
android:text="123456789"
/>
</LinearLayout>
</LinearLayout>
我猜您发送了两次相同的消息。实际上,在代码中有一个
newmessage()
和两个mHandler.sendMessage(m)
,它们可能都被执行
尝试在每次发送邮件时生成新邮件
编辑:
最好是(因为它在引擎盖下回收使用过的邮件)
在您的情况下,如果您需要现有消息的副本,您可以使用。Hmm这确实有效,我试图通过每个视图使用一个来节省资源(是的,数量很小),而不是每次按下onclick时都使用一个。我可以看到如果两个都执行了,将如何抛出错误。但在布局视图中,我看不到这是可能的,ht操作系统肯定缺少它的清理?无论哪种方式,它现在都在发挥作用。将新消息()移动到onClick中。谢谢啊,太好了,不,谢谢!新消息()已更改为Message.get()
<?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="?android:attr/listPreferredItemHeight"
android:padding="6dip">
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="6dip"
android:src="@drawable/icon" />
<LinearLayout
android:id="@+id/details"
android:orientation="vertical"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="fill_parent">
<TextView
android:id="@+id/toptext"
android:textColor="#99FF66"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:singleLine="true"
android:text="123456789"
/>
</LinearLayout>
</LinearLayout>