我想在片段中添加折线图,但是我失败了。我该怎么办?
这是我的片段代码:
public class Info extends Fragment
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
return inflater.inflate(R.layout.info, container, false);
GraphView graph = (GraphView) findViewById(R.id.graph);
LineGraphSeries<DataPoint> series = new LineGraphSeries<DataPoint>(new DataPoint[] {
new DataPoint(0, 1),
new DataPoint(1, 5),
new DataPoint(2, 3),
new DataPoint(3, 2),
new DataPoint(4, 6)
});
graph.addSeries(series);
}
}
当我尝试运行时,我收到此错误: GraphView graph =(GraphView)findViewById(R.id.graph); 错误:(42,39)错误:找不到符号方法findViewById(int)
我的info.xml中也有这个
<com.jjoe64.graphview.GraphView
android:layout_width="match_parent"
android:layout_height="125dp"
android:id="@+id/graph" />
但仍然存在错误。
答案 0 :(得分:0)
UPDATE 3: I misread your error message. I apologize. Your error message is saying it can't find the method findViewById on the Fragment. This is because Fragments do not have this method, only Activities do. You must get a reference to your inflated view and then call view.findViewById(R.id.graph), like in the following code:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.info, container, false);
GraphView graph = (GraphView) view.findViewById(R.id.graph);
LineGraphSeries<DataPoint> series = new LineGraphSeries<DataPoint>(new
DataPoint[] {
new DataPoint(0, 1),
new DataPoint(1, 5),
new DataPoint(2, 3),
new DataPoint(3, 2),
new DataPoint(4, 6)
});
graph.addSeries(series);
return view;
}
UPDATE 2: Move your return statement to the end of your method. Like this:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
GraphView graph = (GraphView) findViewById(R.id.graph);
LineGraphSeries<DataPoint> series = new LineGraphSeries<DataPoint>(new
DataPoint[] {
new DataPoint(0, 1),
new DataPoint(1, 5),
new DataPoint(2, 3),
new DataPoint(3, 2),
new DataPoint(4, 6)
});
graph.addSeries(series);
return inflater.inflate(R.layout.info, container, false);
}
UPDATE 1: According to your updated question, your code shows that you are returning from inside your onCreateView
before you set your graph with your data. In fact, you shouldn't be getting the error you're getting since that code never gets reached. This is the code I'm referring to:
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
return inflater.inflate(R.layout.info, container, false);
GraphView graph = (GraphView) findViewById(R.id.graph);
LineGraphSeries<DataPoint> series = new LineGraphSeries<DataPoint>(new
DataPoint[] {
new DataPoint(0, 1),
new DataPoint(1, 5),
new DataPoint(2, 3),
new DataPoint(3, 2),
new DataPoint(4, 6)
});
graph.addSeries(series);
}
In the comments of your question, you said you're getting
GraphView graph = (GraphView) findViewById(R.id.graph); Error:(42, 39) error: cannot find symbol method findViewById(int)
This means your layout doesn't have a control with an android:id of graph
. Check your layout and make sure you have a control with android:id="@+id/graph"
as an attribute.