我是Android开发的菜鸟,我试图在对话框中显示饼图。但是,我无法显示饼图。我使用了XYChartBuilder.java作为我的指南,正如这个问题here所建议的那样。我使用了achart演示xychart.xml来测试我的布局设计,它与演示完美配合。但是当我尝试在我的应用程序中使用相同的xml配置时,我得到一个nullpointerexception,说android.graphics.Canvas.throwIfRecycled。我做的唯一更改是在通过layoutinflater打开对话框时实例化ChartView,而不是在演示中的onResume期间。当我将图表linearlayout从定义的200dp高度更改为“wrap_content”时,不会抛出异常。我已经包含了活动android:name =“org.achartengine.GraphicalActivity”/>在我的清单中,这不是问题。问题必须与对话框有关。非常感谢任何帮助。
MY XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<com.activity.GSCC.SegmentedRadioGroup
android:id="@+id/segment_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dip"
android:checkedButton="@+id/button_one"
android:orientation="horizontal" >
<RadioButton
android:id="@id/button_one"
android:layout_weight="0.25"
android:button="@null"
android:gravity="center"
android:minHeight="33dip"
android:minWidth="40dip"
android:text="Ledger"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/radio_colors" />
<RadioButton
android:id="@+id/button_two"
android:layout_weight="0.25"
android:button="@null"
android:gravity="center"
android:minHeight="33dip"
android:minWidth="40dip"
android:text="Chart"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="@color/radio_colors" />
</com.activity.GSCC.SegmentedRadioGroup>
</LinearLayout>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TableLayout
android:id="@+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="105dp" >
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="Coin"
android:textColor="#FFFFFF"
android:textSize="12dp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="Qty."
android:textColor="#FFFFFF"
android:textSize="12dp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="Oz."
android:textColor="#FFFFFF"
android:textSize="12dp" />
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="Value"
android:textColor="#FFFFFF"
android:textSize="12dp" />
</TableRow>
<TableRow>
<TextView
android:id="@+id/tvcurrentcoin"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:ellipsize="none"
android:maxWidth="100dp"
android:text="CurrentCoin"
android:textColor="#FFFFFF"
android:textSize="12dp" >
</TextView>
<TextView
android:id="@+id/tvcurrentqty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="CurrentQuantity"
android:textColor="#FFFFFF"
android:textSize="12dp" />
<TextView
android:id="@+id/tvcurrentoz"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="CurrentOunces"
android:textColor="#FFFFFF"
android:textSize="12dp" >
</TextView>
<TextView
android:id="@+id/tvcurrentvalue"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="CurrentValue"
android:textColor="#FFFFFF"
android:textSize="12dp" >
</TextView>
</TableRow>
<TableRow>
<TextView
android:id="@+id/nothing2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:ellipsize="none"
android:maxWidth="100dp"
android:text=""
android:textColor="#FFFFFF"
android:textSize="12dp" >
</TextView>
<TextView
android:id="@+id/nothing"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text=""
android:textColor="#FFFFFF"
android:textSize="12dp" />
<TextView
android:id="@+id/tvTotalValue"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="TOTAL:"
android:textColor="#FFFFFF"
android:textSize="12dp" >
</TextView>
<TextView
android:id="@+id/tvportfoliovalue"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="CurrentValue"
android:textColor="#FFFFFF"
android:textSize="12dp" >
</TextView>
</TableRow>
</TableLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/add_values" />
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TableRow>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip"
android:text="@string/x" />
<EditText
android:id="@+id/xValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:enabled="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip"
android:text="@string/y" />
<EditText
android:id="@+id/yValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:enabled="false" />
<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:text="@string/add" />
</TableRow>
</TableLayout>
<LinearLayout
android:id="@+id/chart"
android:layout_width="fill_parent"
android:layout_height="200dp" //Doesn't throw exception when set to "wrap content"
android:layout_weight="1"
android:orientation="horizontal" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/new_series"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/new_series" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Spinner
android:id="@+id/spinneradd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.77" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Spinner
android:id="@+id/spinnermodify"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.70" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Spinner
android:id="@+id/spinnerdelete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.77" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Spinner
android:id="@+id/spinnerchange"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.40" />
</LinearLayout>
<Button
android:id="@+id/bSQLdeleteall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Delete All Entries" />
</LinearLayout>
</ScrollView>
我的代码
LayoutInflater layoutInflater = getLayoutInflater();
View view=layoutInflater.inflate(R.layout.sqliteexample2,null);
//CHART ADDITIONS
if (mChartView == null) {
piechartlayout = (LinearLayout) view.findViewById(R.id.chart);
mChartView = ChartFactory.getLineChartView(Activity.this, mDataset, mRenderer);
mRenderer.setClickEnabled(true);
mRenderer.setSelectableBuffer(100);
mChartView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SeriesSelection seriesSelection = mChartView.getCurrentSeriesAndPoint();
double[] xy = mChartView.toRealPoint(0);
if (seriesSelection == null) {
Toast.makeText(Activity.this, "No chart element was clicked", Toast.LENGTH_SHORT)
.show();
} else {
Toast.makeText(Activity.this,
"Chart element in series index " + seriesSelection.getSeriesIndex()
+ " data point index " + seriesSelection.getPointIndex() + " was clicked"
+ " closest point value X=" + seriesSelection.getXValue() + ", Y=" + seriesSelection.getValue()
+ " clicked point value X=" + (float) xy[0] + ", Y=" + (float) xy[1], Toast.LENGTH_SHORT).show();
}
}
});
mChartView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
SeriesSelection seriesSelection = mChartView.getCurrentSeriesAndPoint();
if (seriesSelection == null) {
Toast.makeText(Activity.this, "No chart element was long pressed",
Toast.LENGTH_SHORT);
return false; // no chart element was long pressed, so let something
// else handle the event
} else {
Toast.makeText(Activity.this, "Chart element in series index "
+ seriesSelection.getSeriesIndex() + " data point index "
+ seriesSelection.getPointIndex() + " was long pressed", Toast.LENGTH_SHORT);
return true; // the element was long pressed - the event has been
// handled
}
}
});
mChartView.addZoomListener(new ZoomListener() {
public void zoomApplied(ZoomEvent e) {
String type = "out";
if (e.isZoomIn()) {
type = "in";
}
System.out.println("Zoom " + type + " rate " + e.getZoomRate());
}
public void zoomReset() {
System.out.println("Reset");
}
}, true, true);
mChartView.addPanListener(new PanListener() {
public void panApplied() {
System.out.println("New X range=[" + mRenderer.getXAxisMin() + ", " + mRenderer.getXAxisMax()
+ "], Y range=[" + mRenderer.getYAxisMax() + ", " + mRenderer.getYAxisMax() + "]");
}
});
piechartlayout.addView(mChartView, new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
boolean enabled = mDataset.getSeriesCount() > 0;
setSeriesEnabled(enabled);
} else {
mChartView.repaint();
}
//////CHART ADDITIONS
mX = (EditText) view.findViewById(R.id.xValue);
mX.setEnabled(true);
mY = (EditText) view.findViewById(R.id.yValue);
mY.setEnabled(true);
mRenderer.setApplyBackgroundColor(true);
mRenderer.setBackgroundColor(Color.argb(100, 50, 50, 50));
mRenderer.setAxisTitleTextSize(16);
mRenderer.setChartTitleTextSize(20);
mRenderer.setLabelsTextSize(15);
mRenderer.setLegendTextSize(15);
mRenderer.setMargins(new int[] { 20, 30, 15, 0 });
mRenderer.setZoomButtonsVisible(true);
mRenderer.setPointSize(10);
mRenderer.setInScroll(true);
mAdd = (Button) view.findViewById(R.id.add);
mAdd.setEnabled(true);
mNewSeries = (Button) view.findViewById(R.id.new_series);
mNewSeries.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String seriesTitle = "Series " + (mDataset.getSeriesCount() + 1);
XYSeries series = new XYSeries(seriesTitle);
mDataset.addSeries(series);
mCurrentSeries = series;
XYSeriesRenderer renderer = new XYSeriesRenderer();
mRenderer.addSeriesRenderer(renderer);
renderer.setPointStyle(PointStyle.CIRCLE);
renderer.setFillPoints(true);
mCurrentRenderer = renderer;
setSeriesEnabled(true);
}
});
mAdd.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
double x = 0;
double y = 0;
try {
x = Double.parseDouble(mX.getText().toString());
} catch (NumberFormatException e) {
// TODO
mX.requestFocus();
return;
}
try {
y = Double.parseDouble(mY.getText().toString());
} catch (NumberFormatException e) {
// TODO
mY.requestFocus();
return;
}
mCurrentSeries.add(12, 0);
mX.setText("");
mY.setText("");
mX.requestFocus();
if (mChartView != null) {
mChartView.repaint();
}
Bitmap bitmap = mChartView.toBitmap();
try {
File file = new File(Environment.getExternalStorageDirectory(), "test" + index++ + ".png");
FileOutputStream output = new FileOutputStream(file);
bitmap.compress(CompressFormat.PNG, 100, output);
} catch (Exception e) {
e.printStackTrace();
}
}
});
//END CHART ADDITIONS
FULL STACK TRACE(当我给包含图表的布局定义高度为100dp时)
11-14 20:28:13.430: E/AndroidRuntime(26620): java.lang.NullPointerException
11-14 20:28:13.430: E/AndroidRuntime(26620): at android.graphics.Canvas.throwIfRecycled(Canvas.java:954)
11-14 20:28:13.430: E/AndroidRuntime(26620): at android.graphics.Canvas.drawBitmap(Canvas.java:980)
11-14 20:28:13.430: E/AndroidRuntime(26620): at org.achartengine.GraphicalView.onDraw(GraphicalView.java:174)
11-14 20:28:13.430: E/AndroidRuntime(26620): at android.view.View.draw(View.java:6880)
11-14 20:28:13.430: E/AndroidRuntime(26620): at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
11-14 20:28:13.430: E/AndroidRuntime(26620): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
11-14 20:28:13.430: E/AndroidRuntime(26620): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
11-14 20:28:13.430: E/AndroidRuntime(26620): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
11-14 20:28:13.430: E/AndroidRuntime(26620): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
11-14 20:28:13.430: E/AndroidRuntime(26620): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
11-14 20:28:13.430: E/AndroidRuntime(26620): at android.view.View.draw(View.java:6986)
11-14 20:28:13.430: E/AndroidRuntime(26620): at android.widget.FrameLayout.draw(FrameLayout.java:357)
11-14 20:28:13.430: E/AndroidRuntime(26620): at android.widget.ScrollView.draw(ScrollView.java:1409)
11-14 20:28:13.430: E/AndroidRuntime(26620): at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
11-14 20:28:13.430: E/AndroidRuntime(26620): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
11-14 20:28:13.430: E/AndroidRuntime(26620): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
11-14 20:28:13.430: E/AndroidRuntime(26620): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
11-14 20:28:13.430: E/AndroidRuntime(26620): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
11-14 20:28:13.430: E/AndroidRuntime(26620): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
11-14 20:28:13.430: E/AndroidRuntime(26620): at android.view.View.draw(View.java:6883)
11-14 20:28:13.430: E/AndroidRuntime(26620): at android.widget.FrameLayout.draw(FrameLayout.java:357)
11-14 20:28:13.430: E/AndroidRuntime(26620): at android.view.ViewGroup.drawChild(ViewGroup.java:1646)
11-14 20:28:13.430: E/AndroidRuntime(26620): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
11-14 20:28:13.430: E/AndroidRuntime(26620): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
11-14 20:28:13.430: E/AndroidRuntime(26620): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
11-14 20:28:13.430: E/AndroidRuntime(26620): at android.view.ViewGroup.drawChild(ViewGroup.java:1644)
11-14 20:28:13.430: E/AndroidRuntime(26620): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1373)
11-14 20:28:13.430: E/AndroidRuntime(26620): at android.view.View.draw(View.java:6883)
11-14 20:28:13.430: E/AndroidRuntime(26620): at android.widget.FrameLayout.draw(FrameLayout.java:357)
11-14 20:28:13.430: E/AndroidRuntime(26620): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2116)
11-14 20:28:13.430: E/AndroidRuntime(26620): at android.view.ViewRoot.draw(ViewRoot.java:1565)
11-14 20:28:13.430: E/AndroidRuntime(26620): at android.view.ViewRoot.performTraversals(ViewRoot.java:1301)
11-14 20:28:13.430: E/AndroidRuntime(26620): at android.view.ViewRoot.handleMessage(ViewRoot.java:1914)
11-14 20:28:13.430: E/AndroidRuntime(26620): at android.os.Handler.dispatchMessage(Handler.java:99)
11-14 20:28:13.430: E/AndroidRuntime(26620): at android.os.Looper.loop(Looper.java:130)
11-14 20:28:13.430: E/AndroidRuntime(26620): at android.app.ActivityThread.main(ActivityThread.java:3859)
11-14 20:28:13.430: E/AndroidRuntime(26620): at java.lang.reflect.Method.invokeNative(Native Method)
11-14 20:28:13.430: E/AndroidRuntime(26620): at java.lang.reflect.Method.invoke(Method.java:507)
11-14 20:28:13.430: E/AndroidRuntime(26620): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-14 20:28:13.430: E/AndroidRuntime(26620): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-14 20:28:13.430: E/AndroidRuntime(26620): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
之前我遇到过类似的问题。在对话框中尝试使用缩放来进行achartengine是不可能的。尝试注释掉setzoomvisible,它应该适合你。
答案 1 :(得分:0)
我建议您先下载整个AChartEngine演示程序并按原样运行。然后,您可以开始逐步修改它,直到您获得所需的版本。