0

I am using AChartEngine to make charts which seems to be working quite well so far. The only thing to make this perfect would be to be able to put this chart on a popup. Unfortunately I have no idea how to do this. Whenever I try to include the chart's function to be called with the popup I get a NullPointerException. Here is the code for my popup window (which is called whenever I click on a button)

void popupWindow(){
     LayoutInflater layoutInflater 
     = (LayoutInflater)getBaseContext()
      .getSystemService(LAYOUT_INFLATER_SERVICE);  
    View popupView = layoutInflater.inflate(R.layout.activity_report_popup_layout, null);  
             final PopupWindow popupWindow = new PopupWindow(
               popupView, 
               LayoutParams.WRAP_CONTENT,  
                     LayoutParams.WRAP_CONTENT);  
             Button btnDismiss = (Button)popupView.findViewById(R.id.dismiss);
             btnDismiss.setOnClickListener(new Button.OnClickListener(){

     @Override
     public void onClick(View v) {
      // TODO Auto-generated method stub
      popupWindow.dismiss();
     }});

             popupWindow.showAsDropDown(plotsButton, 50, -30);

   }

Here is the relevant popup layout xml code:

      <TextView
         android:id="@+id/textView1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="Popup"
         android:textAppearance="?android:attr/textAppearanceLarge" />

      <TextView
         android:id="@+id/textView2"
         android:layout_marginTop="5dp"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="This is a simple popup" />

         <Button
         android:id="@+id/dismiss"
         android:layout_marginTop="10dp"
         android:layout_gravity="center_horizontal"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="Close" />


         <LinearLayout
              android:orientation="horizontal"
              android:id="@+id/chart_container"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_below="@id/tv_title" >

          </LinearLayout>

Edit: Decided to also add the chart code:

 private void openChart(){

        Date[] dt = new Date[dates.size()];
        float[] visits = new float[dates.size()];
        for(int i=0;i<dates.size();i++){
            try {
                dt[i]= new SimpleDateFormat("yyyy-MM-dd").parse(dates.get(i));
                visits[i] = Float.parseFloat(distance.get(i));
            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        TimeSeries visitsSeries = new TimeSeries("Visits");

        for(int i=0;i<dt.length;i++){
            visitsSeries.add(dt[i], visits[i]);
        }

        XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();

        dataset.addSeries(visitsSeries);

        XYSeriesRenderer visitsRenderer = new XYSeriesRenderer();
        visitsRenderer.setColor(Color.BLACK);
        visitsRenderer.setPointStyle(PointStyle.CIRCLE);
        visitsRenderer.setFillPoints(true);
        visitsRenderer.setLineWidth(2);
        visitsRenderer.setDisplayChartValues(true);

        XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer();

        multiRenderer.addSeriesRenderer(visitsRenderer);

        LinearLayout chartContainer = (LinearLayout) findViewById(R.id.chart_container);

        mChart = (GraphicalView) ChartFactory.getTimeChartView(getBaseContext(), dataset, multiRenderer,"dd-MMM-yyyy");

        multiRenderer.setClickEnabled(true);
        multiRenderer.setSelectableBuffer(10);

            chartContainer.addView(mChart);
    }

Edit 2: Added the error logs:

 05-16 10:45:39.013: E/AndroidRuntime(6311): FATAL EXCEPTION: main
 05-16 10:45:39.013: E/AndroidRuntime(6311): Process: com.example.jfitnessfunctiontester, PID: 6311
 05-16 10:45:39.013: E/AndroidRuntime(6311): java.lang.NullPointerException
 05-16 10:45:39.013: E/AndroidRuntime(6311):    at activities.ReportListViewActivity.openChart(ReportListViewActivity.java:256)
 05-16 10:45:39.013: E/AndroidRuntime(6311):    at activities.ReportListViewActivity.popupWindow(ReportListViewActivity.java:115)
 05-16 10:45:39.013: E/AndroidRuntime(6311):    at activities.ReportListViewActivity$2.onClick(ReportListViewActivity.java:137)
 05-16 10:45:39.013: E/AndroidRuntime(6311):    at android.view.View.performClick(View.java:4438)
 05-16 10:45:39.013: E/AndroidRuntime(6311):    at android.view.View$PerformClick.run(View.java:18422)
 05-16 10:45:39.013: E/AndroidRuntime(6311):    at android.os.Handler.handleCallback(Handler.java:733)
 05-16 10:45:39.013: E/AndroidRuntime(6311):    at android.os.Handler.dispatchMessage(Handler.java:95)
 05-16 10:45:39.013: E/AndroidRuntime(6311):    at android.os.Looper.loop(Looper.java:136)
 05-16 10:45:39.013: E/AndroidRuntime(6311):    at android.app.ActivityThread.main(ActivityThread.java:5017)
 05-16 10:45:39.013: E/AndroidRuntime(6311):    at java.lang.reflect.Method.invokeNative(Native Method)
 05-16 10:45:39.013: E/AndroidRuntime(6311):    at java.lang.reflect.Method.invoke(Method.java:515)
 05-16 10:45:39.013: E/AndroidRuntime(6311):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
 05-16 10:45:39.013: E/AndroidRuntime(6311):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
 05-16 10:45:39.013: E/AndroidRuntime(6311):    at dalvik.system.NativeStart.main(Native Method)
frogatto
  • 28,539
  • 11
  • 83
  • 129
theJuls
  • 6,788
  • 14
  • 73
  • 160
  • Which line is giving you the null pointer exception? – Dawood ibn Kareem May 15 '14 at 23:39
  • It happens when I add the chart to my view. I didn't include the chart code because it is very long, but it's basically: chartContainer.addView(mChart); (this works perfectly if not trying to be put in the popup) – theJuls May 15 '14 at 23:49
  • Then what you're basically asking is why `chartContainer` is null when it shouldn't be. I can't answer this without seeing your code. But I can recommend that you step through with a debugger; just to verify that `chartContainer` is in fact null, and to find out why the code that sets it to something other than null isn't running. – Dawood ibn Kareem May 15 '14 at 23:51
  • I just added a shorter version of the chart code. – theJuls May 16 '14 at 00:02
  • Are you sure it's happening on `chartContainer.addView(mChart);`? There's nothing above this on the stack trace? – Dawood ibn Kareem May 16 '14 at 02:21
  • @DavidWallace that's where it crashes when I debug it. I added the logs so you can see the details of the exception – theJuls May 16 '14 at 13:47
  • So `chartContainer.addView(mChart);` is line 256? – Dawood ibn Kareem May 16 '14 at 14:39
  • Exactly. That's where it is. I don't know if it's relevant, but it works perfectly when it isn't in the popup. – theJuls May 16 '14 at 14:43
  • I don't know whether this is relevant or not, but I'm just looking at your layout XML again. The indentation suggests that the `Button` and the `LinearLayout` are _inside_ the second `TextView`, but that's not the case. You've actually closed the second `TextView` tag, and tricked yourself with the indentation. – Dawood ibn Kareem May 16 '14 at 19:28
  • I may have screwed up when posting the code here. I cut some irrelevant snippets out. Regardless, it turns out I just decided to make another activity for the chart. Unfortunately the popup was just too much work and I couldn't figure out the issue. It also wasn't absolutely necessary, I just thought it would look better. Thanks for your help though! – theJuls May 16 '14 at 20:17
  • OK, theJuls, I'm just sorry that I didn't manage to help you all the way through this problem. Good luck with the project. – Dawood ibn Kareem May 17 '14 at 01:01

0 Answers0