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 8318185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:52:30+00:00 2026-06-08T21:52:30+00:00

I try to save the toggle state in my application.Here is my code boolean

  • 0

I try to save the toggle state in my application.Here is my code

    boolean on;
public SharedPreferences spref;
ToggleButton tb;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.fb_intermidiate);
    spref = getPreferences(MODE_PRIVATE);
    tb = (ToggleButton) findViewById(R.id.toggleButton1);
    on = spref.getBoolean("On", true);  //default is true
    if (on = true) 
    {
      tb.setChecked(true);
    }       else
    {
      tb.setChecked(false);
    }
    back = (Button)findViewById(R.id.button_back); 
    //back.setText(R.string.back_button_in_settings);
    back.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            finish();
        }
    }); 
} 

public void onToggleClicked(View view) {

    on = ((ToggleButton) view).isChecked();
    if (on) {
        Toast.makeText(this, "On : Notification will be Enabled", Toast.LENGTH_SHORT).show();
        SharedPreferences.Editor editor = spref.edit();
        editor.putBoolean("On", true); // value to store
        editor.commit();

    } else {
    Toast.makeText(this, "Off : Notification will be Disabled", Toast.LENGTH_SHORT).show();
        SharedPreferences.Editor editor =spref.edit();
        editor.putBoolean("Off", false); // value to store
        editor.commit();
    }  
}

But Its getting force close.:(

Here is my logcat

07-25 09:02:23.317: E/AndroidRuntime(640): java.lang.RuntimeException: Unable to start       activity ComponentInfo{com.ace.gugulog/com.ace.gugulog.activities.FacebookIntermidiate}: java.lang.NullPointerException

07-25 09:02:23.317: E/AndroidRuntime(640):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1815)

07-25 09:02:23.317: E/AndroidRuntime(640):  at java.lang.reflect.Method.invokeNative(Native Method)

07-25 09:02:23.317: E/AndroidRuntime(640):  at java.lang.reflect.Method.invoke(Method.java:491)

07-25 09:02:23.317: E/AndroidRuntime(640):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)

07-25 09:02:23.317: E/AndroidRuntime(640):  at dalvik.system.NativeStart.main(Native Method)
07-25 09:02:23.317: E/AndroidRuntime(640): Caused by: java.lang.NullPointerException

07-25 09:02:23.317: E/AndroidRuntime(640):  at  com.ace.gugulog.activities.FacebookIntermidiate.onCreate(FacebookIntermidiate.java:32)

07-25 09:02:23.317: E/AndroidRuntime(640):  at android.app.Activity.performCreate(Activity.java:4397)

07-25 09:02:23.317: E/AndroidRuntime(640):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
  • 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-06-08T21:52:33+00:00Added an answer on June 8, 2026 at 9:52 pm

    I think most likely source of error is, you are not using shared preferencces properly, change your code to: you should use getSharedPreferences instead of getPreferences method.

      boolean on;
    public SharedPreferences spref;
    final String PREF_NAME="preferences";
    ToggleButton tb;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.fb_intermidiate);
        spref = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
        tb = (ToggleButton) findViewById(R.id.toggleButton1);
        on = spref.getBoolean("On", true);  //default is true
        if (on = true) 
        {
          tb.setChecked(true);
        }       else
        {
          tb.setChecked(false);
        }
        back = (Button)findViewById(R.id.button_back); 
        //back.setText(R.string.back_button_in_settings);
        back.setOnClickListener(new View.OnClickListener() {
    
            public void onClick(View v) {
                // TODO Auto-generated method stub
                finish();
            }
        }); 
    } 
    
    public void onToggleClicked(View view) {
    
        on = ((ToggleButton) view).isChecked();
        if (on) {
            Toast.makeText(this, "On : Notification will be Enabled", Toast.LENGTH_SHORT).show();
            SharedPreferences.Editor editor = spref.edit();
            editor.putBoolean("On", true); // value to store
            editor.commit();
    
        } else {
        Toast.makeText(this, "Off : Notification will be Disabled", Toast.LENGTH_SHORT).show();
            SharedPreferences.Editor editor =spref.edit();
            editor.putBoolean("Off", false); // value to store
            editor.commit();
        }  
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is what I do when I save my score to the highscore. public
I try to save and restore the state of 3 WebViews in my activity
I got a problem in my Asp.net application. When I try to save some
I try to save html content to the database, with ' or it auto
When I try to save images to a directory, it's not saving to my
When I try and save something to my list in SharePoint I get the
When I try to save a certain of my ActiveRecord instances, I get this
when I try to Save an updated managedobject the changes don't get persisted to
this is the problem once i try to save data into db with sql
I try to create a new file inside a JSP and try to save

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.