androidandroid-alertdialogandroid-graphview

GraphView within a dialog not working - error in logcat


When I place the below codes (while removing GraphDialog in front of the findViewById) in my MainActivity.java, it works perfectly.

But when I place it inside my dialog "GraphDialog", logcat error pointed to this line (see comment lines)

    //Graph View function
    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public void ShowGraphview(View v) {
    GraphView graphResult = GraphDialog.findViewById(R.id.graph);
    GraphDialog.setContentView(R.layout.ivb_popup_graphview);
    btn_close = GraphDialog.findViewById(R.id.dia_btn_close);
    GraphDialog.setCanceledOnTouchOutside(true);

    //Apply Graph view
    LineGraphSeries<DataPoint> series = new LineGraphSeries<>(new DataPoint[]{
            new DataPoint(0, 6),
            new DataPoint(1, 5),
            new DataPoint(2, 3),
            new DataPoint(3, 2),
            new DataPoint(4, 6)
    });

 //------------This line is where the logcat error pointed to-------
    graphResult.addSeries(series);
 //------------logcat error pointed to------------------------------

    /*Close Button */
    btn_close.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            GraphDialog.dismiss();
        }
    });
    GraphDialog.show();
}

Solution

  • Works when I swap these 2 lines....

    GraphView graphResult = GraphDialog.findViewById(R.id.graph);
    
    GraphDialog.setContentView(R.layout.ivb_popup_graphview);