这是我的错误家伙:
*** 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)
我正在尝试的是一个列表视图,由自定义列表项填充,每个列表项有多个视图,每个视图都附加了一个onclick侦听器.当这个onClickListener被按下时,它会向一个处理程序发送一个带有what和arg1参数的消息.
单击我的一个元素将触发启动新活动的意图.
点击另一个显示吐司.
当这些被组合按下时,我得到上面的错误.即点击文字来触发意图(然后按回车),然后点击图像显示吐司,然后当您点击文字再次触发意图时,我会得到FC.
我创建了一个演示项目,再现这个:
http://rapidshare.com/files/429227042/HandlerTest.rar
这里是下面的代码,我试图删除尽可能多的cruft,我可以得到的错误的骨头:
如果你想跳过什么重要的看看onClickListener在ConversationAdapter.class和他们如何与StartPage.class
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>
StartPage.class:
package com.handler.test;import java.util.ArrayList;import androID.app.ListActivity;import androID.app.ProgressDialog;import androID.content.Context;import androID.content.Intent;import androID.os.Bundle;import androID.os.Handler;import androID.os.Message;import androID.util.Log;import androID.Widget.Toast;public class StartPage extends ListActivity {private ArrayList<Conversation> mConversations = null;private ConversationAdapter mAdapter;private Context mContext;private ProgressDialog mProgressDialog;/** Called when the activity is first created. */@OverrIDepublic voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.main); mContext = this; mConversations = new ArrayList<Conversation>(); this.mAdapter = new ConversationAdapter(mContext,R.layout.inBox_row,mConversations,mHandler); setlistadapter(this.mAdapter); new Thread(new Runnable() { @OverrIDe public voID run() { getConversations(); } }).start(); mProgressDialog = ProgressDialog.show(StartPage.this,"Please wait...","RetrIEving data ...",true);}private voID getConversations() { try { mConversations = new ArrayList<Conversation>(); Conversation o1 = new Conversation(); o1.setStatus("SF services"); o1.setMessage("Pending"); mConversations.add(o1); } catch (Exception e) { Log.e("BACKGROUND_PROC",e.getMessage()); } runOnUiThread(returnRes);}private Runnable returnRes = new Runnable() { @OverrIDe public voID run() { if(mConversations != null && mConversations.size() > 0){ mAdapter.notifyDataSetChanged(); for(int i=0;i<mConversations.size();i++) mAdapter.add(mConversations.get(i)); } mProgressDialog.dismiss(); mAdapter.notifyDataSetChanged(); } };private Handler mHandler = new Handler(){ @OverrIDe public voID handleMessage(Message msg) { int convIndex = msg.arg1; int vIEwtouched = msg.what; switch(vIEwtouched){ case ConversationAdapter.PROF_ICON: showNumber(convIndex); break; case ConversationAdapter.MESSAGE: showMessageDetails(convIndex); break; } super.handleMessage(msg); }};private voID showNumber(int convIndex) { Toast.makeText(mContext,"pressed: "+convIndex,Toast.LENGTH_LONG).show();}private voID showMessageDetails(int convIndex) { final Conversation conv = mConversations.get(convIndex); Intent i = new Intent(mContext,DetailsPage.class); i.putExtra("someExtra",conv); startActivity(i);}}
DetailsPage.class
package com.handler.test;import androID.app.Activity;import androID.os.Bundle;import androID.util.Log;public class DetailsPage extends Activity {@OverrIDeprotected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.main); Log.i("Test","Details Page"); }}
Conversation.class:
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;}@OverrIDepublic 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;}}
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>
对不起,所有的代码!
解决方法 我的猜测是,你发送两次相同的信息.实际上在代码中有一个新的Message()和两个mHandler.sendMessage(m)可能都被执行.尝试在每次发送消息时发出新消息.
编辑:
Message.obtain()比Message m = new Message()更好(因为它回收了使用过的消息)
在您的情况下,如果需要现有消息的副本,则可以使用new.copyFrom(old).
总结以上是内存溢出为你收集整理的Android Handler Message和ListView全部内容,希望文章能够帮你解决Android Handler Message和ListView所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)