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
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