RandomUser
RandomUser

Reputation: 4220

Loading XML resources in Android

I am trying to get this charting package to work:

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

The issue I have isn't with the package, but my lack of understanding of how/where to define the XML and how to reference it from code.

This my class to draw the 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

    }
}

I am not clear on where to define the XML for the chart, which I've added as a new file under values/linechart.xml:

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

When I build I get an error that Charthandler.java can't find the symbol chart.

Note: If I add this XML inside the existing content_main.xml layout and then copy my code into the onCreate function of the MainActivity it works, but I would like to be able to organize the code in a separate class and XML definitions in their own file(s)

Update:

I created a new file 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>

The build completes, but I get a runtime error when my code tries to find the 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) 

Upvotes: 0

Views: 69

Answers (1)

Stevenkang90
Stevenkang90

Reputation: 68

put it under res/layout to resolve your issue

Upvotes: 1

Related Questions