I seem to be getting a crash after a long period of time on my device:
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() seems to work fine when I exit and resume my app, but fails after some time...
Line 457 in my app is the JSON 'query' object:
/**
* 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"));
开发者_JS百科 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);
}
}
I experienced similar problems earlier. It is not that Android kills some variable, but because after some time Android kills the process, when you start the program again, the program is started not on the first activity, but the last activity you were on. Therefore you may get some variables that you usually have them initialized on the first activity not initialized.
The error speaks by itself: NullPointerException
. Just try to see why are you getting a null value there, and try to catch it also...
catch (JSONException e) {
System.out.println(e);
}
catch (NullPointerException n) {
// do something useful here
}
精彩评论