ClassCast Expcetion在酒吧焦炭使用MPAndroidChart罐 [英] ClassCast Expcetion in Bar char using MPAndroidChart jar

查看:374
本文介绍了ClassCast Expcetion在酒吧焦炭使用MPAndroidChart罐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新在这个网站其实我的工作表技术,但是当我部署此code和运行它会给类转换异常。但没有在code错误。请任何一个能帮助我。在下面找到主要活动类的main.xml

i am new in this website actually i worked on Chart technology but when i deploy this code and run it will give class cast Exception . but there is not Error on Code . please any one help me . find below Main Activity classand Main.xml.

public class MainActivity extends Activity {
private LinearLayout mainLayout;
private PieChart mChart;
// we're going to display pie chart for smartphones martket shares
private float[] yData = { 5, 10, 15, 30, 40 };
private String[] xData = { "Sony", "Huawei", "LG", "Apple", "Samsung" };

 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainLayout = (LinearLayout) findViewById(R.id.mainLayout);
mChart = new PieChart(this);
// add pie chart to main layout
mainLayout.addView(mChart);
mainLayout.setBackgroundColor(Color.parseColor("#55656C"));

// configure pie chart
mChart.setUsePercentValues(true);
mChart.setDescription("Smartphones Market Share");

// enable hole and configure
mChart.setDrawHoleEnabled(true);
mChart.setHoleColorTransparent(true);
mChart.setHoleRadius(7);
mChart.setTransparentCircleRadius(10);

// enable rotation of the chart by touch
mChart.setRotationAngle(0);
mChart.setRotationEnabled(true);

// set a chart value selected listener
mChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {

  @Override
  public void onValueSelected(Entry e, int dataSetIndex, Highlight h) {
    // display msg when value selected
    if (e == null)
      return;

    Toast.makeText(MainActivity.this,
      xData[e.getXIndex()] + " = " + e.getVal() + "%", Toast.LENGTH_SHORT).show();
  }

  @Override
  public void onNothingSelected() {

  }
});

// add data
addData();

// customize legends
Legend l = mChart.getLegend();
l.setPosition(LegendPosition.RIGHT_OF_CHART);
l.setXEntrySpace(7);
l.setYEntrySpace(5);
}

private void addData() {
ArrayList<Entry> yVals1 = new ArrayList<Entry>();

for (int i = 0; i < yData.length; i++)
  yVals1.add(new Entry(yData[i], i));

ArrayList<String> xVals = new ArrayList<String>();

for (int i = 0; i < xData.length; i++)
  xVals.add(xData[i]);

// create pie data set
PieDataSet dataSet = new PieDataSet(yVals1, "Market Share");
dataSet.setSliceSpace(3);
dataSet.setSelectionShift(5);

// add many colors
ArrayList<Integer> colors = new ArrayList<Integer>();

for (int c : ColorTemplate.VORDIPLOM_COLORS)
  colors.add(c);

for (int c : ColorTemplate.JOYFUL_COLORS)
  colors.add(c);

for (int c : ColorTemplate.COLORFUL_COLORS)
  colors.add(c);

for (int c : ColorTemplate.LIBERTY_COLORS)
  colors.add(c);

for (int c : ColorTemplate.PASTEL_COLORS)
  colors.add(c);

colors.add(ColorTemplate.getHoloBlue());
dataSet.setColors(colors);

// instantiate pie data object now
PieData data = new PieData(xVals, dataSet);
data.setValueFormatter(new PercentFormatter());
data.setValueTextSize(11f);
data.setValueTextColor(Color.GRAY);

mChart.setData(data);

// undo all highlights
mChart.highlightValues(null);

// update pie chart
mChart.invalidate();
}

}

和activity_main.xml

and activity_main.xml

 <LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:id="@+id/mainLayout">
</LinearLayout>

但是现在表尺寸非常小,我不无为什么?我们怎样才能设置图表大小大。在此先感谢

推荐答案

正在铸造的LinearLayout (XML)到 RelativeLayout的(在你的活动)。病程中你不能做到这一点,它会导致您所描述的异常。

You are casting a LinearLayout (xml) to a RelativeLayout (in your Activity). Of couse you cannot do that and it will result in the exception you described.

这篇关于ClassCast Expcetion在酒吧焦炭使用MPAndroidChart罐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆