我有一个名为NewFragment
的片段,我试图在名为PerformanceGraph
的类中调用方法。
该类应该绘制一个我打算使用片段显示的图形。这是在意图中返回的。
这是我的Fragment类:
public class NewFragment extends Fragment {
public static final NewFragment newInstance() {
NewFragment f = new NewFragment();
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.newfragment, container, false);
PerformanceGraph line = new PerformanceGraph();
Intent intent = line.getIntent(getApplicationContext());
startActivity(intent);
return v;
}
}
这是我的PerformanceGraph课程:
public class PerformanceGraph {
public Intent getIntent(Context context) {
int x[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
int y[] = { 1, 4, 9, 16, 25, 36, 38, 39, 38, 37, 36, 32, 31, 30, 30 };
int z[] = { 3, 4, 6, 7, 4, 3, 2, 2, 4, 3, 5, 5, 4, 6, 7 };
String xaxis[] = { "jan", "feb", "mar", "apr", "may", "jun", "jul",
"aug", "sept", "oct", "nov", "dec", "jan", "feb", "mar" };
TimeSeries series = new TimeSeries("line1");
TimeSeries series2 = new TimeSeries("line2");
for (int i = 0; i < x.length; i++) {
series.add(x[i], y[i]);
series2.add(x[i], z[i]);
}
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
dataset.addSeries(series);
dataset.addSeries(series2);
XYSeriesRenderer renderer = new XYSeriesRenderer();
renderer.setColor(Color.BLUE);
renderer.setPointStyle(PointStyle.SQUARE);
renderer.setFillPoints(true);
XYSeriesRenderer renderer2 = new XYSeriesRenderer();
renderer2.setColor(Color.DKGRAY);
renderer2.setPointStyle(PointStyle.CIRCLE);
renderer2.setFillPoints(false);
XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
mRenderer.addSeriesRenderer(renderer);
mRenderer.addSeriesRenderer(renderer2);
mRenderer.setChartTitle("haha");
mRenderer.setXLabels(0);
mRenderer.setXTitle("Duration");
mRenderer.setYTitle("Price (ksh)");
mRenderer.setApplyBackgroundColor(true);
mRenderer.setAxisTitleTextSize(15);
mRenderer.setChartTitleTextSize(20);
mRenderer.setLabelsTextSize(15);
mRenderer.setLegendTextSize(12);
mRenderer.setPointSize(2f);
mRenderer.setAxesColor(Color.BLACK);
mRenderer.setYLabelsAlign(Align.RIGHT);
mRenderer.setYLabelsVerticalPadding(2);
mRenderer.setShowGridY(true);
mRenderer.setGridColor(Color.GRAY);
mRenderer.setLabelsColor(Color.parseColor("#5f5f5f"));
mRenderer.setBackgroundColor(Color.parseColor("#F5F5F5"));
mRenderer.setMarginsColor(Color.parseColor("#F5F5F5"));
mRenderer.setPanEnabled(false, false);
mRenderer.setZoomEnabled(false, false);
for (int i = 0; i < x.length; i++) {
mRenderer.addXTextLabel(x[i], xaxis[i]);
}
Intent intent = ChartFactory.getLineChartIntent(context, dataset,
mRenderer, "Line graph title");
return intent;
}
}
问题是,在片段类中,我在尝试调用getIntent()
时遇到错误。必须在参数中提供应用程序上下文。
我怎样才能做到这一点?尝试getIntent(getApplicationContext())
或getIntent(this)
无法解决问题。
答案 0 :(得分:1)
在xml for fragment中,将线性布局放置在您想要的位置并自定义。
<LinearLayout
android:layout_width="wrap_content"
android:id="@+id/chart"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginLeft="22dp"
android:layout_marginTop="23dp"
android:orientation="vertical" >
</LinearLayout>
然后
public View displayLineChart()
{
int x[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
int y[] = { 1, 4, 9, 16, 25, 36, 38, 39, 38, 37, 36, 32, 31, 30, 30 };
int z[] = { 3, 4, 6, 7, 4, 3, 2, 2, 4, 3, 5, 5, 4, 6, 7 };
String xaxis[] = { "jan", "feb", "mar", "apr", "may", "jun", "jul",
"aug", "sept", "oct", "nov", "dec", "jan", "feb", "mar" };
TimeSeries series = new TimeSeries("line1");
TimeSeries series2 = new TimeSeries("line2");
for (int i = 0; i < x.length; i++) {
series.add(x[i], y[i]);
series2.add(x[i], z[i]);
}
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
dataset.addSeries(series);
dataset.addSeries(series2);
XYSeriesRenderer renderer = new XYSeriesRenderer();
renderer.setColor(Color.BLUE);
renderer.setPointStyle(PointStyle.SQUARE);
renderer.setFillPoints(true);
XYSeriesRenderer renderer2 = new XYSeriesRenderer();
renderer2.setColor(Color.DKGRAY);
renderer2.setPointStyle(PointStyle.CIRCLE);
renderer2.setFillPoints(false);
XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
mRenderer.addSeriesRenderer(renderer);
mRenderer.addSeriesRenderer(renderer2);
mRenderer.setChartTitle("haha");
mRenderer.setXLabels(0);
mRenderer.setXTitle("Duration");
mRenderer.setYTitle("Price (ksh)");
mRenderer.setApplyBackgroundColor(true);
mRenderer.setAxisTitleTextSize(15);
mRenderer.setChartTitleTextSize(20);
mRenderer.setLabelsTextSize(15);
mRenderer.setLegendTextSize(12);
mRenderer.setPointSize(2f);
mRenderer.setAxesColor(Color.BLACK);
mRenderer.setYLabelsAlign(Align.RIGHT);
// mRenderer.setYLabelsVerticalPadding(2);
mRenderer.setShowGridY(true);
mRenderer.setGridColor(Color.GRAY);
mRenderer.setLabelsColor(Color.parseColor("#5f5f5f"));
mRenderer.setBackgroundColor(Color.parseColor("#F5F5F5"));
mRenderer.setMarginsColor(Color.parseColor("#F5F5F5"));
mRenderer.setPanEnabled(false, false);
mRenderer.setZoomEnabled(false, false);
for (int i = 0; i < x.length; i++) {
mRenderer.addXTextLabel(x[i], xaxis[i]);
}
mChartView = ChartFactory.getLineChartView(getActivity(), dataset,
mRenderer);
return mChartView;
}
然后在onCreateView
View v = inflater.inflate(R.layout.newfragment, container, false);
LinearLayout layout = (LinearLayout) view.findViewById(R.id.chart);
layout.addView(displayLineChart());
快照