gpt4 book ai didi

java - Android 会在一段时间后杀死局部变量吗?

转载 作者:搜寻专家 更新时间:2023-11-01 09:17:27 28 4
gpt4 key购买 nike

在我的设备上运行了很长时间后,我似乎遇到了崩溃:

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);
}
}

最佳答案

我之前遇到过类似的问题。并不是 Android 杀死了一些变量,而是因为过了一段时间 Android 杀死了进程,当你再次启动程序时,程序不是在第一个 Activity 上启动,而是在你进行的最后一个 Activity 上启动。因此,您可能会得到一些通常在未初始化的第一个 Activity 中初始化的变量。

关于java - Android 会在一段时间后杀死局部变量吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3664734/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com