我的应用程序有两个选项卡。对于选项卡,我使用 fragment 。每个选项卡都有一个使用 Volley 的网络调用。对于 Volley 错误,我使用 Snackbar 来显示错误。我的问题是,如果我在触发错误时位于该特定 fragment 中,则 Snackbar 运行良好。如果我在错误触发此错误之前返回。
日志..
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)
fragment ...
@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));
}
snackbar ....
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);
}
最佳答案
你可以尝试在调用 SnackBar 时使用 Activity content View 作为你的参数,像这样:
View view = getActivity().findViewById(android.R.id.content);
Snackbar snackbar = Snackbar.make(view, "Please connect to the network!", Snackbar.LENGTH_LONG);
...
snackbar.show();
关于android - fragment 中的 Snackbar 给出 IllegalArgumentException : No suitable parent found from the given view,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53879037/