应用程序在从文件中读取数据并在Android中绘制图形时冻结

时间:2016-12-12 05:13:44

标签: java android android-studio android-asynctask android-graphview

我使用的是Windows 7,64位和Android Studio。

我正在完成两项任务 -

  1. 从doInBackground中的AsyncTaskRunner中的文件读取数据。然后我发布进度并传递数据。

  2. 正在进行更新,我正在阅读数据并将其绘制到图表中。

  3. 我正在使用GraphView绘制图表。

    问题 - 我的应用程序在几秒钟后冻结,“应用程序没有响应”显示。

    我是Android编程的新手,所以请指导我。

    这是代码 -

    package com.example.user.ui_layout;
    
    //import java.util.Random;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.BufferedReader;
    
    import com.jjoe64.graphview.GraphView;
    import com.jjoe64.graphview.Viewport;
    import com.jjoe64.graphview.series.DataPoint;
    import com.jjoe64.graphview.series.LineGraphSeries;
    import com.jjoe64.graphview.GridLabelRenderer;
    
    import android.os.AsyncTask;
    import android.graphics.Color;
    import android.os.Environment;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    
    public class MainActivity extends AppCompatActivity {
        //private static final Random RANDOM = new Random();
        private LineGraphSeries<DataPoint> series;
        private int lastX = 0;
    
        String line;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
    
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            GraphView graphView = (GraphView) findViewById(R.id.graph);
            GridLabelRenderer gridLabelRenderer = graphView.getGridLabelRenderer();
    
            series = new LineGraphSeries<DataPoint>();
            series.setColor(Color.RED);
            graphView.addSeries(series);
    
            Viewport viewport = graphView.getViewport();
            viewport.setScrollable(false);
            viewport.setScalable(true);
    
            viewport.setYAxisBoundsManual(true);
            viewport.setMinY(-50);
            viewport.setMaxY(300);
    
            viewport.setXAxisBoundsManual(true);
            viewport.setMinX(0);
            viewport.setMaxX(200);
    
    
            new AsyncTaskRunner().execute("");
        }
    
    
        private class AsyncTaskRunner extends AsyncTask<String, Integer, String>
        {
            @Override
            protected String doInBackground(String... params) {
                try {
                    Thread.sleep(3000);                 //1000 milliseconds is one second.
                } catch (InterruptedException ex) {
                    Thread.currentThread().interrupt();
                }
                File sdcard = Environment.getExternalStorageDirectory();
                File file = new File(sdcard, "demo.txt");
                try {
                    BufferedReader br = new BufferedReader(new FileReader(file));
                    while ((line = br.readLine()) != null)
                    {
                        int y = Integer.parseInt(line);
                            publishProgress(y);
    
    
                    }
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return line;
            }
    
            @Override
            protected void onPostExecute(String result)
            {
    
            }
    
            @Override
            protected void onPreExecute()
            {
    
            }
    
            @Override
            protected void onProgressUpdate(Integer... s)
            {
    
                series.appendData(new DataPoint(lastX++, s[0]), true, 1800);
    
    
            }
    
        }
    }
    

0 个答案:

没有答案