从后台线程

时间:2017-05-10 22:22:35

标签: android runnable ui-thread

我正在尝试从runnable中更新我的UI线程。它工作正常,直到我通过按下后退按钮来中断该方法。当发生这种情况时,它会从NPE崩溃。如果按下后退按钮,我需要一种方法来阻止此线程更新。谢谢!

                  view.post ( uiRun = new Runnable() {
                        public void run() {
                                /* UI update */

                                areasAdapter2 = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, CL);
                                areasAdapter2.setDropDownViewResource(android.R.layout.simple_list_item_1);
                                mContactLog.setAdapter(areasAdapter2);
                        }
                    });

05-10 18:26:00.610 13511-13511 / com.example.bryan.customer84 E / UncaughtException:java.lang.NullPointerException:尝试调用虚方法'java.lang.Object android.content.Context.getSystemService (null。对象引用上的(java.lang.String)'                                                                                      在android.widget.ArrayAdapter.init(ArrayAdapter.java:310)                                                                                      在android.widget.ArrayAdapter。(ArrayAdapter.java:153)                                                                                      在com.example.bryan.customer84.PhoneSearch $ 9.run(PhoneSearch.java:606)                                                                                      在android.os.Handler.handleCallback(Handler.java:739)                                                                                      在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                      在android.os.Looper.loop(Looper.java:145)                                                                                      在android.app.ActivityThread.main(ActivityThread.java:5835)                                                                                      at java.lang.reflect.Method.invoke(Native Method)                                                                                      在java.lang.reflect.Method.invoke(Method.java:372)                                                                                      在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1399)                                                                                      在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

1 个答案:

答案 0 :(得分:0)

您正在尝试访问已经垃圾收集的活动...在run(()中为getActivity()添加空检查!= null