简体   繁体   English

如何在弹出窗口中显示Android Piechart?

[英]How to display Android piechart in popup window?

I want to display pie-chart in pop-up window..I am using the "achartengine-1.0.0" here is my code:-- 我想在弹出窗口中显示饼图。我正在使用“ achartengine-1.0.0”,这是我的代码:-

AndroidPopupWindowActivity111.java AndroidPopupWindowActivity111.java

   @SuppressLint("InflateParams")
public class AndroidPopupWindowActivity111 extends Activity {

    private static int[] COLORS = new int[] { Color.MAGENTA, Color.CYAN };  
    LinearLayout layout;
    private static String[] NAME_LIST = new String[] { "A", "B" };  
    private CategorySeries mSeries = new CategorySeries("");  
    private DefaultRenderer mRenderer = new DefaultRenderer();  
    private GraphicalView mChartView;  



    private  int[] VALUES = { 40, 60};


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.popup_main);

        mRenderer.setApplyBackgroundColor(true);
        mRenderer.setBackgroundColor(Color.argb(100, 50, 50, 50));  
        mRenderer.setChartTitleTextSize(20);  
        mRenderer.setLabelsTextSize(15);  
        mRenderer.setLegendTextSize(15);  
        mRenderer.setMargins(new int[] { 20, 30, 15, 0 });  
        mRenderer.setZoomButtonsVisible(true);  
        mRenderer.setStartAngle(90);  

        for (int i = 0; i < VALUES.length; i++) {  
        //mSeries.add(NAME_LIST[i] + " " + VALUES[i], VALUES[i]);  
            mSeries.add(NAME_LIST[i] + "(" + VALUES[i]+"%)", VALUES[i]);  
            SimpleSeriesRenderer renderer = new SimpleSeriesRenderer();  
        renderer.setColor(COLORS[(mSeries.getItemCount() - 1) % COLORS.length]);  
        mRenderer.addSeriesRenderer(renderer);  
        }  

        if (mChartView != null) {  
        mChartView.repaint();  
        }  

        final Button btnOpenPopup = (Button)findViewById(R.id.openpopup);
        btnOpenPopup.setOnClickListener(new Button.OnClickListener(){

   @Override
   public void onClick(View arg0) {
    LayoutInflater layoutInflater 
     = (LayoutInflater)getBaseContext()
      .getSystemService(LAYOUT_INFLATER_SERVICE);  
    View popupView = layoutInflater.inflate(R.layout.main_piechart, null);  
             final PopupWindow popupWindow = new PopupWindow( popupView, LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);  
             layout = (LinearLayout) findViewById(R.id.chart);  
             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(btnOpenPopup, 50, -30);

   }});
    }

    @SuppressLint("ShowToast")
    @Override  
    protected void onResume() {  
    super.onResume();  
    if (mChartView == null) {  

    mChartView = ChartFactory.getPieChartView(this, mSeries, mRenderer);  
    mRenderer.setClickEnabled(true);  
    mRenderer.setSelectableBuffer(10);  

    mChartView.setOnClickListener(new View.OnClickListener() {  
    @Override  
    public void onClick(View v) {  
    SeriesSelection seriesSelection = mChartView.getCurrentSeriesAndPoint();  

    if (seriesSelection == null) {  
    Toast.makeText(AndroidPopupWindowActivity111.this,"No chart element was clicked",Toast.LENGTH_SHORT).show();  
    } else {  
    Toast.makeText(AndroidPopupWindowActivity111.this,"Chart element data point index "+ (seriesSelection.getPointIndex()+1) + " was clicked" + " point value="+ seriesSelection.getValue(), Toast.LENGTH_SHORT).show();  
    }  
    }  
    });  

    mChartView.setOnLongClickListener(new View.OnLongClickListener() {  
    @SuppressLint("ShowToast")
    @Override  
    public boolean onLongClick(View v) {  
    SeriesSelection seriesSelection = mChartView.getCurrentSeriesAndPoint();  
    if (seriesSelection == null) {  
    Toast.makeText(AndroidPopupWindowActivity111.this,"No chart element was long pressed", Toast.LENGTH_SHORT);  
    return false;   
    } else {  
    Toast.makeText(AndroidPopupWindowActivity111.this,"Chart element data point index "+ seriesSelection.getPointIndex()+ " was long pressed",Toast.LENGTH_SHORT);  
    return true;         
    }  
    }  
    });  
    layout.addView(mChartView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));  
    }  
    else {  
    mChartView.repaint();  
    }  
    }  
}

popup_main.xml popup_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="hellooo" />
    <Button
        android:id="@+id/openpopup"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Open Popup Window" />

</LinearLayout>

main_piechart.xml main_piechart.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent"  
    android:orientation="vertical" 
    android:paddingLeft="10dp"
    android:paddingRight="10dp"  
    android:paddingTop="30dp">

    <LinearLayout
        android:id="@+id/chart"
        android:layout_width="300dp"
        android:layout_height="190dp"

        android:orientation="horizontal" >

    </LinearLayout>  

     <Button
      android:id="@+id/dismiss"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="Dismiss" />

</LinearLayout> 

When I run this program..It is showing the error:--- 当我运行该程序时..它显示错误:---

Process: in.wptrafficanalyzer.achartenginepiechart, PID: 14513
 java.lang.RuntimeException: Unable to resume activity {in.wptrafficanalyzer.achartenginepiechart/in.wptrafficanalyzer.achartenginepiechart.AndroidPopupWindowActivity111}: java.lang.NullPointerException
    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2972)
    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3001)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2403)
    at android.app.ActivityThread.access$800(ActivityThread.java:159)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1329)
    at android.os.Handler.dispatchMessage(Handler.java:110)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:5324)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
    at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
    at in.wptrafficanalyzer.achartenginepiechart.AndroidPopupWindowActivity111.onResume(AndroidPopupWindowActivity111.java:130)
    at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1197)

Where is the wrong in that program???I cant understand..thanks in advance 该程序哪里出了问题?我无法理解。

Here: 这里:

layout.addView(mChartView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); layout.addView(mChartView,新的LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));

line causing issue because layout is not initialized. 行导致问题,因为layout未初始化。 you are initializing layout on Button click but onResume is called on Activity start. 您在单击按钮时初始化layout ,但是在活动开始时调用onResume

Initialize layout in onCreate as after setContentView : setContentView之后在onCreate初始化layout

layout = (LinearLayout) findViewById(R.id.chart);  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM