Search code examples
androidandroid-snackbar

Starting intent from snackbar in a diffrent class causing errors


I have a snackbar utility class which is called when the internet is not working.

 public void noInternetSnackBar(Activity activity){
        Snackbar snackbar = Snackbar.make(activity.findViewById(android.R.id.content), "Your internet is not working", Snackbar.LENGTH_LONG);
        snackbar.setActionTextColor(Color.YELLOW);
        snackbar.setAction("Settings", v -> {
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.setComponent(new ComponentName("com.android.settings",
                    "com.android.settings.Settings$DataUsageSummaryActivity"));
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
        });
        snackbar.show();
    }

the snackbar is displayed properly but on clicking the action the follwing error occurs

   java.lang.NullPointerException: Attempt to invoke virtual method 'android.app.ActivityThread$ApplicationThread android.app.ActivityThread.getApplicationThread()' on a null object reference
        at android.app.Activity.startActivityForResult(Activity.java:4488)
        at android.support.v4.app.BaseFragmentActivityApi16.startActivityForResult(BaseFragmentActivityApi16.java:54)
        at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:68)
        at android.app.Activity.startActivityForResult(Activity.java:4445)
        at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:751)
        at android.app.Activity.startActivity(Activity.java:4806)
        at android.app.Activity.startActivity(Activity.java:4774)
        at in.reevtech.rtpl_sales.utils.AlertUtil.lambda$noInternetSnackBar$0$AlertUtil(AlertUtil.java:57)
        at in.reevtech.rtpl_sales.utils.AlertUtil$$Lambda$0.onClick(Unknown Source:21)
        at android.support.design.widget.Snackbar$1.onClick(Snackbar.java:255)
        at android.view.View.performClick(View.java:6294)
        at android.view.View$PerformClick.run(View.java:24770)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

Solution

  • You need to check if data settings activity is present or not.

    Try this

    private static final String SETTINGS_PACKAGE = "com.android.settings";
    private static final String SETTINGS_CLASS_DATA_USAGE_SETTINGS = "com.android.settings.Settings$DataUsageSummaryActivity";
    
    try {
                final Intent intent = new Intent(Intent.ACTION_MAIN, null);
                final ComponentName cn = new ComponentName(SETTINGS_PACKAGE, SETTINGS_CLASS_DATA_USAGE_SETTINGS);
                intent.setComponent(cn);
                context.startActivity(intent);
            } catch (ActivityNotFoundException e) {
                Log.v(TAG, "Data settings usage Activity is not present");
            }