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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:19:14+00:00 2026-05-23T18:19:14+00:00

I use SharedPreferences for storing data. When I try to call the data-storing function

  • 0

I use SharedPreferences for storing data. When I try to call the data-storing function from the constructor of my class, it shows a Runtime error. However, when I call the function from onCreate() it works fine.
Is there any way I can call my data-storing function from the constructor?

package com.examples.storedata;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;

import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Activity1 extends Activity 
{
    EditText editText1, editText2;
    TextView textSavedMem1, textSavedMem2;
    Button buttonSaveMem1, buttonSaveMem2;

    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main); 

        textSavedMem1 = (TextView)findViewById(R.id.savedmem1);
        textSavedMem2 = (TextView)findViewById(R.id.savedmem2);
        editText1 = (EditText)findViewById(R.id.edittext1);
        editText2 = (EditText)findViewById(R.id.edittext2);
        buttonSaveMem1 = (Button)findViewById(R.id.save_mem1);
        buttonSaveMem2 = (Button)findViewById(R.id.save_mem2); 
        buttonSaveMem1.setOnClickListener(buttonSaveMem1OnClickListener);
        buttonSaveMem2.setOnClickListener(buttonSaveMem2OnClickListener);
    }

    public Activity1()
    {
        LoadPreferences(); //show error while calling from here
    }

    Button.OnClickListener buttonSaveMem1OnClickListener
        = new Button.OnClickListener(){
            public void onClick(View arg0) {
                SavePreferences("MEM1", editText1.getText().toString());
                LoadPreferences();
            }
        };

    Button.OnClickListener buttonSaveMem2OnClickListener
        = new Button.OnClickListener(){
            public void onClick(View arg0) {
                SavePreferences("MEM2", editText2.getText().toString());
                LoadPreferences();
            }
        };


   private void SavePreferences(String key, String value)
   {
       SharedPreferences sharedPreferences = getPreferences(MODE_APPEND);
       SharedPreferences.Editor editor = sharedPreferences.edit();
       editor.putString(key, value);
       editor.putInt("1",5);
       editor.commit();
   }

   private void LoadPreferences()
   {
       SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
       String strSavedMem1 = sharedPreferences.getString("MEM1", "");
       String strSavedMem2 = sharedPreferences.getString("MEM2", "");
       textSavedMem1.setText(strSavedMem1);
       textSavedMem2.setText(strSavedMem2);
    }
}
  • 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-23T18:19:15+00:00Added an answer on May 23, 2026 at 6:19 pm

    You are attempting to access your text views before you have initialised the view or the variables:

    textSavedMem1.setText(strSavedMem1);
    textSavedMem2.setText(strSavedMem2);
    

    When you call LoadPreferences from the Activity constructor, onCreate hasn’t yet been called, which means textSavedMem1 and textSavedMem2 have not been initialised and are null.

    When you call it from onCreate, so long as it is after the following, then these variable are initialised and everything works as expected.

    setContentView(R.layout.main); 
    
    textSavedMem1 = (TextView)findViewById(R.id.savedmem1);
    textSavedMem2 = (TextView)findViewById(R.id.savedmem2);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We use a data acquisition card to take readings from a device that increases
I have some information stored as SharedPreferences. I need to access that information from
Hi i have a class ( MyCustomForm.xml ) which i use as a LoginForm
I am writing a game and storing almost all active game data (stats, location
I wrote some test code to learn how to use SharedPreferences. The app saves
I am trying to get data from a webserver and display a message saying
I'm still learning Java. I'm trying to get use SharedPreferences to set the url
'''use Jython''' import shutil print dir(shutil) There is no, shutil.move, how does one move
Use case: A does something on his box and gots stuck. He asks B
Use case: 3rd party application wants to programatically monitor a text file being generated

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.