MPAndroidChart折线图资源空

时间:2017-09-19 08:14:15

标签: java android android-layout charts

我正在尝试做一个简单的折线图示例,我发现here,但是当我运行代码时,即使资源存在,当我尝试调用行

lineChart = (LineChart) findViewById(R.id.lineChart);

它返回null。任何人都知道如何知道资源,但然后返回null?

这是我的布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent">

    <com.github.mikephil.charting.charts.LineChart
        android:id="@+id/lineChart"
        android:layout_width="match_parent"
        android:layout_height="400dp"
        android:visibility="visible"></com.github.mikephil.charting.charts.LineChart>
</RelativeLayout>

,这是java文件

import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;

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 com.github.mikephil.charting.interfaces.datasets.ILineDataSet;

import java.util.ArrayList;

public class CowDetailViewActivity extends AppCompatActivity {


        LineChart lineChart;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            lineChart = (LineChart) findViewById(R.id.lineChart);

            ArrayList<String> xAXES = new ArrayList<>();
            ArrayList<Entry> yAXESsin = new ArrayList<>();
            ArrayList<Entry> yAXEScos = new ArrayList<>();
            double x = 0 ;
            int numDataPoints = 1000;
            for(int i=0;i<numDataPoints;i++){
                float sinFunction = Float.parseFloat(String.valueOf(Math.sin(x)));
                float cosFunction = Float.parseFloat(String.valueOf(Math.cos(x)));
                x = x + 0.1;
                yAXESsin.add(new Entry(sinFunction,i));
                yAXEScos.add(new Entry(cosFunction,i));
                xAXES.add(i, String.valueOf(x));
            }
            String[] xaxes = new String[xAXES.size()];
            for(int i=0; i<xAXES.size();i++){
                xaxes[i] = xAXES.get(i).toString();
            }

            ArrayList<ILineDataSet> lineDataSets = new ArrayList<>();

            LineDataSet lineDataSet1 = new LineDataSet(yAXEScos,"cos");
            lineDataSet1.setDrawCircles(false);
            lineDataSet1.setColor(Color.BLUE);

            LineDataSet lineDataSet2 = new LineDataSet(yAXESsin,"sin");
            lineDataSet2.setDrawCircles(false);
            lineDataSet2.setColor(Color.RED);

            lineDataSets.add(lineDataSet1);
            lineDataSets.add(lineDataSet2);

            lineChart.setData(new LineData(lineDataSets));

            lineChart.setVisibleXRangeMaximum(65f);
        }
    }

CRASH LOG

09-19 08:16:39.186 32071-32076/com.example.timmy.embedivet I/art: After code cache collection, code=60KB, data=47KB
09-19 08:16:39.186 32071-32076/com.example.timmy.embedivet I/art: Increasing code cache capacity to 256KB
09-19 08:16:39.295 32071-32078/com.example.timmy.embedivet I/art: Starting a blocking GC Instrumentation
09-19 08:16:39.436 32071-32082/com.example.timmy.embedivet W/art: Suspending all threads took: 6.669ms
09-19 08:16:39.516 32071-32174/com.example.timmy.embedivet D/EGL_emulation: eglMakeCurrent: 0xa4907160: ver 2 0 (tinfo 0xa4903af0)
09-19 08:16:39.523 32071-32076/com.example.timmy.embedivet I/art: Do full code cache collection, code=72KB, data=84KB
09-19 08:16:39.523 32071-32076/com.example.timmy.embedivet I/art: After code cache collection, code=52KB, data=52KB
09-19 08:16:40.988 32071-32071/com.example.timmy.embedivet W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
09-19 08:16:50.607 32071-32071/com.example.timmy.embedivet E/MPChartLib-Utils: Utils NOT INITIALIZED. You need to call Utils.init(...) at least once before calling Utils.convertDpToPixel(...). Otherwise conversion does not take place.
09-19 08:16:50.608 32071-32071/com.example.timmy.embedivet E/MPChartLib-Utils: Utils NOT INITIALIZED. You need to call Utils.init(...) at least once before calling Utils.convertDpToPixel(...). Otherwise conversion does not take place.
09-19 08:16:52.007 32071-32071/com.example.timmy.embedivet D/AndroidRuntime: Shutting down VM
09-19 08:16:52.011 32071-32071/com.example.timmy.embedivet E/AndroidRuntime: FATAL EXCEPTION: main
                                                                             Process: com.example.timmy.embedivet, PID: 32071
                                                                             java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.timmy.embedivet/com.example.timmy.embedivet.CowDetailViewActivity}: 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:2646)
                                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                                                                                 at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                 at android.os.Looper.loop(Looper.java:154)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
                                                                              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.example.timmy.embedivet.CowDetailViewActivity.onCreate(CowDetailViewActivity.java:62)
                                                                                 at android.app.Activity.performCreate(Activity.java:6662)
                                                                                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
                                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
                                                                                 at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                 at android.os.Looper.loop(Looper.java:154) 
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:6077) 
                                                                                 at java.lang.reflect.Method.invoke(Native Method) 
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

enter image description here

2 个答案:

答案 0 :(得分:1)

哇,我明白了。我正在将内容视图设置为activity_main.xml,它确实存在于我的项目中,但不是包含图表的布局。注意资源文件。

答案 1 :(得分:0)

您的代码在

行有错误
lineChart.setData(new LineData(lineDataSets));

不正确。因为LineData中没有受支持的构造函数。它必须是:

lineChart.setData(new LineData(xaxes,lineDataSets));

支持的构造函数是:

LineData() 
LineData(List<String> xVals)
LineData(String[] xVals)
LineData(List<String> xVals, List<ILineDataSet> dataSets)
LineData(String[] xVals, List<ILineDataSet> dataSets) 
LineData(List<String> xVals, ILineDataSet dataSet)
LineData(String[] xVals, ILineDataSet dataSet) 
你的朋友应该是这样的:

repositories {
    maven { url "https://jitpack.io" }
}
dependencies {

    compile 'com.android.support:appcompat-v7:26.+'
    testCompile 'junit:junit:4.12' 
    compile 'com.github.PhilJay:MPAndroidChart:v2.2.4'
}