achartengine Piechart从远程数据库生成

时间:2014-03-13 15:32:46

标签: android achartengine

我正在尝试生成饼图,其中数据来自phpmyadmin数据库。我已经获得了正确的数据,但问题是,它没有生成饼图,它崩溃并给我一个错误“NullPointerException”

这是我的代码:

public class Statisticslist extends Activity {

private ProgressDialog pDialog;

JSONParser jsonParser = new JSONParser();
JSONArray history = null;

String diagnosis;
int diagcount;

String[] categoryNames;
double[] values;

GraphicalView chartview = null;
public DefaultRenderer mRenderer = new DefaultRenderer();
public CategorySeries series = new CategorySeries("Diagnosis");

private static int[] colors = new int[]{
    Color.BLACK, Color.BLUE, Color.CYAN, Color.DKGRAY, Color.GRAY,
    Color.GREEN, Color.LTGRAY, Color.MAGENTA, Color.RED, Color.YELLOW
};

private static final String LIST = "http://192.168.43.15:8080/DoctorScheduler/activities/statisticslist.php";

//json node
private static final String TAG_POST        = "post";
private static final String TAG_DIAGNOSIS   = "diagnosis";
private static final String TAG_DIAGCOUNT   = "diagcount";

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

    new LoadSingleHistory().execute();
}

class LoadSingleHistory extends AsyncTask<String, String, String>{

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        pDialog = new ProgressDialog(Statisticslist.this);
        pDialog.setMessage("Loading patient information ...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    @Override
    protected String doInBackground(String... args) {
        // TODO Auto-generated method stub
        List<NameValuePair> params = new ArrayList<NameValuePair>();

        // getting JSON string from URL
        String json = jsonParser.getJSONFromURL(LIST, "GET", params);
        // Check your log cat for JSON response
        Log.d("Single History JSON: ", json);

        try {
            JSONObject jObj = new JSONObject(json);
            if(jObj != null){

                history = jObj.getJSONArray(TAG_POST);
                if(history != null){

                    int count = history.length();
                    values = new double[count];
                    categoryNames = new String[count];

                    for(int i=0; i<count; i++){
                    JSONObject c = history.getJSONObject(i);

                    values[i] = c.getDouble(TAG_DIAGCOUNT);
                    categoryNames[i] = c.getString(TAG_DIAGNOSIS);

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

        return null;
    }

    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        pDialog.dismiss();

        int i = 0;
        series.add(categoryNames[i], values[i]);
        SimpleSeriesRenderer renderer = new SimpleSeriesRenderer();
        renderer.setColor(colors[(series.getItemCount()-1) % colors.length]);
        renderer.setDisplayChartValues(true);
        mRenderer.addSeriesRenderer(renderer);
        i++;

        chartview = ChartFactory.getPieChartView(Statisticslist.this, series, mRenderer);

        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        chartview.setLayoutParams(params);
        RelativeLayout layout = (RelativeLayout) findViewById(R.id.PieCharts);
        layout.addView(chartview);
    }   
}

这是它返回的json:

{"success":1,"message":"Patient Available","post":[{"diagnosis":"Cold","diagcount":"2"}, {"diagnosis":"Cough","diagcount":"1"},{"diagnosis":"Dengue","diagcount":"1"},{"diagnosis":"Fever","diagcount":"2"},{"diagnosis":"German Measles","diagcount":"3"},{"diagnosis":"Measles","diagcount":"2"},{"diagnosis":"Tooth Ache","diagcount":"1"}]}

错误:

java.lang.NullPointerException
at com.example.doctorscheduler.Statisticslist$LoadSingleHistory.onPostExecute(Statisticslist.java:131)
at com.example.doctorscheduler.Statisticslist$LoadSingleHistory.onPostExecute(Statisticslist.java:1)

0 个答案:

没有答案