0

I'm trying to call this class below from an AlertDialog.. I tried Intent but I always get exception. code that calls Switchoff class:

 Intent intent = new Intent(Premium.this, Switchoff.class );
                        startActivity( intent );

The class I'm trying to call:

public  class Switchoff extends AppCompatPreferenceActivity{


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    final SwitchPreference vibrateSwitch = (SwitchPreference) findPreference( getString( R.string.pref_vibrate_switch_enabled ) );
    vibrateSwitch.setChecked(false);
}

}

the error:

   java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
        at android.content.ContextWrapper.getPackageName(ContextWrapper.java:142)

PS: - I need to keep both findPreference(), and onCreate() method - Both class (Premium.class, Switchoff.class) are registed in the Manifest file

THANK YOU FOR EVERY HELP!

Nizar
  • 1,112
  • 4
  • 17
  • 29
  • 1
    Possible duplicate of [android.content.Context.getPackageName()' on a null object reference](https://stackoverflow.com/questions/28515049/android-content-context-getpackagename-on-a-null-object-reference) – Martin Zeitler Apr 09 '19 at 21:00
  • I tryied that solution and the error still persist – Nizar Apr 10 '19 at 07:58
  • this is also a [NullPointerException](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) duplicate and it's off-topic, because it does not provide a [MCVE](https://stackoverflow.com/help/mcve)... besides it clearly reads `getPackageName()` and not `findPreference()`. maybe this [answer](https://stackoverflow.com/questions/6589797/how-to-get-package-name-from-anywhere) helps? – Martin Zeitler Apr 10 '19 at 08:30

0 Answers0