开发者

android:Handler from a Runnable throws Null Pointer Exception

I have a Thread that downloads data from internet

public class Bp implements Runnable
{
Handler myHandler;
public void setHandler(Handler myHandler)
{ this.myHandler=myHandler; }
....
myHandler.sendEmptyMessage (0);
}

There is an activity that needs to be updated according to downloaded data.

public class Hp extends Activity implements Runnable
{
....
public Handler myHandler = new Handler() {
  public void  handleMessage(Message msg) {
 //TODO handle myHandler from "Bp" Thread     
 //TODO remove Queue's View 
   if(m_adapter2.getCount ()==6)
   {
    m_adapter2.remove (开发者_StackOverflowqueue);         //removing view named queue from adapter
    m_adapter2.notifyDataSetChanged ();
   }
  }
};

Whenever I run above code I get NullPointer Exception .Please help me to update the view.


yes that's correct. You are creating the handler in the BP thread.. but what you need is to create it in the context of the main or GUI thread running in the HP activity because the MessageQueue of the main/GUI thread is in question here as you're updating it with data received from the other thread.


Solved the issue by rewriting the code neatly & it worked well.Seems earlier there were problems setting Handler.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜