1

在我的应用程序中,我有一个 listView。

我一次填充了这个 listView 10 个项目。

所以,当我显示一个 spalsh 屏幕时,我会在后台获取第 10 个项目。一旦启动画面消失,用户就可以看到前 10 个项目。

现在在包含此 listView 的 Activity 中,我写了如下内容:

onLastItemReached(){
    getNextten();
}

工作正常。当我要从此 listViewActivity 进行其他活动时,我设置了标志,以便在按下返回时保留在此之前浏览的所有项目(我的意思是,如果用户向下滚动到 100 个项目并单击一个项目并进入某个活动和点击设备返回按钮,他将显示第 100 项,其余 99 项不需要再次获取)

问题是当我关闭应用程序并在大约 5-10 分钟后打开它时,它会崩溃说:

ArrayIndexOutOfBoundsException

如果我在 1-3 分钟内重新打开它,它工作正常。

抱歉,我无法发布任何代码,因为所有活动都有大约 800 行。

任何人都知道为什么我总是得到这个例外。我想知道的一件事是,有没有类似的东西if an app is in background for more than 5 mins remove all its data

编辑:

我正在关闭我的应用程序,我的意思是当我在 ListView 屏幕上时,我按下 Home 按钮,5-10 分钟后重新打开应用程序

这是我得到的 StackTrace,我试图处理 unCaughtExceptions:

threadid=1: thread exiting with uncaught exception (group=0x40020660)
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Android.WiC_MobileApp/com.Android.WiC_MobileApp.FeedListViewActivity}: java.lang.ArrayIndexOutOfBoundsException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1728)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1747)
at android.app.ActivityThread.access$1500(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:993)

所以在查看我的日志猫后,它说错误在第 201 行,并且该行包含:

    Log.d("array of product names", SplashActivity.products_array[0] + "--"
            + SplashActivity.products_array[1]);

这是一个填充在 SplashActivity 中并制作的数组public static

4

3 回答 3

2

if an app is in background for more than 5 mins remove all its data

This could actually happen within 1 minute. It's just dependent on the environment that the system is running on. When you press the home button (or back button on the top activity) the system will put your app in a hibernation sort of mode. It keeps it in memory until that memory is needed. This allows the user to quickly go back to it on the event that they simply wanted to switch real quick and go back. After a few minutes though, if the user opened up a game or something, the OS may decide to wipe the app from memory. In which case, when the user reopens your app, you have to rebuild everything from scratch.

The reason your app crashes is because you're trying to retrieve the element before the app has actually rebuilt the ListView. If the app is still in memory, it works because everything is still built. If the app was removed, then the list must be rebuilt, re-inflated, or whatever. Then you try to retrieve the element.

于 2012-08-29T13:29:17.447 回答
2

When your app is in the background, Android can kill it at any time (simply by killing the process that your activities are running in). If the user then wants to return to your app, Android creates a new process for your app and then recreates only the activity that was on the top of the activity stack (ie: the one that was showing when your app went to the background).

From the logs it looks like your app crashes when trying to launch FeedListViewActivity. Since this is probably the activity that was showing when your app went to the background it looks like Android is trying to recreate the activity, which would imply that your process was killed while the app was in the background (see my explanation above).

You say that the data you are trying to access is set up by SplashActivity and kept in a static variable. The problem is that when Android creates a new process for you, it only recreates the top activity, not all the activities underneath that. It doesn't recreate the SplashActivity so that variable has not yet been initialized.

Here are some possible alternatives to deal with this:

  1. Check if the process has been recreated in onCreate() of FeedListViewActivity and, if so, you'll need to recreate the list of data that the SplashActivity had previously set up for you. You can tell if the process has been recreated by having SplashActivity set a public static boolean variable to true when it runs and by checking that variable in FeedListViewActivity.

  2. Check if the process has been recreated in onCreate() of FeedListViewActivity and, if so, simply redirect to your SplashActivity to start the application all over again.

  3. Persist the data by saving it to a Bundle in onSaveInstanceState(). Android will call this method on your activity before the process gets killed so you have a chance to save any data you may need later when the activity/process is recreated. You'll need to restore the data in onCreate() and/or onRestoreInstanceState().

  4. Persist the data in a file or in an SQLlite database.

于 2012-08-29T14:16:59.520 回答
0

你得到,ArrayIndexOutOfBoundsException因为每次当你到达最后一个项目时,你都试图再读 10 条记录。

But suppose your array has 50 records. When you reach to 50 you are again going to read new 10 records. it means 51-60

which are not available that's why you are getting exception

onLastItemReached(){
    if(totalDispayed < yourArray.length())   <-----
    getNextten();
}

You need to implement something like as i have mentioned above

于 2012-08-29T13:26:33.610 回答