私はAndroid開発の初心者で、ダイアログに円グラフを表示しようとしています。ただし、円グラフを表示できません。このSOの質問で提案されているように、XYChartBuilder.javaをガイドとして使用しました。。achartデモxychart.xmlを使用してレイアウトデザインをテストしましたが、デモで完全に機能しました。しかし、アプリケーションで同じxml構成を使用しようとすると、android.graphics.Canvas.throwIfRecycledというnullpointerexceptionが発生します。私が行った唯一の変更は、デモのonResume中ではなく、layoutinflaterを介してダイアログが開いたときにChartViewをインスタンス化することでした。チャート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
フルスタックトレース(チャートを含むレイアウトに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)