【Debug-Snackbar】在Fragment中使用Snackbar報錯
阿新 • • 發佈:2018-12-14
在Fragment中使用Snackbar報錯
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.***.****/vertu.assistant.activity.****Activity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.view.ViewGroup.getContext()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2315) ... Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.view.ViewGroup.getContext()' on a null object reference at com.androidadvance.topsnackbar.TSnackbar.<init>(TSnackbar.java:129)
將程式碼改為
Snackbar.make(getActivity().findViewById(android.R.id.content), "Hello!", Snackbar.LENGTH_LONG).show();