-3
我想在我的Android應用程序中做一些圖,但我在findviewbyid()獲得NullPointer異常。 由於我在不同的選項卡上繪製圖形,所以我使用了一個單獨的圖形片段。在一個片段中的異常
編輯:我的問題是不同的。我知道我在哪裏以及如何獲取空指針異常,但由於我是Android新手,我不知道如何在這種特殊情況下將其刪除。
代碼圖表片段:
public class Graphs_tab extends android.support.v4.app.Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.graphs_tab,container,false);
GraphView graph = (GraphView) getView().findViewById(R.id.graph);
LineGraphSeries<DataPoint> series = new LineGraphSeries<>(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 rootView;
}}
XML代碼:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<com.jjoe64.graphview.GraphView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/graph" />
</RelativeLayout>
簡單,最好的解決辦法空! –