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

  • Home
  • SEARCH
  • 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 8900123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:01:47+00:00 2026-06-15T01:01:47+00:00

I have an appwidget with a simple textview, which is editable as an edittextfield

  • 0

I have an appwidget with a simple textview, which is editable as an edittextfield in a config activity, with a spinner the user can change the textsize, I need to save a spinners selection, and restore again with sharedpreferences, and i needs the appwidgetid, so the user can add multiple appwidget.

I’ve tried a lot of things and done researching for 2 days now, but I can not get it to work, I get force close as soon as the widget is being added.

This is what i think looks the best, but it does not work. Spinner Selection – Save to SharedPreferences, then Retrieve

This is driving me crazy! any sugestions to fix this will be VERY much appreciated

Regards Jakob Harteg

Save and load prefs

    // Write the prefix to the SharedPreferences object for this widget
    static void saveTitlePref(Context context, int appWidgetId, String text) {

            SharedPreferences.Editor editor = context.getSharedPreferences(PREFS_NAME, 0).edit();
        editor.putString(PREF_PREFIX_KEY + appWidgetId, text);
        spinner.getSelectedItemPosition();
        editor.putInt(spinnerSelection + appWidgetId, 0);
        editor.commit();
    }

    // Read the prefix from the SharedPreferences object for this widget.
    static String loadTitlePref(Context context, int appWidgetId) {
        SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);

            String prefix = prefs.getString(PREF_PREFIX_KEY + appWidgetId, null);

        spinner.setSelection(prefs.getInt(spinnerSelection + appWidgetId, 0));

        // If there is no preference saved, get the default from a resource
        if (prefix != null) {
            return prefix;
        } else {
            return context.getString(R.string.appwidget_prefix_default);
        }
    }

Almost full code:

public class WidgetConfig extends Activity implements OnItemSelectedListener{

    Dialog myDialog;
    Context context;
    static EditText info;
    static Spinner spinner;
    private static final String[] paths = { "10", "12", "14", "16", "18", "20",
        "22", "24", "26", "28", "30", "32", "34", "36", "38", "40", "50", "60"};

    File path = null;
    private static final String PREFS_NAME = "com.harteg.NotesWidgetPro.Widget";
    private static final String PREF_PREFIX_KEY = "prefix_";
    private final static String FONT_SIZE_KEY="fontsize";
    static String spinnerSelection;

    int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;   

    public WidgetConfig() {
        super();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setContentView(R.layout.widgetconfig);
        context = WidgetConfig.this;

        // back button = cancel
        setResult(RESULT_CANCELED);

        info = (EditText) findViewById(R.id.etwidgetconfig);

        findViewById(R.id.bwidgetconfig).setOnClickListener(mOnClickListener);
        findViewById(R.id.bwidgetconfig1).setOnClickListener(mOnClickListener);

        // Find the widget id from the intent.
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        if (extras != null) {
            mAppWidgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
                    AppWidgetManager.INVALID_APPWIDGET_ID);
        }

        // If they gave us an intent without the widget id, just bail.
        if (mAppWidgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
            finish();
        }

        info.setText(loadTitlePref(WidgetConfig.this, mAppWidgetId));

        //------------ Text Size spinner ---------------
        spinner = (Spinner) findViewById(R.id.TxtSizeSP);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(WidgetConfig.this,
                android.R.layout.simple_spinner_item, paths);

        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(adapter);
        spinner.setSelection(7);
        spinner.setOnItemSelectedListener(this);

        //--------------------------------------------------

    } // onCreate finished

    public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {

        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); 

        switch (position) {
        case 0:
            info.setTextSize(10.0f);
            views.setFloat(R.id.tvConfigInput, "setTextSize", 10);
            break;
        case 1:
            info.setTextSize(12.0f);
            views.setFloat(R.id.tvConfigInput, "setTextSize", 12);
            break;

        ....

        case 17:
            info.setTextSize(28.0f);
            views.setFloat(R.id.tvConfigInput, "setTextSize", 60);
           break;  
        }
        appWidgetManager.updateAppWidget(mAppWidgetId, views);
    }

    public void onNothingSelected(AdapterView<?> arg0) {

    }

    View.OnClickListener mOnClickListener = new View.OnClickListener() {
        public void onClick(View v) {

            // When the button is clicked, save the string in our prefs and
            // return that they clicked OK.
            String titlePrefix = info.getText().toString();
            saveTitlePref(context, mAppWidgetId, titlePrefix);


            // Push widget update to surface with newly set prefix
            AppWidgetManager appWidgetManager = AppWidgetManager
                    .getInstance(context);
            Widget.updateAppWidget(context, appWidgetManager, mAppWidgetId,
                    titlePrefix);

            // Make sure we pass back the original appWidgetId
            Intent resultValue = new Intent();
            resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
                    mAppWidgetId);
            setResult(RESULT_OK, resultValue);
            finish();

        }
    };

    // Write the prefix to the SharedPreferences object for this widget
    static void saveTitlePref(Context context, int appWidgetId, String text) {

        //Getting the SharedPreference object
        SharedPreferences.Editor editor = context.getSharedPreferences(PREFS_NAME, 0).edit();

        // save the values to preferences
        editor.putString(PREF_PREFIX_KEY + appWidgetId, text);

        spinner.getSelectedItemPosition();
        editor.putInt(spinnerSelection + appWidgetId, 0);


        // Saves the values
        editor.commit();
    }

    // Read the prefix from the SharedPreferences object for this widget.
    static String loadTitlePref(Context context, int appWidgetId) {
        SharedPreferences prefs = context.getSharedPreferences(PREFS_NAME, 0);
        String prefix = prefs.getString(PREF_PREFIX_KEY + appWidgetId, null);

        spinner.setSelection(prefs.getInt(spinnerSelection + appWidgetId, 0));

        // If there is no preference saved, get the default from a resource
        if (prefix != null) {
            return prefix;
        } else {
            return context.getString(R.string.appwidget_prefix_default);
        }
    }

    static void deleteTitlePref(Context context, int appWidgetId) {
    }

    static void loadAllTitlePrefs(Context context,
            ArrayList<Integer> appWidgetIds, ArrayList<String> texts) {
    }

...

}

UPDATE:

I added log tags like this:

Log.v(TAG, "before save");
        spinner.getSelectedItemPosition();
        editor.putInt(spinnerSelection + appWidgetId, 0);
        Log.v(TAG, "after save");

Log.v(TAG, "before load");
        spinner.setSelection(prefs.getInt(spinnerSelection + appWidgetId, 0));
        Log.v(TAG, "after load");

but they don’t even appear in the log:

11-16 21:25:08.470: E/AndroidRuntime(1337): FATAL EXCEPTION: main
11-16 21:25:08.470: E/AndroidRuntime(1337): java.lang.RuntimeException: Unable to start receiver com.harteg.NotesWidgetPro.Widget: java.lang.NullPointerException
11-16 21:25:08.470: E/AndroidRuntime(1337):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2236)
11-16 21:25:08.470: E/AndroidRuntime(1337):     at android.app.ActivityThread.access$1500(ActivityThread.java:130)
11-16 21:25:08.470: E/AndroidRuntime(1337):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1271)
11-16 21:25:08.470: E/AndroidRuntime(1337):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-16 21:25:08.470: E/AndroidRuntime(1337):     at android.os.Looper.loop(Looper.java:137)
11-16 21:25:08.470: E/AndroidRuntime(1337):     at android.app.ActivityThread.main(ActivityThread.java:4745)
11-16 21:25:08.470: E/AndroidRuntime(1337):     at java.lang.reflect.Method.invokeNative(Native Method)
11-16 21:25:08.470: E/AndroidRuntime(1337):     at java.lang.reflect.Method.invoke(Method.java:511)
11-16 21:25:08.470: E/AndroidRuntime(1337):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
11-16 21:25:08.470: E/AndroidRuntime(1337):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-16 21:25:08.470: E/AndroidRuntime(1337):     at dalvik.system.NativeStart.main(Native Method)
11-16 21:25:08.470: E/AndroidRuntime(1337): Caused by: java.lang.NullPointerException
11-16 21:25:08.470: E/AndroidRuntime(1337):     at com.harteg.NotesWidgetPro.WidgetConfig.loadTitlePref(WidgetConfig.java:267)
11-16 21:25:08.470: E/AndroidRuntime(1337):     at com.harteg.NotesWidgetPro.Widget.onUpdate(Widget.java:32)
11-16 21:25:08.470: E/AndroidRuntime(1337):     at android.appwidget.AppWidgetProvider.onReceive(AppWidgetProvider.java:66)
11-16 21:25:08.470: E/AndroidRuntime(1337):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2229)
11-16 21:25:08.470: E/AndroidRuntime(1337):     ... 10 more

if I delete the lines for saving and loading the position the widget works perfect

UPDATE:

I’ve also tried with:

variable:

private final static String PREF_PREFIX_KEY_FONT_SIZE = "prefix_fontsize_";

To save:

editor.putInt(PREF_PREFIX_KEY_FONT_SIZE + appWidgetId, spinner.getSelectedItemPosition());

to load:

prefs.getInt(PREF_PREFIX_KEY_FONT_SIZE + appWidgetId, spinner.getSelectedItemPosition());

gives me the same error.
please help

  • 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-15T01:01:48+00:00Added an answer on June 15, 2026 at 1:01 am

    I got it working, this is what i did

    I edited my spinner to this:

    //------------ Text Size spinner ---------------
            spinner = (Spinner) findViewById(R.id.TxtSizeSP);
    
    
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(WidgetConfig.this,
                    android.R.layout.simple_spinner_item, paths);
    
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinner.setAdapter(adapter);
    
            // Retrieve spinner position from sharedpreferences
            SharedPreferences sharedPref = getSharedPreferences(PREFS_NAME + mAppWidgetId, MODE_PRIVATE);
            int spinnerValue = sharedPref.getInt("userChoiceSpinner",-1);
            if(spinnerValue != -1) 
              // set the value of the spinner 
              spinner.setSelection(spinnerValue);
    
            spinner.setOnItemSelectedListener(this);
    

    And after all my cases in the onItemSelected() method I added:

    // save inputed spinner position to sharedpreferences
            int userChoice = spinner.getSelectedItemPosition();
            SharedPreferences sharedPref = getSharedPreferences(PREFS_NAME + mAppWidgetId, 0);
            SharedPreferences.Editor prefEditor = sharedPref.edit();
            prefEditor.putInt("userChoiceSpinner", userChoice);
            prefEditor.commit();
    

    This might also work:
    http://a2zandroidtutorials.blogspot.dk/2012/07/spinner-with-previously-selected-value.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've created a widget which displays a simple textview, which is editable as an
I have an appwidget layout with a textview and an imageview. Lint always tells
I have a Button on appwidget, that I need to 'enable'/'disable' programmatically from a
I have a (hopefully) a relatively simple question. How do I tell Android which
I'm writing an AppWidget where I first have a configure screen where the user
Good day, I have an activity which i navigate to from an icon on
i have three classess.activity,service and appwidget. i have two buttons in appwidget. and onclicking
I have an application which has an Appwidget. All works well. Now I have
I have a simple android application which does search. I have a custom popup
I have an appwidget which uses ListView . I have created a class that

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.