绘图应用程序仅在2分钟后停止

时间:2019-03-11 14:23:44

标签: android multithreading android-graphview

我有一个蓝牙应用程序,可以绘制来自加速度计的数据,如果工作正常,但是在运行约2分钟后,出现以下错误,应用程序关闭。我正在使用graphview绘制数据和选项卡的图形以分别显示它们。下面显示的应用程序代码来自制图活动。

编辑:我知道什么是空指针异常。我不知道的是它的来源。这就是我需要的帮助,因为我认为这不是我写的。

2019-03-11 10:01:07.385 15431-15431/com.example.tabbedgraph I/Choreographer: Skipped 76 frames!  The application may be doing too much work on its main thread.
2019-03-11 10:01:07.386 15431-17408/com.example.tabbedgraph D/X Series Try: Success
2019-03-11 10:01:07.469 15431-17409/com.example.tabbedgraph D/Data mani. X: DataVal: X: -0.04
2019-03-11 10:01:07.469 15431-17409/com.example.tabbedgraph D/Data mani. Y: DataVal: Y: -2.82
2019-03-11 10:01:07.469 15431-17409/com.example.tabbedgraph D/Data mani. Z: DataVal: Z: 7.22 
2019-03-11 10:01:07.475 15431-17409/com.example.tabbedgraph D/Y Series Try: Success
2019-03-11 10:01:07.480 15431-15431/com.example.tabbedgraph D/AndroidRuntime: Shutting down VM
2019-03-11 10:01:07.489 15431-15431/com.example.tabbedgraph E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.tabbedgraph, PID: 15431
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference
    at com.jjoe64.graphview.GridLabelRenderer.drawVerticalSteps(GridLabelRenderer.java:1348)
    at com.jjoe64.graphview.GridLabelRenderer.draw(GridLabelRenderer.java:1117)
    at com.jjoe64.graphview.GraphView.drawGraphElements(GraphView.java:307)
    at com.jjoe64.graphview.GridLabelRenderer.draw(GridLabelRenderer.java:1108)
    at com.jjoe64.graphview.GraphView.drawGraphElements(GraphView.java:307)
    at com.jjoe64.graphview.GraphView.onDraw(GraphView.java:336)
    at android.view.View.draw(View.java:20370)
    at android.view.View.updateDisplayListIfDirty(View.java:19315)
    at android.view.View.draw(View.java:20093)
    at android.view.ViewGroup.drawChild(ViewGroup.java:4421)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4207)
    at android.view.View.updateDisplayListIfDirty(View.java:19306)
    at android.view.View.draw(View.java:20093)
    at android.view.ViewGroup.drawChild(ViewGroup.java:4421)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4207)
    at android.view.View.updateDisplayListIfDirty(View.java:19306)
    at android.view.View.draw(View.java:20093)
    at android.view.ViewGroup.drawChild(ViewGroup.java:4421)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4207)
    at android.view.View.updateDisplayListIfDirty(View.java:19306)
    at android.view.View.draw(View.java:20093)
    at android.view.ViewGroup.drawChild(ViewGroup.java:4421)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4207)
    at android.view.View.updateDisplayListIfDirty(View.java:19306)
    at android.view.View.draw(View.java:20093)
    at android.view.ViewGroup.drawChild(ViewGroup.java:4421)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4207)
    at android.view.View.updateDisplayListIfDirty(View.java:19306)
    at android.view.View.draw(View.java:20093)
    at android.view.ViewGroup.drawChild(ViewGroup.java:4421)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4207)
    at android.support.constraint.ConstraintLayout.dispatchDraw(ConstraintLayout.java:2023)
    at android.view.View.updateDisplayListIfDirty(View.java:19306)
    at android.view.View.draw(View.java:20093)
    at android.view.ViewGroup.drawChild(ViewGroup.java:4421)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4207)
    at android.view.View.updateDisplayListIfDirty(View.java:19306)
    at android.view.View.draw(View.java:20093)
    at android.view.ViewGroup.drawChild(ViewGroup.java:4421)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4207)
    at android.view.View.updateDisplayListIfDirty(View.java:19306)
    at android.view.View.draw(View.java:20093)
    at android.view.ViewGroup.drawChild(ViewGroup.java:4421)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4207)
    at android.view.View.draw(View.java:20373)
    at com.android.internal.policy.DecorView.draw(DecorView.java:980)
    at android.view.View.updateDisplayListIfDirty(View.java:19315)
    at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:686)
    at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:692)
    at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:800)
    at android.view.ViewRootImpl.draw(ViewRootImpl.java:3496)
    at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:3283)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2818)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1780)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7827)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
    at android.view.Choreographer.doCallbacks(Choreographer.java:723)
    at android.view.Choreographer.doFrame(Choreographer.java:658)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
    at android.os.Handler.handleCallback(Handler.java:789)
    at android.os.Handler.dispatchMessage(Handler.java:98)
    at android.os.Looper.loop(Looper.java:164)
2019-03-11 10:01:07.490 15431-15431/com.example.tabbedgraph E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:6944)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
2019-03-11 10:01:07.562 15431-15473/com.example.tabbedgraph I/onCharacteristicChanged: Entered
2019-03-11 10:01:07.563 15431-15473/com.example.tabbedgraph I/Message: Z: 7.26  
2019-03-11 10:01:07.563 15431-15473/com.example.tabbedgraph D/Bluetooth Device: Data sent to show data
2019-03-11 10:01:07.563 15431-15473/com.example.tabbedgraph I/onCharacteristicChanged: Bye

fgsfgsfgafgafg

public class ShowData extends Activity {
private String SHOWDATA = "Graph Activity";


TextView Xval;
TextView Yval;
TextView Zval;

TabHost host;
int X_DATA_TAB = 0;
int Y_DATA_TAB = 1;
int Z_DATA_TAB = 2;

final LineGraphSeries<DataPoint> xSeries = new LineGraphSeries<>();
final LineGraphSeries<DataPoint> ySeries = new LineGraphSeries<>();
final LineGraphSeries<DataPoint> zSeries = new LineGraphSeries<>();

//    LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
LocalBroadcastManager lbm;

int whereX, whereY, whereZ;

GraphView graph1, graph2, graph3;

Handler graphHandle = new Handler();

long start = System.currentTimeMillis();

//    LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
IntentFilter incomingData = new IntentFilter("Data Reception");

ArrayList pointsArray = new ArrayList();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_show_data);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
    lbm = LocalBroadcastManager.getInstance(this);

    Log.i(SHOWDATA, "onCreate entered");

    Xval = findViewById(R.id.valueX);
    Xval.setTextColor(Color.RED);

    Yval = findViewById(R.id.valueY);
    Yval.setTextColor(Color.GREEN);

    Zval = findViewById(R.id.valueZ);
    Zval.setTextColor(Color.BLUE);

    graph1 = (GraphView) findViewById(R.id.graphX);
    graph1.getGridLabelRenderer().setHorizontalAxisTitle("time (s)");
    graph1.getGridLabelRenderer().setVerticalAxisTitle("Accel. (m*s^-2)");

    graph2 = (GraphView) findViewById(R.id.graphY);
    graph2.getGridLabelRenderer().setHorizontalAxisTitle("time (s)");
    graph2.getGridLabelRenderer().setVerticalAxisTitle("Accel. (m*s^-2)");

    graph3 = (GraphView) findViewById(R.id.graphZ);
    graph3.getGridLabelRenderer().setHorizontalAxisTitle("time (s)");
    graph3.getGridLabelRenderer().setVerticalAxisTitle("Accel. (m*s^-2)");

    //LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);


    xSeries.setColor(Color.RED);
    ySeries.setColor(Color.GREEN);
    zSeries.setColor(Color.BLUE);

    Viewport vp1 = graph1.getViewport();
    vp1.setXAxisBoundsManual(true);
    vp1.setMinX(0);
    vp1.setMaxX(10);

    Viewport vp2 = graph2.getViewport();
    vp2.setXAxisBoundsManual(true);
    vp2.setMinX(0);
    vp2.setMaxX(10);

    Viewport vp3 = graph3.getViewport();
    vp3.setXAxisBoundsManual(true);
    vp3.setMinX(0);
    vp3.setMaxX(10);

//        lbm.registerReceiver(xyzData, incomingData);

    host = findViewById(R.id.tabhost);

    LocalActivityManager mLocalActivityManager = new LocalActivityManager(ShowData.this, false);
    mLocalActivityManager.dispatchCreate(savedInstanceState);
    host.setup(mLocalActivityManager);


    TabHost.TabSpec spec = host.newTabSpec("X tab");
    spec.setContent(R.id.Xgraph);
    spec.setIndicator("X tab");
    host.addTab(spec);

    spec = host.newTabSpec("Y tab");
    spec.setContent(R.id.Ygraph);
    spec.setIndicator("Y tab");
    host.addTab(spec);

    spec = host.newTabSpec("Z tab");
    spec.setContent(R.id.tab3);
    spec.setIndicator("Z tab");
    host.addTab(spec);

    host.setCurrentTab(0);

}

public BroadcastReceiver xyzData = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, final Intent intent) {
        //Do the things

        final String Xdata = intent.getStringExtra("X data");
        final String Ydata = intent.getStringExtra("Y data");
        final String Zdata = intent.getStringExtra("Z data");


        Xval.setText(Xdata);
        Yval.setText(Ydata);
        Zval.setText(Zdata);

        Thread graphRelated = new Thread(new Runnable() {
            @Override
            public void run() {

                String Xdata2 = null;
                String Ydata2 = null;
                String Zdata2 = null;

                try {
                    if (Xdata.contains("X")) {
                        whereX = Xdata.indexOf("X");
                        try {
                            Xdata2 = Xdata.substring(whereX + 3, whereX + 8);
                            Log.d("Data mani. X", "DataVal: " + Xdata);
                        } catch (Exception e) {
                            Log.d("X at end", "" + whereX);
                        }

                    }

                } catch(Exception e){
                    e.printStackTrace();
                }

                try {
                    if (Ydata.contains("Y")) {
                        whereY = Ydata.indexOf("Y");
                        try {
                            Ydata2 = Ydata.substring(whereY + 3, whereY + 8);
                            Log.d("Data mani. Y", "DataVal: " + Ydata);
                        } catch (Exception e) {
                            Log.d("Y at end", "" + whereY);
                        }

                    }

                } catch(Exception e){
                    e.printStackTrace();
                }

                try {
                    if (Zdata.contains("Z")) {
                        whereZ = Zdata.indexOf("Z");
                        try {
                            Zdata2 = Zdata.substring(whereZ + 3, whereZ + 8);
                            Log.d("Data mani. Z", "DataVal: " + Zdata);
                        } catch (Exception e) {
                            Log.d("Z at end", "" + whereZ);
                        }

                    }

                } catch(Exception e){
                    e.printStackTrace();
                }

                if (host.getCurrentTab() == X_DATA_TAB){
                    try{
                        addEntry(Xdata2, xSeries);
                        graph1.addSeries(xSeries);
                        Log.d("X Series Try", "Success");
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }

                if (host.getCurrentTab() == Y_DATA_TAB){
                    try{
                        addEntry(Ydata2, ySeries);
                        graph2.addSeries(ySeries);
                        Log.d("Y Series Try", "Success");
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }

                if (host.getCurrentTab() == Z_DATA_TAB){
                    try{
                        addEntry(Zdata2, zSeries);
                        graph3.addSeries(zSeries);
                        Log.d("Z Series Try", "Success");
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                }
            }
        });

        graphRelated.start();
 }
};

public void addEntry(final String data, final LineGraphSeries Series){
    // does there need to be a runnable in a function or can we just do one?
    graphHandle.postDelayed(new Runnable() {
        @Override
        public void run() {
            float dataVal = 0;
            DataPoint point;
            float current = (float) ((System.currentTimeMillis() - start)/1000.00);
            Log.d("Time: ", "" + current);
            try {

                dataVal = Float.parseFloat(data);
                point = new DataPoint(current ,dataVal);
                Series.appendData(point, true, 50); // Data point, scroll to end, max data points
                //lastX = lastX + 1;
                Log.i("Time", "" + current);
                Log.d("Add Entry", "" + Series.toString());
                Log.d("dataVal1", "" + dataVal);
                Log.i("Datapoint1", "Point " + point.toString());

                point = null;

            } catch (Exception e) {
                e.printStackTrace();
                Log.d("STR -> INT", "int dataVal contained string");
                Log.d("dataVal2", "" + dataVal);

                point = null;
            }


        }
    }, 200);

}

@Override
public void onPause(){
    super.onPause();

    lbm.unregisterReceiver(xyzData);
}

@Override
public void onResume(){
    super.onResume();

    //LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
    lbm.registerReceiver(xyzData, incomingData);
}
}

0 个答案:

没有答案