我有一个带有简单 textview 的 appwidget,它可以作为配置活动中的 edittextfield 进行编辑,使用微调器,用户可以更改文本大小,我需要保存微调器选择,并使用 sharedpreferences 再次恢复,我需要 appwidgetid,所以用户可以添加多个appwidget。
我已经尝试了很多东西并且已经完成了 2 天的研究,但是我无法让它工作,一旦添加了小部件,我就会强制关闭。
这是我认为看起来最好的,但它不起作用。微调器选择 - 保存到 SharedPreferences,然后检索
这真让我抓狂!任何解决此问题的建议将不胜感激
问候雅各布·哈特格
保存和加载首选项
// 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);
}
}
几乎完整的代码:
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) {
}
...
}
更新:
我添加了这样的日志标签:
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");
但它们甚至没有出现在日志中:
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
如果我删除用于保存和加载位置的行,则小部件可以完美运行
更新:
我也尝试过:
多变的:
private final static String PREF_PREFIX_KEY_FONT_SIZE = "prefix_fontsize_";
保存:
editor.putInt(PREF_PREFIX_KEY_FONT_SIZE + appWidgetId, spinner.getSelectedItemPosition());
装载:
prefs.getInt(PREF_PREFIX_KEY_FONT_SIZE + appWidgetId, spinner.getSelectedItemPosition());
给我同样的错误。请帮忙