2017-06-12 107 views
0

我想在一個片段中添加了曲線圖,但是我失敗了。我該怎麼做?如何在片段中創建圖形?

這裏是我的代碼片段:

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圖=(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

什麼是你所得到的錯誤?你能否詳細解釋一下正在發生的事情,你想要做什麼,以及哪些工作沒有具體工作? – Marc

+0

我正在錯誤此GraphView圖表=(GraphView)findViewById(R.id.graph); 錯誤:(42,39)錯誤:無法找到符號方法findViewById(int)的 – Murat

+0

您可以編輯您的問題和錯誤補充呢? – Marc

回答

0

更新3:我誤解你的錯誤消息。我道歉。你的錯誤信息是說它找不到Fragment上的findViewById方法。這是因爲Fragments沒有這個方法,只有Activities才行。你必須得到一個關於你放大的視圖,然後調用view.findViewById(R.id.graph),如下面的代碼:

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; 
} 

更新2:將您return語句的你的方法結束。就像這樣:

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); 
} 

更新1:根據更新後的問題,你的代碼表明您從onCreateView內返回您與您的數據集的圖形之前。事實上,你不應該得到你得到的錯誤,因爲代碼永遠不會到達。這是我指的是代碼:

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); Error:(42, 39) error: cannot find symbol method findViewById(int) 

這意味着你的佈局不具有控制使用android:id爲。檢查你的佈局,並確保你有一個控制android:id="@+id/graph"作爲一個屬性。

+0

我有它 Murat

+0

查看我更新的答案。 – Marc

+0

所以我應該怎麼做我不明白:: S – Murat