当我延长Fragment
和Activity
时,我在创建graphview
时遇到类错误
我使用rootview访问findViewById
但我在GraphView Creation上遇到错误
public class M_oneFragment extends Fragment{
public M_oneFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.graph_main, container, false);
return rootView;
// first init data
// sin curve
int num = 150;
GraphViewData[] data = new GraphViewData[num];
double v=0;
for (int i=0; i<num; i++) {
v += 0.2;
data[i] = new GraphViewData(i, Math.sin(v));
}
GraphViewSeries seriesSin = new GraphViewSeries("Sinus curve", new GraphViewSeriesStyle(Color.rgb(200, 50, 00), 3), data);
// cos curve
data = new GraphViewData[num];
v=0;
for (int i=0; i<num; i++) {
v += 0.2;
data[i] = new GraphViewData(i, Math.cos(v));
}
GraphViewSeries seriesCos = new GraphViewSeries("Cosinus curve", new GraphViewSeriesStyle(Color.rgb(90, 250, 00), 3), data);
// random curve
num = 1000;
data = new GraphViewData[num];
v=0;
for (int i=0; i<num; i++) {
v += 0.2;
data[i] = new GraphViewData(i, Math.sin(Math.random()*v));
}
GraphViewSeries seriesRnd = new GraphViewSeries("Random curve", null, data);
// create graph
GraphView graphView = new LineGraphView(
this
, "GraphView"
);
// add data
graphView.addSeries(seriesCos);
graphView.addSeries(seriesSin);
graphView.addSeries(seriesRnd);
// optional - set view port, start=2, size=10
graphView.setViewPort(2, 10);
graphView.setScalable(true);
// optional - legend
graphView.setShowLegend(true);
LinearLayout layout = (LinearLayout)rootView.findViewById(R.id.layout);
layout.addView(graphView);
}
}
创建图表时出错
GraphView graphView = new LineGraphView(
this
, "GraphView"
);
帮帮我
答案 0 :(得分:0)
您需要将上下文传递给LineGraphView的构造函数,因此请使用getActivity()而不是此。