在我的设备上经过很长一段时间后,我似乎遇到了崩溃:
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);
}
}
答案 0 :(得分:3)
我之前遇到过类似的问题。并不是因为Android会杀死一些变量,而是因为Android杀了一段时间后,当你再次启动程序时,程序不是在第一个活动上启动,而是在你上次活动的最后一个活动上启动。因此,您可能会获得一些通常在未初始化的第一个活动上初始化它们的变量。
答案 1 :(得分:0)
错误本身就说明了:NullPointerException
。试着看看为什么你在那里得到一个空值,并尝试抓住它......
catch (JSONException e) {
System.out.println(e);
}
catch (NullPointerException n) {
// do something useful here
}