我正在努力将首选项添加到我的应用程序中,一切都很顺利,直到我尝试从数据库中获取一个数组以加载到 ListPreference 中。我正在使用两个单独的数组,on 用于条目,on 用于 entryValues。一切都很好,直到我尝试将 CharSequence[] 传递给 setEntries 并设置 entryValues 函数。
这是代码:
public class EditPreferences extends PreferenceActivity {
private SQLiteDatabase db;
private TimesheetDatabaseHelper dbHelp = new TimesheetDatabaseHelper(this);
@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preference_activity);
ListPreference projectsList = (ListPreference)findPreference("@string/perfProjectKey");
/** Open the database table for reading and writing */
db = dbHelp.getReadableDatabase();
String getProjects = "select _id, name from projects;";
Cursor cu = db.rawQuery(getProjects, null);
Log.d("Database call:", "Success");
Log.d("Database Rows Returned:", String.valueOf(cu.getCount()));
List<String> entries = new ArrayList<String>();
List<String> entryValues = new ArrayList<String>();
if(cu != null && cu.getCount() > 0){
cu.moveToFirst();
do {
entries.add(cu.getString(1));
entryValues.add(Integer.toString(cu.getInt(0)));
} while (cu.moveToNext());
}
cu.close();
db.close();
Log.d("Out of the loop", "and probably closed");
final CharSequence[] entryCharSeq = entries.toArray(new CharSequence[entries.size()]);
final CharSequence[] entryValsChar = entryValues.toArray(new CharSequence[entryValues.size()]);
Log.d("Past the cast", "maybe we work?");
Log.d("Entries size:", Integer.toString(entries.size()));
Log.d("Entry Values size:", Integer.toString(entryValues.size()));
projectsList.setEntries(entryCharSeq);
Log.d("We got here:", "TRUE!");
projectsList.setEntryValues(entryValsChar);
}
}
根据堆栈跟踪,我可以在必要时发布,它是第 55 行的 nullPointerException,第 55 行是
projectsList.setEntries(entryCharSeq);
我不知道怎么了。希望有人可以提供帮助。