Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6734613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:54:34+00:00 2026-05-26T10:54:34+00:00

I am trying to save the values from different checkboxes located in a layout.xml

  • 0

I am trying to save the values from different checkboxes located in a layout.xml file, using sharedpreferences.

But when i compile the program i get a NullPointerException, according Logcat the cause is in line 463, marked in my code.

I tried sevaral things, but i can`t get rid of the exception.

Could someone please help me?

The Exception from Logcat:

10-29 23:35:31.556: E/AndroidRuntime(3292): FATAL EXCEPTION: main 
10-29 23:35:31.556: E/AndroidRuntime(3292): java.lang.RuntimeException: Unable to resume activity {com.sencide/com.sencide.AndroidLogin}: java.lang.NullPointerException 
10-29 23:35:31.556: E/AndroidRuntime(3292):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2241) 
10-29 23:35:31.556: E/AndroidRuntime(3292):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2256) 
10-29 23:35:31.556: E/AndroidRuntime(3292):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1789) 
10-29 23:35:31.556: E/AndroidRuntime(3292):     at android.app.ActivityThread.access$1500(ActivityThread.java:123) 
10-29 23:35:31.556: E/AndroidRuntime(3292):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939) 
10-29 23:35:31.556: E/AndroidRuntime(3292):     at android.os.Handler.dispatchMessage(Handler.java:99) 
10-29 23:35:31.556: E/AndroidRuntime(3292):     at android.os.Looper.loop(Looper.java:130) 
10-29 23:35:31.556: E/AndroidRuntime(3292):     at android.app.ActivityThread.main(ActivityThread.java:3835) 
10-29 23:35:31.556: E/AndroidRuntime(3292):     at java.lang.reflect.Method.invokeNative(Native Method) 
10-29 23:35:31.556: E/AndroidRuntime(3292):     at java.lang.reflect.Method.invoke(Method.java:507) 
10-29 23:35:31.556: E/AndroidRuntime(3292):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847) 
10-29 23:35:31.556: E/AndroidRuntime(3292):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605) 
10-29 23:35:31.556: E/AndroidRuntime(3292):     at dalvik.system.NativeStart.main(Native Method) 
10-29 23:35:31.556: E/AndroidRuntime(3292): Caused by: java.lang.NullPointerException 
10-29 23:35:31.556: E/AndroidRuntime(3292):     at com.sencide.AndroidLogin.onResume(AndroidLogin.java:463) 
10-29 23:35:31.556: E/AndroidRuntime(3292):     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1150) 
10-29 23:35:31.556: E/AndroidRuntime(3292):     at android.app.Activity.performResume(Activity.java:3832) 
10-29 23:35:31.556: E/AndroidRuntime(3292):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2231) 
10-29 23:35:31.556: E/AndroidRuntime(3292):     ... 12 more 

Part of the code in my Main program xml:

On Create and set values:

public static final String PREFS_NAME = "SharedPrefsDemoPreferences"; 
public static final String PREF_BOOL = "PrefBool";
private SharedPreferences mPrefs;
private CheckBox mCheckBox;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mPrefs = getSharedPreferences(PREFS_NAME, 0);
    mCheckBox = (CheckBox)findViewById(R.id.nieuwbel);// in layout.xml 

The OnPause and OnResume:

@Override
protected void onResume() {      
    mCheckBox.setChecked(mPrefs.getBoolean(PREF_BOOL, false)); <-- Line 463   
    super.onResume();}

@Override    
protected void onPause() { 
    Editor e = mPrefs.edit();
    e.putBoolean(PREF_BOOL, mCheckBox.isChecked());              
    e.commit();         
    Toast.makeText(this, "Settings Saved.", Toast.LENGTH_SHORT).show();        
    super.onPause();    }

The layout file with the checkbox:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >


<CheckBox
    android:id="@+id/nieuwbel"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:checked="true"
    android:text="Nieuw beltegoed" />


<CheckBox
    android:id="@+id/vorige"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:checked="true"
    android:text="Tegoed vorige periode" />

and so on…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T10:54:35+00:00Added an answer on May 26, 2026 at 10:54 am

    You need to get SharedPreferences again. At that point mPrefs can be also null object. Also try reinstatiating the mCheckBox. This happens if the activity went from onPause() to onResume() not calling onCreate() again where you have instatiation of both object properly.

    Edit:

        @Override
        protected void onResume() {      
            mPrefs = getSharedPreferences(PREFS_NAME, 0);
             mCheckBox = (CheckBox)findViewById(R.id.nieuwbel);
            if(mPrefs!=null)  //check in case there are no prefs saved, due some reason
               mCheckBox.setChecked(mPrefs.getBoolean(PREF_BOOL, false));
            else
                mCheckBox.setChecked(true); //here put default value if there are no preferences saved
            super.onResume();
    }
    

    Edit 2: Inflating the other xml.

    You put this inside onResume()

    LayoutInflater inflater = (LayoutInflater)getSystemService
          (Context.LAYOUT_INFLATER_SERVICE);
       View v = inflater.inflate(R.layout.layout, null); // you have the xml as a view from which you can extract the checkbox
       mCheckBox = (CheckBox)v.findViewById(R.id.nieuwbel);
       .... do the rest of the coding in the first edit :)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to save some values from my app using NSCoding. I'm able
I am using asp.net and I am trying to save checkbox values into a
I m new to Struts2 and Hibernate. I am trying to save values from
I'm trying to develop an app that gets four coord. values from an XML
I am trying to save an NSMutableArray containing text to a plist file using
I´m trying to upload a file which is fetched from a server. But when
Hi I am trying to use NSUserDefaults to save some default values in database.
I am trying to save data to a database on a button push, but
I'm trying to save some XML-Data in my UserSettings (Properties.Settings.Default.UserSettings) in a .NET Winforms
I'm trying to save a PDF file to SQL Server and I already have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.