1. 程式人生 > >【Debug-Snackbar】在Fragment中使用Snackbar報錯

【Debug-Snackbar】在Fragment中使用Snackbar報錯

在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();