标签: indexoutofboundsexception

JVM错误?Cached Object字段值导致ArrayIndexOutOfBoundsException

这有点奇怪,但代码说的多于单词,所以看看测试看看我在做什么.在我当前的设置(Windows 64位上的Java 7更新21)中,此测试因ArrayIndexOutOfBoundsException而失败,但是将测试方法代码替换为注释代码,它可以正常工作.我想知道Java规范中是否有任何部分可以解释原因.

在我看来,正如"michael nesterenko"建议的那样,在调用方法之前,数组字段的值被缓存在堆栈中,并且在从调用返回时不会更新.我不知道它是JVM错误还是记录在案的"优化".没有涉及多线程或"魔术".

public class TestAIOOB {
    private String[] array = new String[0];
    private int grow(final String txt) {
        final int index = array.length;
        array = Arrays.copyOf(array, index + 1);
        array[index] = txt;
        return index;
    }
    @Test
    public void testGrow() {
        //final int index = grow("test");
        //System.out.println(array[index]);
        System.out.println(array[grow("test")]);
    }
}
Run Code Online (Sandbox Code Playgroud)

java jvm indexoutofboundsexception

7
推荐指数
1
解决办法
190
查看次数

android.graphics.Paint.getTextRunAdvances 处的 IndexOutOfBoundsException(Paint.java:1774)

在我的应用程序中,用户有时会遇到错误并且应用程序崩溃。用户向我发送日志:

java.lang.IndexOutOfBoundsException
 at android.graphics.Paint.getTextRunAdvances(Paint.java:1774)
 at android.graphics.Paint.getTextRunAdvances(Paint.java:1747)
 at android.text.MeasuredText.addStyleRun(MeasuredText.java:164)
 at android.text.MeasuredText.addStyleRun(MeasuredText.java:204)
 at android.text.StaticLayout.generate(StaticLayout.java:281)
 at android.text.DynamicLayout.reflow(DynamicLayout.java:332)
 at android.text.DynamicLayout.<init>(DynamicLayout.java:176)
 at android.widget.TextView.makeSingleLayout(TextView.java:6089)
 at android.widget.TextView.makeNewLayout(TextView.java:5987)
 at android.widget.TextView.onMeasure(TextView.java:6344)
 at android.view.View.measure(View.java:15479)
 at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:617)
 at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:399)
 at android.view.View.measure(View.java:15479)
 at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4826)
 at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1396)
 at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1038)
 at android.widget.LinearLayout.onMeasure(LinearLayout.java:576)
 at android.view.View.measure(View.java:15479)
 at android.widget.ListView.setupChild(ListView.java:1847)
 at android.widget.ListView.makeAndAddView(ListView.java:1772)
 at android.widget.ListView.fillDown(ListView.java:672)
 at android.widget.ListView.fillSpecific(ListView.java:1330)
 at android.widget.ListView.layoutChildren(ListView.java:1600)
 at android.widget.AbsListView.onLayout(AbsListView.java:2300)
 at android.view.View.layout(View.java:14061)
 at android.view.ViewGroup.layout(ViewGroup.java:4374)
 at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
 at android.view.View.layout(View.java:14061)
 at android.view.ViewGroup.layout(ViewGroup.java:4374)
 at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1655)
 at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1513)
 at android.widget.LinearLayout.onLayout(LinearLayout.java:1426)
 at android.view.View.layout(View.java:14061)
 at android.view.ViewGroup.layout(ViewGroup.java:4374)
 at android.widget.RelativeLayout.onLayout(RelativeLayout.java:948)
 at android.view.View.layout(View.java:14061)
 at android.view.ViewGroup.layout(ViewGroup.java:4374)
 at android.widget.FrameLayout.onLayout(FrameLayout.java:448) …
Run Code Online (Sandbox Code Playgroud)

android indexoutofboundsexception

7
推荐指数
1
解决办法
4101
查看次数

RecyclerView:IndexOutOfBoundsException检测到不一致.商品位置无效

致命异常:java.lang.IndexOutOfBoundsException检测到不一致.项目位置5无效(偏移量:5).state:24

这种崩溃发生得太多了!!!

7107用户和12k崩溃!

我在Fabric Crashlytics中得到它.

Fatal Exception: java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid item position 5(offset:5).state:41 at android.support.v7.widget.RecyclerView$Recycler.clear(Unknown Source) at android.support.v7.widget.GapWorker.add(Unknown Source) at android.support.v7.widget.GapWorker.add(Unknown Source) at android.support.v7.widget.GapWorker.remove(Unknown Source) at android.support.v7.widget.GapWorker.add(Unknown Source) at android.support.v7.widget.GapWorker.run(Unknown Source) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5930) at java.lang.reflect.Method.invoke(Method.java) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)

DateList是涉及RecyclerView的片段:

public class DateList extends Fragment {

DatesItemAdapter adapter;
List<DateItem2> keyList;
CardView cardView;
double v = 1;
float radius = 1;
ProgressBarCircular mCircular;
SwipeRefreshLayout swipeRefreshLayout;
RecyclerView mRecyclerView;
Animation animSlideUp;
Animation animSlideDown;
Uri.Builder builder; …
Run Code Online (Sandbox Code Playgroud)

android adapter indexoutofboundsexception crashlytics android-recyclerview

7
推荐指数
1
解决办法
2956
查看次数

神秘的 indexOutOfBoundsException 涉及 Android 中的 measureLimit

我的代码在 99% 的情况下都可以完美运行,但是如果我将键盘混搭的时间足够长,有时我可以让它抛出 IndexOutOfBounds 异常。我无法确切地告诉您如何重现它,因为如果没有字面上的键盘捣碎时间,我自己无法重现它。

边界与我不同的 editText 的字符限制相匹配,但我无法弄清楚是什么导致操作尝试在边界之外执行某些操作。

该错误已由 pushDigitToNextEditText、moveCursorIfNecessary 和 pushDigitToFront 方法(间接)触发,但它也发生在我的堆栈跟踪中没有任何代码,只有 android 源代码。(这怎么可能?)

我几乎找不到关于这种特殊类型的 outOfBounds 异常的信息,我几乎准备放弃了。我不敢相信实际用户会像我必须让它抛出错误的方式滥用编辑文本框,但谁知道呢?

出于同样的原因,我无法告诉您如何重现它,我不知道所需的最少代码量是多少。所以这就是全部。我添加了一个堆栈跟踪,但我可以给你更多。我有很多。

package com.example.phonenumberdemo

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.widget.EditText
import com.example.phonenumberdemo.databinding.ActivityMainBinding
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        setUpPhoneEditTexts()
    }

    private var digitDeletedFlag = false
    private var userIsNotMakingChanges = false
    private var prevEditText1CursorPosition: Int? = null
    private var prevEditText2CursorPosition: Int? = null

    private …
Run Code Online (Sandbox Code Playgroud)

java android indexoutofboundsexception kotlin

7
推荐指数
0
解决办法
616
查看次数

限制JavaFX TextField的字符数会导致撤消时出现IndexOutOfBounds

我要求限制用户可以输入到TextFieldJavaFX控件中的字符数.我已经延长TextField,像这样

public class LengthLimitedTextField extends TextField {
    /**
     * @param maxCharacters The max allowed characters that can be entered into this {@link TextField}.
     */
    public LengthLimitedTextField(final int maxCharacters) {
        final TextField thisField = this;
        this.textProperty().addListener(new ChangeListener<String>() {
            @Override
            public void changed(ObservableValue<? extends String> observable,
                                String oldValue, String newValue) {
                // Force correct length by deleting the last entered character if text is longer than maxCharacters
                if (newValue.length() > maxCharacters) {
                    thisField.deleteNextChar();
                }
            }
        });
    }
} …
Run Code Online (Sandbox Code Playgroud)

java text indexoutofboundsexception javafx-2 javafx-8

6
推荐指数
2
解决办法
5890
查看次数

RecyclerView IndexOutOfBoundsException

当我RecyclerView通过循环删除一些项目时,为什么执行异常?我Collentions.synchronizedMap在适配器中使用'deleteItem方法'也使用了synchronized(片段中的方法).

public void elementController(JsonObject jsonObject , String type)   {
    if ( jsonObject == null || type == null )    {
        return;
    }

    int position =0 , resultPosition =0;
    if ( type.equals("update") || type.equals("delete"))    {

        String id = jsonObject.get(ELEMENT_ID).getAsString();
        Map<String , Element> map = gridFragment.getMap();

        synchronized (map) {  
            for (String s : map.keySet()) {
                if (s.equals(id)) {
                    resultPosition = position;
                } else {
                    position++;
                }
            }
        }
    }

    if(position-1 > gridFragment.getmAdapter().getData().size() || position <0)   {
        return;
    } …
Run Code Online (Sandbox Code Playgroud)

android synchronized indexoutofboundsexception android-recyclerview

6
推荐指数
2
解决办法
1万
查看次数

在 TextView 上膨胀 xml 布局 ArrayIndexOutOfBoundsException 时出错

我遇到了一个奇怪的崩溃问题。我似乎无法弄清楚问题所在。

这是崩溃报告。

                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.bigappcompany.grocery/com.bigappcompany.grocery.activity.OrderDetailsActivity}: android.view.InflateException: Binary XML file line #125: Binary XML file line #125: Error inflating class <unknown>
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2560)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2626)
                  at android.app.ActivityThread.-wrap11(ActivityThread.java)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1475)
                  at android.os.Handler.dispatchMessage(Handler.java:111)
                  at android.os.Looper.loop(Looper.java:207)
                  at android.app.ActivityThread.main(ActivityThread.java:5740)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:766)
               Caused by: android.view.InflateException: Binary XML file line #125: Binary XML file line #125: Error inflating class <unknown>
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
                  at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
                  at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292)
                  at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
                  at com.bigappcompany.grocery.activity.OrderDetailsActivity.onCreate(OrderDetailsActivity.java:11)
                  at android.app.Activity.performCreate(Activity.java:6543)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2513)
                  at …
Run Code Online (Sandbox Code Playgroud)

xml android textview android-layout indexoutofboundsexception

6
推荐指数
1
解决办法
2072
查看次数

RecyclerView java.lang.IndexOutOfBoundsException:检测到不一致。无效的视图支架适配器位置。由于更改了getItemCount()

我正在java.lang.IndexOutOfBoundsException: Inconsistency detected使用RecyclerView,但是只有在我似乎将其扔掉时才使用。当我滚动得足够慢时,它不会崩溃。

我将基本适配器用于许多其他子类,但是只有某些子类会崩溃。知道为什么会这样吗?

编辑

我发现发生这种情况的原因是因为getItemCount()更改取决于页脚加载视图的状态。我相信这会导致更改和滚动时出现不一致。

我通过始终在getItemCount()中的页眉/页脚加载视图返回2个额外的项来进行测试。而不是根据其状态动态计算计数。解决此问题的好方法是什么?

我的适配器

public abstract class BaseRecyclerAdapter<T>
        extends RecyclerView.Adapter<RecyclerView.ViewHolder>
        implements LoadingAndErrorViewHolder.LoadingViewHolderListener {

    public static final int STATUS_HIDDEN = 0;
    public static final int STATUS_LOADING = 1;
    public static final int STATUS_ERROR = 2;

    protected static final int TYPE_HEADER_LOADING = 0;
    protected static final int TYPE_FOOTER_LOADING = 1;
    protected static final int TYPE_NORMAL = 2;

    private static final int POSITION_TYPE_HEADER_LOADING = 0;

    private BaseRecyclerAdapterListener mBaseRecyclerAdapterListener;
    private final List<T> mBackingList = …
Run Code Online (Sandbox Code Playgroud)

android indexoutofboundsexception recycler-adapter android-recyclerview

6
推荐指数
1
解决办法
6682
查看次数

从android kotlin中的文件夹中获取图像列表

我正在尝试使用此功能从文件夹中获取图像列表

var gpath:String = Environment.getExternalStorageDirectory().absolutePath
var spath = "testfolder"
var fullpath = File(gpath + File.separator + spath)
var list = imageReader(fullpath)

fun imageReader(root : File):ArrayList<File>{
    val a : ArrayList<File> ? = null
    val files = root.listFiles()
    for (i in 0..files.size){
        if (files[i].name.endsWith(".jpg")){
            a?.add(files[i])
        }
    }
    return a!!
}
Run Code Online (Sandbox Code Playgroud)

但我有这些例外:

java.lang.ArrayIndexOutOfBoundsException:length=3;index=3

kotin.kotlinNullPointerException

我读过这个问题,但我不知道如何解决它,

有什么帮助吗?

android arraylist nullpointerexception indexoutofboundsexception kotlin

6
推荐指数
2
解决办法
8087
查看次数

Android:getChildDrawingOrder() 返回无效索引 1(子计数为 1)

这是堆栈跟踪:

Fatal Exception: java.lang.IndexOutOfBoundsException: getChildDrawingOrder() returned invalid index 1 (child count is 1)
   at android.view.ViewGroup.getAndVerifyPreorderedIndex(ViewGroup.java:1988)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4204)
   at android.view.View.draw(View.java:20373)
   at android.view.View.updateDisplayListIfDirty(View.java:19315)
   at android.view.View.draw(View.java:20093)
   at android.view.ViewGroup.drawChild(ViewGroup.java:4421)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4207)
   at android.view.View.updateDisplayListIfDirty(View.java:19306)
   at android.view.View.draw(View.java:20093)
   at android.view.ViewGroup.drawChild(ViewGroup.java:4421)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4207)
   at android.view.View.updateDisplayListIfDirty(View.java:19306)
   at android.view.View.draw(View.java:20093)
   at android.view.ViewGroup.drawChild(ViewGroup.java:4421)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4231)
   at android.view.View.draw(View.java:20373)
   at androidx.viewpager.widget.ViewPager.draw(ViewPager.java:2426)
   at android.view.View.updateDisplayListIfDirty(View.java:19315)
   at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4405)
   at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4385)
   at android.view.View.updateDisplayListIfDirty(View.java:19274)
   at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4405)
   at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4385)
   at android.view.View.updateDisplayListIfDirty(View.java:19274)
   at android.view.View.draw(View.java:20093)
   at android.view.ViewGroup.drawChild(ViewGroup.java:4421)
   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4231)
   at android.view.View.updateDisplayListIfDirty(View.java:19306)
   at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4405)
   at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4385)
   at android.view.View.updateDisplayListIfDirty(View.java:19274)
   at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:4405)
   at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:4385)
   at …
Run Code Online (Sandbox Code Playgroud)

android viewgroup indexoutofboundsexception

6
推荐指数
1
解决办法
703
查看次数