我似乎時間我的設備上長時間後得到一個崩潰:Android會在一段時間後終止本地變量嗎?
E/AndroidRuntime(1115): FATAL EXCEPTION: main
E/AndroidRuntime(1115): java.lang.RuntimeException: Unable to resume activity {org.stocktwits.activity/org.stocktwits.activity.Main}: java.lang.NullPointerException
E/AndroidRuntime(1115): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3128)
E/AndroidRuntime(1115): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3143)
E/AndroidRuntime(1115): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2059)
E/AndroidRuntime(1115): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(1115): at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(1115): at android.app.ActivityThread.main(ActivityThread.java:4627)
E/AndroidRuntime(1115): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(1115): at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(1115): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime(1115): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E/AndroidRuntime(1115): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(1115): Caused by: java.lang.NullPointerException
E/AndroidRuntime(1115): at org.stocktwits.activity.Main.getQuotesFromYQL(Main.java:457)
E/AndroidRuntime(1115): at org.stocktwits.activity.Main.onStart(Main.java:339)
E/AndroidRuntime(1115): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
E/AndroidRuntime(1115): at android.app.Activity.performStart(Activity.java:3781)
E/AndroidRuntime(1115): at android.app.Activity.performRestart(Activity.java:3811)
E/AndroidRuntime(1115): at android.app.Activity.performResume(Activity.java:3816)
E/AndroidRuntime(1115): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3118)
E/AndroidRuntime(1115): ... 10 more
在onStart()似乎正常工作,當我退出和恢復我的應用程序,但一段時間後失敗...
線457在我的應用程序是JSON '查詢' 對象:
/**
* Performs YQL, parses JSON, and adds Quotes to adapter
*/
private void getQuotesFromYQL() {
quotesAdapter.clear();
System.out.println("YQL QUERY: " + buildQuery());
JSONObject json = RestClient.connect(buildQuery());
try {
JSONObject query = json.getJSONObject("query");
JSONObject results = query.getJSONObject("results");
if (query.getString("count").equals("1")) { // YQL JSON doesn't
// return an array for
// single quotes
JSONObject quote = results.getJSONObject("quote");
Quote myQuote = new Quote();
myQuote.setName(quote.getString("Name"));
myQuote.setSymbol(quote.getString("Symbol"));
myQuote.setLastTradePriceOnly(quote
.getString("LastTradePriceOnly"));
myQuote.setChange(quote.getString("Change"));
myQuote.setOpen(quote.getString("Open"));
myQuote.setMarketCapitalization(quote
.getString("MarketCapitalization"));
myQuote.setDaysHigh(quote.getString("DaysHigh"));
myQuote.setYearHigh(quote.getString("YearHigh"));
myQuote.setDaysLow(quote.getString("DaysLow"));
myQuote.setYearLow(quote.getString("YearLow"));
myQuote.setVolume(quote.getString("Volume"));
myQuote.setAverageDailyVolume(quote
.getString("AverageDailyVolume"));
myQuote.setPeRatio(quote.getString("PERatio"));
myQuote.setDividendYield(quote.getString("DividendYield"));
quotesAdapter.add(myQuote);
} else {
JSONArray quotes = results.getJSONArray("quote");
for (int i = 0; i < quotes.length(); i++) {
JSONObject quote = quotes.getJSONObject(i);
// .getJSONObject("quote");
// Do something with the user
Quote myQuote = new Quote();
myQuote.setName(quote.getString("Name"));
myQuote.setSymbol(quote.getString("Symbol"));
myQuote.setLastTradePriceOnly(quote
.getString("LastTradePriceOnly"));
myQuote.setChange(quote.getString("Change"));
myQuote.setOpen(quote.getString("Open"));
myQuote.setMarketCapitalization(quote
.getString("MarketCapitalization"));
myQuote.setDaysHigh(quote.getString("DaysHigh"));
myQuote.setYearHigh(quote.getString("YearHigh"));
myQuote.setDaysLow(quote.getString("DaysLow"));
myQuote.setYearLow(quote.getString("YearLow"));
myQuote.setVolume(quote.getString("Volume"));
myQuote.setAverageDailyVolume(quote
.getString("AverageDailyVolume"));
myQuote.setPeRatio(quote.getString("PERatio"));
myQuote.setDividendYield(quote.getString("DividendYield"));
quotesAdapter.add(myQuote);
}
}
serializeQuotes();
} catch (JSONException e) {
System.out.println(e);
}
}
我可以通過捕獲nullpointerexception來防止崩潰,但從上面的答案看來,操作系統正在殺死我的進程並跳過onCreate()。我如何正確處理這個問題? – 2010-09-08 06:31:24
如果殺死進程,它不會跳過onCreate()。它*有*做onCreate()以顯示當前的活動,因爲它需要創建活動。它可能會創建與最初不同的順序的活動,因爲它們僅在顯示時創建。你只需要處理這一點。 (和原始評論 - 只是捕捉NullPointerException最好是一個創可貼,如果不是完全錯誤的話;代碼只需要正確處理事情,所以這不會發生。) – hackbod 2010-09-08 06:37:53
是否hacky檢查null onStart ()或onResume()並在那裏重新初始化? – 2010-09-08 15:33:32