问题
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();
}
}
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>
but now Chart Size is very Small i dont no why ? how can we set chart size bigger . Thanks in advance
回答1:
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.
回答2:
Problem seems coming from menu. Can you post code in the main activity relative to menu ? and also content of res/menu ?
来源:https://stackoverflow.com/questions/32919683/classcast-expcetion-in-bar-char-using-mpandroidchart-jar