首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何修复Android片段中的“只有创建视图层次结构的原始线程才能触摸其视图”?

如何修复Android片段中的“只有创建视图层次结构的原始线程才能触摸其视图”?
EN

Stack Overflow用户
提问于 2019-05-10 01:49:39
回答 1查看 661关注 0票数 0

我的代码中有一个问题

代码语言:javascript
复制
public class StatusFragment extends Fragment {



    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.fragment_status, container, false);

        //Untuk menjalankan command dari API
        {
            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {

                    final TextView tv0 = (TextView) view.findViewById(R.id.textView0);
                    final TextView tv1 = (TextView) view.findViewById(R.id.textView1);
                    final TextView tv2 = (TextView) view.findViewById(R.id.textView2);
                    final TextView tv3 = (TextView) view.findViewById(R.id.textView3);
                    final TextView tv4 = (TextView) view.findViewById(R.id.textView4);
                    final TextView tv5 = (TextView) view.findViewById(R.id.textView5);
                    final TextView tv6 = (TextView) view.findViewById(R.id.textView6);
                    final TextView tv7 = (TextView) view.findViewById(R.id.textView7);
                    final TextView tv8 = (TextView) view.findViewById(R.id.textView8);

                    ApiConnection con = MainActivity.getCon();
                    if (con !=null)
                        try {

                            //Untuk Menampilkan Resource dari Mikrotik
                            String rs = con.execute("/system/resource/print interval=3",
                                    new ResultListener() {

                                        public void receive(Map<String, String> result) {
                                            tv0.setText(result.get("platform"));
                                            tv1.setText(result.get("board-name"));
                                            tv2.setText(result.get("version"));
                                            tv3.setText(result.get("uptime"));
                                            tv4.setText(String.format("%s %%", result.get("cpu-load")));
                                            tv5.setText(String.format("%s MB", Integer.parseInt(result.get("free-memory")) / (1024*1024)));
                                            tv6.setText(String.format("%s MB", Integer.parseInt(result.get("total-memory")) / (1024*1024)));
                                            tv7.setText(String.format("%s MB", Integer.parseInt(result.get("free-hdd-space")) / (1024*1024)));
                                            tv8.setText(String.format("%s MB", Integer.parseInt(result.get("total-hdd-space")) / (1024*1024)));
                                        }

                                        public void error(MikrotikApiException e) {
                                            System.out.println("An error occurred: " + e.getMessage());
                                        }

                                        public void completed() {
                                            System.out.println("Asynchronous command has finished");
                                        }
                                    }

                            );


                        } catch (MikrotikApiException e) {
                            e.printStackTrace();
                        }
                }
            });


        }

        return view;
    }

}

05-10 08:26:58.797 7122 -7164/com.tasanahetech.mikboboxv2E/AndroidRuntime:致命异常: Mikrotik结果处理器进程: com.tasanahetech.mikroboxv2,PID: 7122 com.tasanahetech.mikroboxv2只有创建视图层次结构的原始线程才能触摸其视图。在android.view.ViewRootImpl.checkThread(ViewRootImpl.java:6556) at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:907) at android.view.View.requestLayout(View.java:18722) at android.view.View.requestLayout(View.java:18722) at android.view.View.requestLayout(View.java:18722) at android.view.View.requestLayout(View.java:18722)在android.view.View.requestLayout(View.java:18722) at android.view.View.requestLayout(View.java:18722) at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:360) at android.view.View.requestLayout(View.java:18722) at android.view.View.requestLayout(View.java:18722) at android.support.constraint.ConstraintLayout.requestLayout(ConstraintLayout.java:3172)在android.view.View.requestLayout(View.java:18722) at android.view.View.requestLayout(View.java:18722) at android.view.View.requestLayout(View.java:18722) at android.widget.TextView.checkForRelayout(TextView.java:7172) at android.widget.TextView.setText(TextView.java:4342) at android.widget.TextView.setText(TextView.java:4199)在android.widget.TextView.setText(TextView.java:4174) at com.tasanahetech.mikroboxv2.StatusFragment$1$1.receive(StatusFragment.java:57) at com.tasanahetech.mikroboxv2.api.impl.ApiConnectionImpl$Processor.run(ApiConnectionImpl.java:258)

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-10 02:20:32

解决问题的最快方法是将所有"tv0.setText()“调用封装到另一个"runOnUiThread”块中。如下所示:

代码语言:javascript
复制
public void receive(Map<String, String> result) {
  getActivity().runOnUiThread(new Runnable() {
    @Override
    public void run() {
      tv0.setText(result.get("platform"));
      tv1.setText(result.get("board-name"));
      tv2.setText(result.get("version"));
      tv3.setText(result.get("uptime"));
      tv4.setText(String.format("%s %%", result.get("cpu-load")));
      tv5.setText(String.format("%s MB", Integer.parseInt(result.get("free-memory")) / (1024*1024)));
      tv6.setText(String.format("%s MB", Integer.parseInt(result.get("total-memory")) / (1024*1024)));
      tv7.setText(String.format("%s MB", Integer.parseInt(result.get("free-hdd-space")) / (1024*1024)));
      tv8.setText(String.format("%s MB", Integer.parseInt(result.get("total-hdd-space")) / (1024*1024)));
  });
}

并且可以删除外部的"getActivity().runOnUiThread“。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56069686

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档