自定义片段中的Android饼图

时间:2015-01-07 07:40:24

标签: java android xml

我想在片段对话框中显示饼图..

这是代码: -

MainActivity.java

public class MainActivity extends Activity implements
        MyDialogFragment.Communicator {

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

    }

    @SuppressLint("NewApi")
    public void showDialog(View view) {

        FragmentManager manager = getFragmentManager();
        MyDialogFragment1 mydialog = new MyDialogFragment1();
        mydialog.show(manager, "mydialog");

    }

    @Override
    public void message(String data) {

        Toast.makeText(getApplicationContext(), data + " button clicked",
                Toast.LENGTH_SHORT).show();

    }

}

MyDialogFragment1.java

@SuppressLint({ "NewApi", "InflateParams" })
public class MyDialogFragment1 extends DialogFragment implements OnClickListener {

    Button no_button;

    Context context;
    private static int[] COLORS = new int[] { Color.MAGENTA, Color.CYAN };  


    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 };



    Communicator communicator;

    @SuppressLint("NewApi")
    @Override
    public void onAttach(Activity activity) {

        super.onAttach(activity);

           if (activity instanceof Communicator) {
            communicator = (Communicator) getActivity();
          } else {
            throw new ClassCastException(activity.toString()
                    + " must implemenet MyListFragment.communicator");
        }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        setCancelable(false);
        getDialog().setTitle("Title");

       // View view = inflater.inflate(R.layout.main, null, false);

        View view = (LinearLayout) inflater.inflate(R.layout.main,container, false);




        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();  
        }  


        //yes_button = (Button) view.findViewById(R.id.yesbtn);
        no_button = (Button) view.findViewById(R.id.nobtn);

        // setting onclick listener for buttons
     //   yes_button.setOnClickListener(this);
        no_button.setOnClickListener(this);

        return view;
    }
    @SuppressLint("ShowToast")
    @Override  
    public void onResume() {  
    super.onResume();  
    if (mChartView == null) {  
    LinearLayout layout = (LinearLayout) findViewById(R.id.chart);  
    mChartView = ChartFactory.getPieChartView(context, mSeries, mRenderer ) ;
    mRenderer.setClickEnabled(true);  
    mRenderer.setSelectableBuffer(10);  

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


    }  
    });  

    mChartView.setOnLongClickListener(new View.OnLongClickListener() {  
    @SuppressLint("ShowToast")
    @Override  
    public boolean onLongClick(View v) {  
    SeriesSelection seriesSelection = mChartView.getCurrentSeriesAndPoint();
    return false;  

    }  
    });  
    layout.addView(mChartView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));  
    }  
    else {  
    mChartView.repaint();  
    }  
    } 
    private LinearLayout findViewById(int chart) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {

        case R.id.nobtn :
            dismiss();
            communicator.message("Dialog No btn clicked");
            break;
        }

    }

    public interface Communicator {
        public void message(String data);
    }

}

和xml文件是: - 的 activity_main.xml中

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="20dp" >

    <Button
        android:id="@+id/button1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="showDialog"
        android:text="Show Dialog" />

</LinearLayout>

main.xml 是: -

   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
        android:layout_width="fill_parent"  
        android:layout_height="fill_parent"  
        android:orientation="vertical" >  

        <LinearLayout  
            android:id="@+id/chart"  
            android:layout_width="fill_parent"  
            android:layout_height="wrap_content"  
            android:layout_weight="1"  
            android:orientation="horizontal" >  
        </LinearLayout>  

         <Button
        android:id="@+id/nobtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/yesbtn"
        android:layout_alignBottom="@+id/yesbtn"
        android:layout_alignRight="@+id/textView1"
        android:layout_marginRight="38dp"
        android:text="No" />

    </LinearLayout>  

现在,当我运行这个程序时,它会显示按钮,当我点击按钮时它会停止... 并显示以下错误: -

FATAL EXCEPTION: main
        Process: com.emple.dialog_android_example, PID: 21181
        java.lang.ClassCastException: com.emple.dialog_android_example.MainActivity@41ef95b0 must implemenet MyListFragment.communicator
    at com.emple.dialog_android_example.MyDialogFragment1.onAttach(MyDialogFragment1.java:64)
        at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:849)
        at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
        at android.app.BackStackRecord.run(BackStackRecord.java:698)
        at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
    at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
    at android.os.Handler.handleCallback(Handler.java:808)
        at android.os.Handler.dispatchMessage(Handler.java:103)
    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)

问题在哪里????

1 个答案:

答案 0 :(得分:0)

我认为您的活动不是Communicator

的实例

点击此处:

@SuppressLint("NewApi")
@Override
public void onAttach(Activity activity) {

    super.onAttach(activity);

       if (activity instanceof Communicator) {
        communicator = (Communicator) getActivity();
      } else {
        throw new ClassCastException(activity.toString()
                + " must implemenet MyListFragment.communicator");
    }

}