我发送msg回到主UI线程但失败了

时间:2014-05-04 16:18:11

标签: java android multithreading

我通过工作线程向主UI线程发送消息,但它给出了这些错误

05-04 11:59:00.889: E/AndroidRuntime(1156): FATAL EXCEPTION: main
05-04 11:59:00.889: E/AndroidRuntime(1156): Process: com.example.threadproject, PID: 1156
05-04 11:59:00.889: E/AndroidRuntime(1156): java.lang.NullPointerException
05-04 11:59:00.889: E/AndroidRuntime(1156):     at com.example.threadproject.MyThread.PostMsg(MyThread.java:113)
05-04 11:59:00.889: E/AndroidRuntime(1156):     at com.example.threadproject.MyThread.dials(MyThread.java:64)
05-04 11:59:00.889: E/AndroidRuntime(1156):     at com.example.threadproject.MainThread.Function(MainThread.java:39)
05-04 11:59:00.889: E/AndroidRuntime(1156):     at com.example.threadproject.MainThread.access$0(MainThread.java:35)
05-04 11:59:00.889: E/AndroidRuntime(1156):     at com.example.threadproject.MainThread$1.onClick(MainThread.java:28)
05-04 11:59:00.889: E/AndroidRuntime(1156):     at android.view.View.performClick(View.java:4438)
05-04 11:59:00.889: E/AndroidRuntime(1156):     at android.view.View$PerformClick.run(View.java:18422)

代码是

public class MyThread extends Thread
{

    Handler mHandler  ;
    Handler mHandlerMain = new Handler(Looper.getMainLooper());



    public void run() 
        {
            Looper.prepare();
            PostMainThreadMsg();                    
            mHandler = new Handler() 
            {
                public void handleMessage(Message msg) 
                {
                    Log.i("Message", "" + msg);
                    //PostMainThreadMsg();              
                }
             };

            Looper.loop();

        }


    ///////////////////////////////////////////////////////////////////////////////////////////////////////


        public class Dial implements Runnable
        {
            int m_nLine;


            public int m_bResult;

            public void run()
            {
                Log.v("dial","class run");

                m_nLine++;

                RunnableNotify(this);
            }
        }


        public int dials(int nLine) 
        {
            startThread();

            Dial m_objDial = new Dial();

            m_objDial.m_nLine = nLine;


            PostMsg(m_objDial);

            return m_objDial.m_bResult ;

        }

    public void PostMainThreadMsg()
    {
//      mHandlerMain .post(new Runnable() {
//          
//          @Override
//          public void run() {
//              // TODO Auto-generated method stub
//              Message msg = Message.obtain();
//              msg.arg1 = 5;
//              mHandlerMain.sendMessage(msg);
//              
//          }
//      }); super.start();

        Runnable n_obj = new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                Message msg =  Message.obtain();
                msg.arg1 = 20;  
                mHandlerMain.sendMessage(msg);



            }
        };
        PostMssg(n_obj);


    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////


        public void PostMssg(Runnable runnable)
        {
            mHandlerMain.post(runnable);

        }

        public void PostMsg(Runnable runnable)
        {
            mHandler.post(runnable);

            RunnableWait(runnable);


        }

        public void startThread()
        {

            super.start();

        }

2 个答案:

答案 0 :(得分:0)

Dials中启动该主题,然后调用使用PostMsg的{​​{1}}。但mHandler仅在线程中初始化(在您的第一个mHandler方法中)。

所以有一场比赛;无法保证初始化将在使用前发生。 (无论如何,如果你在一个线程中分配一个变量,结果可能在另一个线程中不可见。)

这不难诊断 - 你知道run为空,所以你只需要回过头来查看它应该在哪里初始化,然后看看是否有任何方法可以使初始化没有发生爱好。

答案 1 :(得分:0)

如果我是你,我会使用AsyncTask 管理流程也更容易 如有任何问题,请随时评论:)