简体   繁体   English

stack = java.lang.StackOverflowError:堆栈大小8MB

[英]stack=java.lang.StackOverflowError: stack size 8MB

I'm getting this StackOverFlow error, i understand it fully, but the issue is im not dealing with big data, So how can this error be produced ? 我遇到这个StackOverFlow错误,我完全理解它,但是问题是我不处理大数据,那么该错误怎么产生?

I have an activity, Framelayout, a fragment, 3 options. 我有一个活动,Framelayout,一个片段,三个选项。

In the fragment, when you click on one of those options, it re-create the fragment and put random numbers, the MAX is 15 , So its not that big, this error happens when the user click so fast on the options which cause this overflow. 在片段中,当您单击这些选项之一时,它会重新创建片段并放入随机数,MAX为15 ,所以它不是那么大,当用户快速单击导致该错误的选项时会发生此错误溢出。

This is the code for generating, any ideas about "enhancing" it ? 这是生成代码,关于“增强”的任何想法吗? i don't know if this code is a bad practice for momery using. 我不知道此代码是否对使用Mom不利。

private static List<Integer> SavedNumbers;

public static void SetupSavedNumbersLIst(){
    SavedNumbers = new ArrayList<>();
}


static List<Integer> range;
private static void AddDiff(int mMAX){
    range = new ArrayList<>();
    for(int i = 0 ; i < mMAX ; i++){
        range.add(i);
    }

    range.removeAll(SavedNumbers);
}

private static int ReturnIfDuplic(int mMAX){
    AddDiff(mMAX);
    return new Random().nextInt(range.size());
}


public static int ReturnUniqueSavedNumber(int mMAX){
    int Random = ReturnRandom(mMAX);
    if(SavedNumbers != null && SavedNumbers.size() > 0) {
        if(DoesSavedNumberExist(Random)){
            return ReturnIfDuplic(mMAX);
        } else {
            SavedNumbers.add(Random);
            return Random;
        }
    } else if (SavedNumbers != null && SavedNumbers.size() == 0){
        SavedNumbers.add(Random);
        return Random;
    } else if( SavedNumbers == null){
        SetupSavedNumbersLIst();
        return ReturnUniqueSavedNumber(mMAX);
    } else {
        return 1;
    }
}

private static boolean DoesSavedNumberExist(int Number){
    for(int s: SavedNumbers){
        if(Number == s)
            return true;
    }
    return false;
}

private static int ReturnRandom(int mMAX){
    return new Random().nextInt(mMAX);
}

This line is recursing if you happen to random the same number you have already saved: 如果您随机选择与已保存的相同数字,则此行重新出现:

if(DoesSavedNumberExist(Random)){
    return ReturnUniqueSavedNumber(mMAX);
} 

If you have already saved 1-15, and your max is 15 this will guarantee a stack-overflow as it will never satisfy the if statement. 如果您已经保存了1-15,并且最大为15,这将保证堆栈溢出,因为它将永远无法满足if语句。 This is also generally a bad approach of "random number no good, just try again", since as you add more and more numbers you will add more and more recursions. 这通常也是“随机数不好,请重试”的错误方法,因为随着您添加越来越多的数字,您将添加越来越多的递归。 A better approach may be to have the random number used as an index into the possible numbers that are left to choose. 更好的方法可能是将随机数用作剩下的可供选择的可能数的索引。

i encounter with same problem Stack Size 8MB. 我遇到同样的问题,堆栈大小为8MB。 when i hit my add item api got the same error . 当我点击添加项目api时,遇到了同样的错误。 i thought it was happening at the time of getting values from the edit text. 我认为这是在从编辑文本获取值时发生的。 so that i change my way and get the values in my inIT(); 这样我就改变了方式,并在inIT();获取了值inIT(); before service call 致电之前

inIT()  
sys = input_Systolic.getText().toString();
                dys = input_Diastolic.getText().toString();
                _date = dateView.getText().toString();
                tim = time.getText().toString();

Retrofit call 改造电话

 HashMap<String, Object> pMap = new HashMap<>();
         pMap.put("user_id",loginStatusModal.getUser_id());
         pMap.put("relative_id",rel_id);
         pMap.put("systolic",sys);
         pMap.put("diastolic",dys);
         pMap.put("report_date",_date);
         pMap.put("report_time",tim);

with using this i solve my stack size 8mb error. 用这个我解决了我的堆栈大小8mb错误。

I had the same problem when I was trying to pass many values using intent.putExtra() . 当我尝试使用intent.putExtra()传递许多值时,我遇到了同样的问题。

I resolved this by saving all the data in shared preferences. 我通过将所有数据保存在共享首选项中解决了这一问题。 You can then access any fragments or activities required in the application in a later part. 然后,您可以在以后的部分中访问应用程序中所需的任何片段或活动。

Refer this question to learn more about saving data using shared preferences. 请参阅此问题,以了解有关使用共享首选项保存数据的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误java.lang.StackOverflowError:堆栈大小8MB - Error java.lang.StackOverflowError: stack size 8MB java.lang.StackOverflowError:视图中的堆栈大小为8MB - java.lang.StackOverflowError: stack size 8MB in Views java.lang.StackOverflowError:堆栈大小8MB - java.lang.StackOverflowError: stack size 8MB 值动画制作器中的循环会导致java.lang.StackOverflowError崩溃:堆栈大小8MB - Loop in value animator cause in crash with java.lang.StackOverflowError: stack size 8MB 画画布时出现java.lang.StackOverflowError:堆栈大小8MB - I get java.lang.StackOverflowError: stack size 8MB when i draw my canvas Android Firebase java.lang.StackOverflowError:用户上传图像时堆栈大小 8MB 错误 - Android Firebase java.lang.StackOverflowError: stack size 8MB error when user is uploading an image java.lang.StackOverflowError:android studio上的堆栈大小为8MB。 怎么解决? - java.lang.StackOverflowError: stack size 8MB on android studio. How to solve it? 称为递归onResume活动java.lang.StackOverflowError的方法:堆栈大小8MB - Method called recursive onResume Activity java.lang.StackOverflowError: stack size 8MB Android Firebase实时异常java.lang.StackOverflowError:堆栈大小8MB - Android Firebase Realtime Exception java.lang.StackOverflowError: stack size 8MB StackOverflowError:Android 8.1 ProgressBar上的堆栈大小为8MB - StackOverflowError: stack size 8MB on Android 8.1 of ProgressBar
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM