我在Google Play控制台收到了这个崩溃报告。然而,在我的设备和模拟器上从未遇到过这样的崩溃。
Caused by: java.lang.NullPointerException:
at android.preference.PreferenceManager.getDefaultSharedPreferencesName (PreferenceManager.java:498)
at android.preference.PreferenceManager.getDefaultSharedPreferences (PreferenceManager.java:487)
at DictionaryDB2.getHistoryWords (DictionaryDB2.java)
or .hasObject (DictionaryDB2.java)
这是
getHistoryWords
。public List<Bean> getHistoryWords() {
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(MyApp.getAppContext());
String historyNum = SP.getString("history","50");
SQLiteDatabase db = initializer.getReadableDatabase();
String sql = "SELECT * FROM " + HISTORY_NAME +
" WHERE " + STATUS + " ORDER BY " + STATUS + " DESC LIMIT " + historyNum ;
Cursor cursor = db.rawQuery(sql, null);
List<Bean> wordList = new ArrayList<Bean>();
while(cursor.moveToNext()) {
int id = cursor.getInt(0);
String english = cursor.getString(1);
String malay = cursor.getString(2);
wordList.add(new Bean(id, english, malay));
}
cursor.close();
db.close();
return wordList;
}
这是
MyApp
:public class MyApp extends Application {
private static Context context;
public void onCreate() {
super.onCreate();
MobileAds.initialize(this, "my admob ");
MyApp.context = getApplicationContext();
}
public static Context getAppContext() {
return MyApp.context;
}
}
如何解决这个问题?
PreferenceManager.java
文件。 - Santanu SurSP.getString("history","50");
引起的。 - Michael Dodd