У меня в приложении есть две вкладки.Для вкладок я использую фрагменты.Каждая вкладка имеет сетевой вызов с использованием Volley.Для ошибки залпа я использую Snackbar, чтобы показать ошибку.Моя проблема в том, что Snackbar работает хорошо, если я нахожусь в этом конкретном фрагменте, в то время как ошибка вызывает.Если я вернусь до появления ошибки, эта ошибка появится.
Logcat ..
12-21 09:11:18.782 3707-3707/com.dushanmadushanka.slicdashboard.slicdashboard E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.dushanmadushanka.slicdashboard.slicdashboard, PID: 3707
java.lang.IllegalArgumentException: No suitable parent found from the given view. Please provide a valid view.
at android.support.design.widget.Snackbar.make(Snackbar.java:181)
at com.dushanmadushanka.slicdashboard.slicdashboard.other.VolleyErrorHandle.handleVolleyErrorThree(VolleyErrorHandle.java:102)
at com.dushanmadushanka.slicdashboard.slicdashboard.fragment.general.GeneralRegionalFragment$4.onErrorResponse(GeneralRegionalFragment.java:288)
at com.android.volley.Request.deliverError(Request.java:617)
at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:104)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Фрагмент ...
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
final View lView = inflater.inflate(R.layout.fragment_life_regional, container, false);
getTableJsonData(1, AppConfig.URL_GET, 1, lView);
return lView;
}
Метод ...
private void getTableJsonData(int currentMonth, String mUrl, int screenType, final View lView) {
StringRequest stringRequest = new StringRequest(Request.Method.GET, newUrl, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if (requestQueue != null) {
myList.clear();
lrAdapter.setJsonData(myList);
VolleyErrorHandle.handleVolleyErrorThree(error, progressBar, lView.findViewById(R.id.fragment_life_region_layout));
}
}
});
//AppController.getInstance().addToRequestQueue(stringRequest, TAG_REQ);
requestQueue.add(stringRequest);
stringRequest.setRetryPolicy(new DefaultRetryPolicy(15 * 1000, 1,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
}
Снэк-бар ....
public static void handleVolleyErrorThree(VolleyError error, ProgressBar p, View view) {
if (error instanceof TimeoutError || error instanceof NoConnectionError) {
Snackbar snackbar = Snackbar.make(view, "Please connect to the network!", Snackbar.LENGTH_LONG);
View snackbarView = snackbar.getView();
TextView textView = snackbarView.findViewById(android.support.design.R.id.snackbar_text);
textView.setTextColor(Color.WHITE);
snackbarView.setBackgroundColor(Color.RED);
snackbar.show();
} else if (error instanceof AuthFailureError) {
Snackbar snackbar = Snackbar.make(view, "Authentication error!", Snackbar.LENGTH_LONG);
View snackbarView = snackbar.getView();
TextView textView = snackbarView.findViewById(android.support.design.R.id.snackbar_text);
textView.setTextColor(Color.WHITE);
snackbarView.setBackgroundColor(Color.RED);
snackbar.show();
} else if (error instanceof ServerError) {
Snackbar snackbar = Snackbar.make(view, "No data!", Snackbar.LENGTH_LONG);
View snackbarView = snackbar.getView();
TextView textView = snackbarView.findViewById(android.support.design.R.id.snackbar_text);
textView.setTextColor(Color.WHITE);
snackbarView.setBackgroundColor(Color.RED);
snackbar.show();
} else if (error instanceof NetworkError) {
Snackbar snackbar = Snackbar.make(view, "Network error!", Snackbar.LENGTH_LONG);
View snackbarView = snackbar.getView();
TextView textView = snackbarView.findViewById(android.support.design.R.id.snackbar_text);
textView.setTextColor(Color.WHITE);
snackbarView.setBackgroundColor(Color.RED);
snackbar.show();
} else if (error instanceof ParseError) {
Snackbar snackbar = Snackbar.make(view, "Database connection error!", Snackbar.LENGTH_LONG);
View snackbarView = snackbar.getView();
TextView textView = snackbarView.findViewById(android.support.design.R.id.snackbar_text);
textView.setTextColor(Color.WHITE);
snackbarView.setBackgroundColor(Color.RED);
snackbar.show();
}
p.setVisibility(View.GONE);
}