-1

UPD: I'm sorry, apparently i added 2 id's to my checkbox cause i forgot to delete the default one

I am using a check box and I get this error when running apk:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.CheckBox.setOnClickListener(android.view.View$OnClickListener)' on a null object reference

on the line I want to use checkbox.setOnClickListener

this is the code

final CheckBox settings_1 = (CheckBox) findViewById(R.id.settings_1);
settings_1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if( go_to_menu == 1)
            {
                settings_1.setChecked(false);
                go_to_menu = 0;
                SharedPreferences sharedpref = getPreferences(Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedpref.edit();
                String key = getString(R.string.check1);
                editor.putInt(key, go_to_menu);
                editor.commit();
            }
            else
            {
                settings_1.setChecked(true);
                go_to_menu = 1;
                SharedPreferences sharedpref = getPreferences(Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = sharedpref.edit();
                String key = getString(R.string.check1);
                editor.putInt(key, go_to_menu);
                editor.commit();
            }
        }
    });

1 Answers1

1

Probably this code is invoked before setContentView() or the layout of the activity not contain a view with id settings_1

Oleg Khalidov
  • 5,108
  • 1
  • 28
  • 29