have a first question in my career in stackoverflow. I hope that are you help me.
What's happen:
I have Fragment where i want to set some recyclerView with some dates, it worked until I wanted to do ProgressDialog with some ErrorMgs.
This is my onCreateView:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.recycler_view, container, false);
recyclerView = (RecyclerView) view.findViewById(R.id.my_recycler_view);
textViewErrorMsg = (TextView) view.findViewById(R.id.tv_error_message);
progressBarLoading = (ProgressBar) view.findViewById(R.id.pb_loading);
new TakeDates().execute();
return view;
}
I add AsyncTask:
@SuppressLint("StaticFieldLeak")
class TakeDates extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(getActivity());
progressDialog.getWindow().clearFlags(
WindowManager.LayoutParams.FLAG_DIM_BEHIND);
progressDialog.setMessage("Loading places...");
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.show();
}
@Override
protected Void doInBackground(Void... voids) {
try {
Thread.sleep(2200);
populateFromJson();
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
if (progressDialog.isShowing())
progressDialog.dismiss();
}
}
And the last method is populateFromJson:
private void populateFromJson() {
ConnectivityManager conMgr = (ConnectivityManager) getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
assert conMgr != null;
NetworkInfo i = conMgr.getActiveNetworkInfo();
if (i != null && i.isConnected() && i.isAvailable()) {
ApiService api = RetroClient.getApiService(PLACES_URL);
Call<PlaceList> call = api.getMyJSON();
call.enqueue(new Callback<PlaceList>() {
@Override
public void onResponse(Call<PlaceList> call, Response<PlaceList> response) {
if (response.isSuccessful()) {
itemList = response.body().getItemList();
adapter = new RecyclerViewPlaceAdapter(itemList, getContext());
recyclerView.setAdapter(adapter);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
showPlaces();
} else {
showErrorMessage("Try to restart your app.");
}
}
@Override
public void onFailure(Call<PlaceList> call, Throwable t) {
showErrorMessage("Here is the main error: " + t.getMessage() + "\nTry to restart your app.");
}
});
} else {
showErrorMessage("Internet Connection Not Available" + "\nTry to find better connection to the internet.");
}
}
Now about Errors:
1) Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
I understand that the main part of the Error is here:
After added SLEEP Make ERROR
@Override
protected Void doInBackground(Void... voids) {
try {
Thread.sleep(2200);
populateFromJson();
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}