Вопрос

I'm following the ShinobiCharts for Android quick start guide, except adding the chart fragment programmatically, like so:

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ChartFragment chartFragment = new ChartFragment();
fragmentTransaction.add(R.id.history_container, chartFragment, "chart");
fragmentTransaction.commit();
ShinobiChart shinobiChart = chartFragment.getShinobiChart();

Everything looks to be fine until the last line, when shinobiChart always initializes to null. I'm calling this from a custom class inheriting from Fragment, in the onCreateView method, and I suspect this problem might be rooted in that - but I'm not sure how to fix it.

Это было полезно?

Решение

The ChartFragment gets its shinobiChart during ChartFragment.onCreate, so in your code it's still null at this time - all you've done so far is to instantiate it.

Presumably you're planning to do some chart setup using the ShinobiChart reference, so I'd suggest subclassing ChartFragment, and moving your setup code into its onCreate method (be sure to call super.onCreate(savedInstanceState);).

There's an example of this pattern in the CustomDataAdapter sample.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top