0

私のアプリケーションでは、60秒ごとにコードを実行したいのですが、これはサーバーからデータを取得するための私のコードです。
このために、以下のコードを書きます。
しかし、コードを実行すると60 秒後に、 ForceCloseエラーが表示され、以下のメッセージが に表示されlogCatます。

フラグメントへのビューのエラーを表示してください!

私のコード:

public class MainReminderFragment extends Fragment {

    @BindView(R.id.mainExplore_noExploreTxt)
    TextView mainExplore_noExploreTxt;
    @BindView(R.id.newsPageLoadLay)
    RelativeLayout newsPageLoadLay;
    @BindView(R.id.toolbarTitleTxt)
    TextView toolbarTitleTxt;
    private Context context;
    public List<Datum> model = new ArrayList<>();
    public ReminderAdapter reminderAdapter;
    private SharedPrefrencesHandler prefrencesHandler;
    private String token = "";
    private LinearLayoutManager layoutManager;
    private MainActivity mainActivity;
    private InterfaceApi api;
    private boolean isRunning = false;
    public ProgressBar mainExplore_progressBar;
    public RecyclerView mainExplore_recyclerView;

    public MainReminderFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_main_reminder, container, false);
        //Initialize
        ButterKnife.bind(this, view);
        context = getActivity();
        prefrencesHandler = new SharedPrefrencesHandler(context);
        reminderAdapter = new ReminderAdapter(context, model);
        layoutManager = new LinearLayoutManager(context);
        mainExplore_progressBar = view.findViewById(R.id.mainExplore_progressBar);
        mainExplore_recyclerView = view.findViewById(R.id.mainExplore_recyclerView);
        isRunning = true;
        mainActivity = (MainActivity) getActivity();
        api = ApiClient.getClient().create(InterfaceApi.class);
        //Toolbar name
        toolbarTitleTxt.setText(context.getResources().getString(R.string.reminder));
        //RecyclerView
        mainExplore_recyclerView.setLayoutManager(layoutManager);
        mainExplore_recyclerView.setHasFixedSize(true);
        //Get token
        token = prefrencesHandler.getFromShared(SharedPrefrencesKeys.TOKEN.name());

        //Get data
        getData();
        if (getActivity() != null) {
            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            while (true) {
                                try {
                                    Thread.sleep(60000);
                                } catch (Exception e) {
                                }
                                if (isRunning) {
                                    getData();
                                }
                            }
                        }
                    }).start();
                }
            });
        }
        return view;
    }

    //SendData method
    private SerialReminderListSendData sendData(int page) {
        SerialReminderListSendData listSendData = new SerialReminderListSendData();
        listSendData.setPageIndex(page);
        listSendData.setPageSize(200);

        return listSendData;
    }

    //Get data
    private void getData() {
        Call<SerialReminderListResponse> call = api.getListSerialReminder(token, "2", sendData(1));

        mainExplore_progressBar.setVisibility(View.VISIBLE);

        call.enqueue(new Callback<SerialReminderListResponse>() {
            @Override
            public void onResponse(Call<SerialReminderListResponse> call, Response<SerialReminderListResponse> response) {
                if (response.body().getData() != null && response.body().getStatusCode() != 401
                        && response.body().getStatusCode() != 402) {
                    if (response.body().getData().size() > 0) {
                        model.clear();
                        model.addAll(response.body().getData());
                        reminderAdapter.notifyDataSetChanged();
                        mainExplore_recyclerView.setAdapter(reminderAdapter);
                        //Gone no explore
                        mainExplore_noExploreTxt.setVisibility(View.GONE);
                    } else {
                        mainExplore_noExploreTxt.setVisibility(View.VISIBLE);
                        mainExplore_recyclerView.setVisibility(View.GONE);
                    }
                }

                mainExplore_progressBar.setVisibility(View.GONE);
            }

            @Override
            public void onFailure(Call<SerialReminderListResponse> call, Throwable t) {
                mainExplore_progressBar.setVisibility(View.GONE);
            }
        });
    }
}

logCat のエラー メッセージ:

android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:7130)
at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:1115)
at android.view.ViewGroup.invalidateChild(ViewGroup.java:5284)
at android.view.View.invalidateInternal(View.java:13615)
at android.view.View.invalidate(View.java:13579)
at android.view.View.setFlags(View.java:11422)
at android.view.View.setVisibility(View.java:8014)
at com.app.test.Fragments.MainPageFrags.MainReminderFragment.getData(MainReminderFragment.java:182)
at com.app.test.Fragments.MainPageFrags.MainReminderFragment.access$100(MainReminderFragment.java:47)
at com.app.test.Fragments.MainPageFrags.MainReminderFragment$1$1.run(MainReminderFragment.java:142)
at java.lang.Thread.run(Thread.java:761)

どうすれば修正できますか?私を助けてください

4

3 に答える 3

0

このコードを試してください:

if (getActivity() != null) {
    Thread thread = new Thread() {
        @Override
        public void run() {
            float i;
            try {
                for (i = 0; i <= 100; i++) {
                    getActivity().runOnUiThread(new Runnable() {
                        public void run() {
                            getData();
                        }
                    });
                    sleep(6000);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    };
    thread.start();
}
于 2018-05-11T09:16:16.853 に答える