在Android

时间:2017-07-20 15:01:16

标签: java android xml

我正试图让这个图表包起作用:

https://github.com/PhilJay/MPAndroidChart/wiki/Getting-Started

我遇到的问题不在于软件包,但我对如何定义XML以及如何从代码中引用XML缺乏了解。

这是我的班级绘制LineGraph:

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;

import android.support.v7.app.AppCompatActivity;


import java.util.ArrayList;
import java.util.List;

public class ChartHandler extends AppCompatActivity {

    public void initLineChart() {
        // in this example, a LineChart is initialized from xml
        LineChart chart = (LineChart) findViewById(R.id.chart);

        ArrayList<Integer> data = new ArrayList<Integer>();

        data.add(10);
        data.add(20);
        data.add(30);
        data.add(40);
        data.add(50);

        List<Entry> entries = new ArrayList<Entry>();

        for (Integer item : data) {
            // turn your data into Entry objects
            entries.add(new Entry(item, item));
        }

        LineDataSet dataSet = new LineDataSet(entries, "Label"); // add entries to dataset
        dataSet.setColor(170);
        dataSet.setValueTextColor(0);

        LineData lineData = new LineData(dataSet);
        chart.setData(lineData);
        chart.invalidate(); // refresh

    }
}

我不清楚在哪里为图表定义XML,我已将其作为值/ linechart.xml下的新文件添加:

<?xml version="1.0" encoding="utf-8"?>

当我构建时,我得到一个错误,Charthandler.java无法找到符号图表。

注意:如果我在现有的content_main.xml布局中添加此XML,然后将我的代码复制到MainActivity的onCreate函数中,那么它可以工作,但我希望能够在单独的类和XML定义中组织代码在他们自己的文件中

更新

我创建了一个新文件res / layout / linechart.xml

<?xml version="1.0" encoding="utf-8"?>
<com.github.mikephil.charting.charts.LineChart xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/chart"
    android:layout_width="368dp"
    android:layout_height="495dp"
    tools:layout_editor_absoluteY="8dp"
    tools:layout_editor_absoluteX="8dp">
</com.github.mikephil.charting.charts.LineChart>

构建完成,但是当我的代码试图找到id:

时,我收到运行时错误
07-20 11:38:10.361 1290-1290/com.testapp.cloudsnifferclient E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                Process: com.testapp.cloudsnifferclient, PID: 1290
                                                                                java.lang.RuntimeException: Unable to start activity ComponentInfo{com.testapp.cloudsnifferclient/com.testapp.cloudsnifferclient.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.github.mikephil.charting.charts.LineChart.setData(com.github.mikephil.charting.data.ChartData)' on a null object reference
                                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2693)
                                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758)
                                                                                    at android.app.ActivityThread.access$900(ActivityThread.java:177)
                                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448)
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                    at android.os.Looper.loop(Looper.java:145)
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:5942)
                                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                                    at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
                                                                                 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.github.mikephil.charting.charts.LineChart.setData(com.github.mikephil.charting.data.ChartData)' on a null object reference
                                                                                    at com.testapp.MainActivity.onCreate(MainActivity.java:71)
                                                                                    at android.app.Activity.performCreate(Activity.java:6288)
                                                                                    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
                                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
                                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2758) 
                                                                                    at android.app.ActivityThread.access$900(ActivityThread.java:177) 
                                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1448) 
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                    at android.os.Looper.loop(Looper.java:145) 
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:5942) 
                                                                                    at java.lang.reflect.Method.invoke(Native Method) 
                                                                                    at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) 
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) 

1 个答案:

答案 0 :(得分:1)

将其置于res / layout下以解决您的问题