简体   繁体   English

ArrayList错误,应用程序崩溃

[英]ArrayList Error, App Crashes

I have the following problem. 我有以下问题。 I have a two fragment application with a static arraylist which is used in both fragments . 我有一个带有静态arraylist的两个fragment应用程序,这两个fragments中都使用了它。

The occurance of the error can be described like this: 错误的发生可以这样描述:

  1. Add entries to arraylist ( FragmentA ) entries添加到arraylistFragmentA
  2. Hit button, display arraylist in chart ( Fragment B ) 点击按钮,在图表中显示arraylist片段B
  3. Go back to Fragment A , add another entrie to arraylist -> error occurs, app crashes 返回片段A ,将另一个entrie添加到arraylist >发生错误,应用崩溃

The code of the fragments is this: 片段的代码是这样的:

  • Fragment A 片段A

     button.setOnClickListener(new View.OnClickListener() { public void onClick (View view) { if (editText3.getText().toString().matches("")) { Toast.makeText(getActivity(), "You did not enter a Valid Item ID", Toast.LENGTH_LONG).show(); return; } else if (editText2.getText().toString().matches("")) { Toast.makeText(getActivity(), "You did not enter a Valid Quantitiy", Toast.LENGTH_LONG).show(); return; } else { String nomen = (editText3.getText().toString()); float number = Float.parseFloat(editText2.getText().toString()); entries.add(new PieEntry(number, nomen)); editText3.setText(""); editText2.setText(""); editText3.requestFocus(); } } }); 
  • Fragment B 片段B

      button2.setOnClickListener(new View.OnClickListener() { public void onClick (View view) { getResources().getColor(R.color.violp); getResources().getColor(R.color.bluep); getResources().getColor(R.color.redp); getResources().getColor(R.color.greenp); getResources().getColor(R.color.yellowp); getResources().getColor(R.color.orangep); getResources().getColor(R.color.lightbluep); getResources().getColor(R.color.purplep); getResources().getColor(R.color.darkredp); PieDataSet set = new PieDataSet(entries, ""); set.setColors(new int[]{R.color.bluep, R.color.greenp, R.color.violp, R.color.redp, R.color.yellowp, R.color.orangep, R.color.lightbluep, R.color.purplep, R.color.darkredp}, getActivity()); PieData datax = new PieData(set); piechart.setData(datax); Toast.makeText(getActivity(), (R.string.loading), Toast.LENGTH_SHORT).show(); piechart.setNoDataText(String.valueOf(R.string.nodata)); piechart.notifyDataSetChanged(); piechart.animateX(2000, Easing.EasingOption.EaseInExpo); piechart.animateY(2000, Easing.EasingOption.EaseInExpo); } }); 

THE ERROR IM GETTING IS THIS: 即时消息获取错误是这样的:

java.lang.ArrayIndexOutOfBoundsException: length=1; java.lang.ArrayIndexOutOfBoundsException:length = 1; index=1 索引= 1

If some expereienced experts could help me out I would be very grateful. 如果一些经验丰富的专家可以帮助我,我将非常感激。 Thanks. 谢谢。

UPDATE: ERROR STACK INCLUDED 更新:包含错误堆栈

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.nextpietwo, PID: 2528
                  java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
                      at com.github.mikephil.charting.renderer.PieChartRenderer.drawDataSet(PieChartRenderer.java:244)
                      at com.github.mikephil.charting.renderer.PieChartRenderer.drawData(PieChartRenderer.java:147)
                      at com.github.mikephil.charting.charts.PieChart.onDraw(PieChart.java:126)
                      at android.view.View.draw(View.java:17185)
                      at android.view.View.updateDisplayListIfDirty(View.java:16167)
                      at android.view.View.draw(View.java:16951)
                      at android.view.ViewGroup.drawChild(ViewGroup.java:3727)
                      at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3513)
                      at android.view.View.updateDisplayListIfDirty(View.java:16162)
                      at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3711)
                      at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3691)
                      at android.view.View.updateDisplayListIfDirty(View.java:16130)
                      at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3711)
                      at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3691)
                      at android.view.View.updateDisplayListIfDirty(View.java:16130)
                      at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3711)
                      at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3691)
                      at android.view.View.updateDisplayListIfDirty(View.java:16130)
                      at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3711)
                      at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3691)
                      at android.view.View.updateDisplayListIfDirty(View.java:16130)
                      at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3711)
                      at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3691)
                      at android.view.View.updateDisplayListIfDirty(View.java:16130)
                      at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3711)
                      at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3691)
                      at android.view.View.updateDisplayListIfDirty(View.java:16130)
                      at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3711)
                      at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3691)
                      at android.view.View.updateDisplayListIfDirty(View.java:16130)
                      at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:648)
                      at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:654)
                      at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:762)
                      at android.view.ViewRootImpl.draw(ViewRootImpl.java:2800)
                      at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2608)
                      at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2215)
                      at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1254)
                      at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6337)
                      at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)
                      at android.view.Choreographer.doCallbacks(Choreographer.java:686)
                      at android.view.Choreographer.doFrame(Choreographer.java:621)
                      at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
                      at android.os.Handler.handleCallback(Handler.java:751)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:154)
                      at android.app.ActivityThread.main(ActivityThread.java:6119)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

MPAndroidChart cannot shield you from the effects of not separating model and view layers and from issues with the managing the lifecycles of Fragments and Activities. MPAndroidChart无法使您免受不分离模型层视图层的影响以及管理片段和活动的生命周期问题的影响。 Please note that "communicating" between Fragments (such as one Fragment manipulating the data of another) is not recommended by Google : 请注意, Google不建议在片段之间进行“通信”(例如,一个片段处理另一个片段的数据):

Two Fragments should never communicate directly 两个片段永远不要直接通信

Although Entry may look like a member of the model layer, it is actually designed to be tightly coupled with PieChart which is part of the view layer. 尽管Entry看起来像是模型层的成员,但实际上将其设计为与视图层一部分的PieChart紧密耦合。 It definitely should not outlive the View with which it is associated and if you want to avoid errors such as the ArrayIndexOutOfBoundsException , the backing List lifecycle should coincide with that of the Fragment in which it is used. 它绝对应该活得比与它相关联的搜索,如果你想避免的错误,如ArrayIndexOutOfBoundsException ,后台列表的生命周期应该与其所使用的片段的一致。 This does not mean that you can never add/remove from a List<Entry> once this is part of a PieChart - MPAndroidChart will allow you to do this within the above constraints. 这并不意味着你不能添加/删除一个List<Entry>一旦这是一部分PieChart - MPAndroidChart将允许你在上述约束内做到这一点。

To fix the above error, I would suggest a different approach than sharing a static object which is almost never a good idea in Android. 为了解决上述错误,我建议使用一种不同于共享静态对象的方法,这在Android中几乎从来都不是一个好主意。 You could share, instead, a separate model layer which contains the data you will use to construct the List<Entry> . 相反,您可以共享一个单独的模型层,其中包含将用于构造List<Entry> When Fragment A updates the model, you can use an event to trigger rebuilding of the chart from the data inside Fragment B. 当片段A更新模型时,您可以使用一个事件触发从片段B中的数据重建图表。

You have stated in the comments that you are using a ViewPager . 您已经在注释中指出您正在使用ViewPager The ViewPager uses fragmentTransaction.show() and .hide() and generally keeps the Fragments in a resumed state. ViewPager使用fragmentTransaction.show().hide() ,通常使Fragments处于恢复状态。 Perhaps the easiest solution is to override onHiddenChanged as this is always called when the ViewPager switches to a new page. 也许最简单的解决方案是重写onHiddenChanged因为在ViewPager切换到新页面时始终会调用此方法。 I think (not 100% sure) that you will also need a call in onActivityCreated(View v) . 我认为(并非100%肯定)您还将需要在onActivityCreated(View v)进行调用。

So, you would have something like this: 因此,您将获得以下内容:

@Override
onActivityCreated(View v) {
    //get updated model from Activity
    //rebuild chart from updated model
}

@Override 
onHiddenChanged(boolean hidden) {
    if (hidden == false) {
        //get updated model from Activity
        //rebuild chart from updated model
    }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM