java.lang.ArrayIndexOutOfBoundsException:length = 3;指数= -1

时间:2018-05-25 20:38:52

标签: java android arrays firebase firebase-realtime-database

我尝试从此Firebase数据库中检索Timestamp,并将其设置为x轴标签。对于数据点,它绘制x值是索引,并且每次检测到子项时y值为100.

enter image description here

应用不断崩溃并返回: java.lang.ArrayIndexOutOfBoundsException: length=3; index=-1 我的节点中有3个孩子,但我无法弄清楚为什么我的索引会一直返回 -1

此数据库适用于1个用户。每当我点击另一个客户端时," userid"引用另一个Firebase节点并检索该状态。当我检索第一个客户端数据时,它工作得很好。当我点击第二个并尝试检索数据时,它不会工作。请告诉我,我做错了什么。

这是我的代码:

public class FallHistory extends AppCompatActivity{

String userid, fallstateValue, fallstateTimestamp;
FirebaseAuth auth;
FirebaseUser user;
DatabaseReference ref;
ArrayList<String> array9; //array untuk fallstate
ArrayList<String> array10; //array untuk fallstateTimeStamp
PointsGraphSeries<DataPoint> series ;
int x=0;
int i = 1;
String[] xLabels ;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fall_history);
    auth = FirebaseAuth.getInstance();
    user = auth.getCurrentUser();
    final GraphView graph = (GraphView)findViewById(R.id.graphFall);
    series = new PointsGraphSeries<>();
    final StaticLabelsFormatter staticLabelsFormatter = new StaticLabelsFormatter(graph);
    graph.addSeries(series);
    Viewport vp = graph.getViewport();
    vp.setXAxisBoundsManual(true);
    vp.setMinX(0);
    vp.setMaxX(3); //yg ditunjukin max berapa
    vp.setYAxisBoundsManual(true);
    vp.setMinY(1);
    vp.setMaxY(200); //yg ditunjukin max berapa
    graph.getViewport().setScrollable(true); // enables horizontal scrolling
    //graph.getViewport().setScrollableY(true); // enables vertical scrolling
    //graph.getViewport().setScalable(true); // enables horizontal zooming and scrolling
    //graph.getViewport().setScalableY(true); // enables vertical zooming and scrolling

    array9 = new ArrayList<>(); //array untuk fallstate
    array10 = new ArrayList<>(); //array untuk fallstate timeStamp

    Intent intent = getIntent();
    if(intent!=null)
    {
        userid = intent.getStringExtra("userid");
    }

 ref = FirebaseDatabase.getInstance().getReference().child("Users").child(userid);
    ref.child("fallstate").child("nilaifallstate").addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            fallstateValue = dataSnapshot.child("fallstate").getValue(String.class);
            fallstateTimestamp = dataSnapshot.child("Timestamp").getValue(String.class);
            Log.i("fallstate value", "fallstate value " + fallstateValue);
            Log.i("fall timestamp", "fall timestamp " + fallstateTimestamp);
            array9.add(fallstateValue);
            array10.add(fallstateTimestamp);
            x = x + 1;
            DataPoint point = new DataPoint(x, 100);
            series.appendData(point, true, 1000);

            xLabels = new String[array10.size()];
            array10.toArray(xLabels);


            graph.getGridLabelRenderer().setHumanRounding(true);
            graph.getGridLabelRenderer().setHorizontalLabelsAngle(90);

            graph.getGridLabelRenderer().setLabelFormatter(new DefaultLabelFormatter()
            {
                @Override
                public String formatLabel(double value, boolean isValueX)
                {
                    if (isValueX)
                    {
                        return xLabels[(int) value];
                    }
                    return super.formatLabel(value,isValueX);
                }
            });

            Log.i("xlabels", "xlabels " + xLabels);

        }

请告诉我,我做错了什么。

1 个答案:

答案 0 :(得分:0)

此错误:

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

表示您的数组中有3个元素,并且您尝试从不正确的索引中获取元素。这三个元素放在Java的索引0,1和2中,数组索引以 0 开头!

请注意onChildAdded方法是异步的。使用以下代码行时:

Log.i("xlabels", "xlabels " + xLabels);

您的数组将始终为空。