亲爱的开发人员!
我想在app中使用MPAndroidChart lib。首先,为了改进我的技巧,我想在本教程中制作一个简单的图表:
这很简单,所以对我来说:)。
但是,我的应用程序无法运行。而且我不知道为什么。你能救我吗?
我的XML文件:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:orientation="horizontal"
android:layout_weight="1" >
<Button
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:backgroundTint="#299ED6"
android:drawableLeft="@drawable/gas_ico"
android:text="13,04\nl/100"
style="@style/spalanie_value"
android:textAllCaps="false"/>
<Button
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:backgroundTint="#FF8B00"
android:drawableLeft="@drawable/car0_ico"
android:text="1744\nkm"
style="@style/spalanie_value"
android:textAllCaps="false"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:orientation="horizontal"
android:layout_weight="1" >
<FrameLayout
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#DC3C00"
android:layout_margin="5dp">
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:measureWithLargestChild="false"
android:showDividers="middle|beginning|end"
android:paddingLeft="5dp"
android:paddingTop="10dp"
android:paddingRight="5dp"
android:paddingBottom="10dp">
<TextView
android:id="@+id/tvData"
android:text="@string/data"
style="@style/naglowki_tabeli"
android:layout_gravity="center"
android:layout_width="33dp"
android:layout_weight="1"
android:gravity="center_horizontal" />
<TextView
android:id="@+id/tvSpalanie"
android:text="@string/spalanie"
style="@style/naglowki_tabeli"
android:layout_gravity="center"
android:layout_width="33dp"
android:layout_weight="1"
android:gravity="center_horizontal" />
<TextView
android:id="@+id/tvZasieg"
android:text="@string/zasieg"
style="@style/naglowki_tabeli"
android:layout_gravity="center"
android:layout_width="33dp"
android:layout_weight="1"
android:gravity="center_horizontal" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:measureWithLargestChild="false"
android:padding="5dp"
android:showDividers="middle|beginning|end">
<TextView
android:id="@+id/tvDataData"
android:text="2015-03-24\n2015-04-12\n2015-05-17\n2015-06-05"
style="@style/dane_tabeli"
android:layout_gravity="center"
android:layout_width="33dp"
android:layout_weight="1"
android:gravity="center_horizontal"
android:layout_height="fill_parent" />
<TextView
android:id="@+id/tvSpalanieData"
android:text="8,12 l\n12,16 l\n17,20 l\n14,66 l"
style="@style/dane_tabeli"
android:layout_gravity="center"
android:layout_width="33dp"
android:layout_weight="1"
android:gravity="center_horizontal" />
<TextView
android:id="@+id/tvZasiegData"
android:text="480 km\n405 km\n370 km\n389 km"
style="@style/dane_tabeli"
android:layout_gravity="center"
android:layout_width="33dp"
android:layout_weight="1"
android:gravity="center_horizontal" />
</TableRow>
</TableLayout>
</FrameLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:orientation="horizontal"
android:layout_weight="1" >
<FrameLayout
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#008272"
android:layout_margin="5dp" >
<com.github.mikephil.charting.charts.BarChart
android:id="@+id/chart"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
</LinearLayout>
我的JAVA文件:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNavigationDrawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
ArrayList entries = new ArrayList();
entries.add(new BarEntry(4f, 0));
entries.add(new BarEntry(8f, 1));
entries.add(new BarEntry(6f, 2));
entries.add(new BarEntry(12f, 3));
entries.add(new BarEntry(18f, 4));
entries.add(new BarEntry(9f, 5));
BarDataSet mydataset = new BarDataSet(entries, "# of Calls");
ArrayList labels = new ArrayList();
labels.add("January");
labels.add("February");
labels.add("March");
labels.add("April");
labels.add("May");
labels.add("June");
BarChart b1;
b1=(BarChart) findViewById(R.id.chart);
BarData dane = new BarData(labels,mydataset);
b1.setData(dane); }
结果:app和logcat崩溃:
06-25 10:54:12.418 26699-26699 /?我/艺术:延迟启用-Xcheck:jni 06-25 10:54:12.572 26699-26699 / com.mkilinski.spalanie D / AndroidRuntime:关闭VM 06-25 10:54:12.573 26699-26699 / com.mkilinski.spalanie E / AndroidRuntime:致命异常:主要 过程:com.mkilinski.spalanie,PID:26699 java.lang.RuntimeException:无法启动活动ComponentInfo {com.mkilinski.spalanie / com.mkilinski.spalanie.MainActivity}:java.lang.NullPointerException:尝试调用虚方法&#39; void com.github.mikephil.charting .charts.BarChart.setData(com.github.mikephil.charting.data.ChartData)&#39;在空对象引用上
答案 0 :(得分:0)
似乎如果我在Java和setContentView(chartName)中动态创建图表,它运行正常。 但是那些可以访问xml中定义的图表,调用 (xxxChart)findViewById(R.id.my_chart_id);抛出这个例外?