处理程序/循环器/向线程

时间:2017-01-22 18:51:41

标签: android multithreading handler android-looper

我写了一个测试应用程序来理解Threads之间的通信机制。

我的消息来源:

  

http://techtej.blogspot.de/2011/02/android-passing-data-between-main.html,   send message to Thread which is listening for data from network

我不明白如何使用Handler和Looper。

  

这里:(每一步都应该给出他的状态:Log.v(标签,“......”))

     
      
  • MainActivity创建并启动Thread(Runnable“r”)
  •   
  • MainActivity - >消息给 - > [R
  •   
  • MainActivity< - modified message< - r
  •   

    

package com.example.desktop.testhandlerlooper; import android.os.Handler; import android.os.Looper; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; public class MainActivity extends AppCompatActivity { private static final String TAG = "MyActivity"; private Handler mHandler2; private Handler mHandler; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mHandler = new Handler(){ @Override public void handleMessage(Message msg) { Log.v(TAG, (String)msg.obj); } }; mHandler.post(r); } @Override protected void onStart() { super.onStart(); for(int i = 10; i>=0; i--) { Message msg = mHandler2.obtainMessage(); msg.arg1 = 3; msg.obj = String.valueOf(i); mHandler2.sendMessage(msg); } } Runnable r = new Runnable() { private static final String TAG = "Thread"; @Override public void run() { Log.v(TAG, "Before the looper"); Looper.prepare(); //======================================= mHandler2 = new Handler() { public void handleMessage(Message msg) { synchronized (this) { Message m = mHandler.obtainMessage(); //Message message = Message.obtain(); m.obj = "ACK:" + msg.obj; Log.v(TAG, (String)m.obj); //message.sendToTarget(); mHandler.sendMessage(msg); } } }; //======================================= Looper.loop(); Log.v(TAG, "After the looper"); } }; }

Logcat:

  

---------崩溃开始E / AndroidRuntime:FATAL EXCEPTION:main   处理:com.example.desktop.testhandlerlooper,PID:2872   java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.desktop.testhandlerlooper / com.example.desktop.testhandlerlooper.MainActivity}:   java.lang.NullPointerException:尝试调用虚方法   'android.os.Message android.os.Handler.obtainMessage()'上的null   对象参考                         在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)                         在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)                         在android.app.ActivityThread.-wrap11(ActivityThread.java)                         在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1344)                         在android.os.Handler.dispatchMessage(Handler.java:102)                         在android.os.Looper.loop(Looper.java:148)                         在android.app.ActivityThread.main(ActivityThread.java:5417)                         at java.lang.reflect.Method.invoke(Native Method)                         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)                         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)                      引起:java.lang.NullPointerException:尝试调用虚方法'android.os.Message   android.os.Handler.obtainMessage()'对空对象引用                         在com.example.desktop.testhandlerlooper.MainActivity.onStart(MainActivity.java:35)                         在android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1237)                         在android.app.Activity.performStart(Activity.java:6253)                         在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)                         在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)                         在android.app.ActivityThread.-wrap11(ActivityThread.java)                         在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1344)                         在android.os.Handler.dispatchMessage(Handler.java:102)                         在android.os.Looper.loop(Looper.java:148)                         在android.app.ActivityThread.main(ActivityThread.java:5417)                         at java.lang.reflect.Method.invoke(Native Method)                         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726)                         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)   申请已终止。

除了我想做的事情,我理解错误但不知道如何解决它。也许是MainThread和ChildTread之间的同步?

1 个答案:

答案 0 :(得分:0)

您正在尝试在初始化之前使用mHandler2变量,如您在日志行中看到的: com.example.desktop.testhandlerlooper.MainActivity.onStart(MainActivity.java:35)

您应该首先声明和初始化变量,然后开始使用它们。