2010-11-06 21 views
9

मैं लंबे समय से इस पद के लिए अब माफी माँगता हूँ, लेकिन यह एक ही रास्ता मैं समझा सकता है, और गूगल परिणाम है कि निर्णायक नहीं होते हैं के 3 पृष्ठ प्रदान करता हैएंड्रॉयड हैंडलर संदेश और ListView

यहाँ मेरी त्रुटि लोग है:

*** 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) 

जो मैं कोशिश कर रहा हूं वह एक सूचीदृश्य है, जो कस्टम सूची आइटमों द्वारा पॉप्युलेट किया गया है, प्रत्येक सूची आइटम में कई दृश्य हैं और प्रत्येक दृश्य में एक ऑनक्लिक श्रोता संलग्न है। जब यह ऑनक्लिक लिस्टनर दबाया जाता है तो यह एक हैंडलर को एक संदेश और तर्क 1 तर्क के साथ संदेश भेजता है। मेरे तत्वों में से किसी एक पर क्लिक करने से एक नई गतिविधि शुरू करने का इरादा समाप्त हो जाता है। दूसरे पर क्लिक करना एक टोस्ट दिखाता है। जब इन्हें संयोजन में दबाया जाता है तो मुझे उपरोक्त त्रुटि मिलती है। अर्थात् इरादे को आग लगाने के लिए टेक्स्ट पर क्लिक करके, (फिर वापस दबाएं) फिर टोस्ट दिखाने के लिए छवि पर क्लिक करके, फिर जब आप फिर से इरादे को आग लगाने के लिए टेक्स्ट पर क्लिक करते हैं तो मुझे एफसी मिल जाता है।

मैं एक डेमो परियोजना है कि इस reproduces बनाया है:

: http://rapidshare.com/files/429227042/HandlerTest.rar

और यहाँ कोड के नीचे है, मैं के रूप में ज्यादा cruft दूर करने के लिए मैं कर सकता के रूप में त्रुटि की हड्डियों को प्राप्त करने की कोशिश आप onClickListener के ConversationAdapter.class में कम से क्या महत्वपूर्ण देखो करने के लिए छोड़ना चाहते हैं और वे कैसे StartPage.class साथ

एंड्रॉयड प्रकट इंटरैक्ट करते हैं तो:

<?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. */ 
@Override 
public 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 { 


@Override 
protected 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; 
} 

@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; 
} 
} 

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> 

सभी कोड के लिए खेद है!

उत्तर

32

मेरा अनुमान है कि आप एक ही संदेश को दो बार भेज रहे हैं। दरअसल कोड में एक new Message() और दो mHandler.sendMessage(m) है जो संभवतः दोनों निष्पादित हैं।

हर बार जब आप कोई संदेश भेजते हैं तो एक नया संदेश बनाने का प्रयास करें।

संपादित:

Message.obtain()Message m = new Message() के लिए बेहतर

अपने मामले तुम new.copyFrom(old) इस्तेमाल कर सकते हैं तो आप मौजूदा संदेश की एक प्रति की जरूरत है में (क्योंकि यह हुड के नीचे प्रयुक्त संदेशों recycles) है।

+0

हम्म यह काम करता है, मैं संसाधनों को बचाने की कोशिश कर रहा था (हाँ एक छोटी राशि) केवल एक प्रति दृश्य करके, प्रत्येक बार ऑनक्लिक दबाया जाता है। मैं देख सकता हूं कि दोनों को निष्कर्ष निकाला जाने पर एक त्रुटि कैसे फेंक दी जाएगी। लेकिन लेआउट व्यू के साथ मैं इसे संभव नहीं देख सकता, एचटी ओएस लापता होना चाहिए यह साफ है? किसी भी तरह से यह अब काम कर रहा है। नए संदेश() को ऑनक्लिक में ले जाकर। धन्यवाद! – Blundell

+0

आह महान ऐसा नहीं किया, धन्यवाद! नया संदेश() संदेश में बदल गया .obtain() – Blundell

संबंधित मुद्दे