我的代码中有一个问题
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)
谢谢
发布于 2019-05-10 02:20:32
解决问题的最快方法是将所有"tv0.setText()“调用封装到另一个"runOnUiThread”块中。如下所示:
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“。
https://stackoverflow.com/questions/56069686
复制相似问题