Android:AchartEngine体重图layout.addView引发异常

时间:2014-05-23 10:36:25

标签: android android-layout achartengine

我正在尝试使用Achart Engine lib显示wheight图表。但我在行中有一个例外:layout.addView ...

有人可以告诉我我错过了什么。因为当我尝试以新意图显示图表时,它很快就会起作用,但当我尝试在布局中显示它时会崩溃。

这是我使用的代码:

    LinearLayout layout;
    View mChartView ;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wifi);

        CategorySeries category = new CategorySeries("Weight indic");
        category.add("Current", 75);
        category.add("Minimum", 65);
        category.add("Maximum", 90);

        DialRenderer renderer = new DialRenderer();
        renderer.setChartTitleTextSize(20);
        renderer.setLabelsTextSize(15);
        renderer.setLegendTextSize(15);
        renderer.setMargins(new int[] {20, 30, 15, 0});

        SimpleSeriesRenderer r = new SimpleSeriesRenderer();
        r.setColor(Color.BLUE);

        renderer.addSeriesRenderer(r);

        r = new SimpleSeriesRenderer();
        r.setColor(Color.rgb(0, 150, 0));
        renderer.addSeriesRenderer(r);
        r = new SimpleSeriesRenderer();
        r.setColor(Color.GREEN);
        renderer.addSeriesRenderer(r);
        renderer.setLabelsTextSize(10);
        renderer.setLabelsColor(Color.WHITE);
        renderer.setShowLabels(true);
        renderer.setVisualTypes(new DialRenderer.Type[] {Type.ARROW, Type.NEEDLE, Type.NEEDLE});
        renderer.setMinValue(0);
        renderer.setMaxValue(150);

        layout = (LinearLayout) findViewById(R.id.chart);

        if (mChartView != null) {
           layout.removeView(mChartView);
        }

        mChartView = ChartFactory.getDialChartView(MyWifiNetwork.this, category, renderer);

        layout.addView(mChartView, 0, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

    }

这就是logCat:

05-23 11:25:03.332: W/dalvikvm(28280): threadid=1: thread exiting with uncaught exception (group=0x40d869a8)
05-23 11:25:03.365: E/AndroidRuntime(28280): FATAL EXCEPTION: main
05-23 11:25:03.365: E/AndroidRuntime(28280): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.debitdistance/com.example.debitdistance.MyWifiNetwork}: java.lang.NullPointerException
05-23 11:25:03.365: E/AndroidRuntime(28280):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
05-23 11:25:03.365: E/AndroidRuntime(28280):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
05-23 11:25:03.365: E/AndroidRuntime(28280):    at android.app.ActivityThread.access$600(ActivityThread.java:162)
05-23 11:25:03.365: E/AndroidRuntime(28280):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
05-23 11:25:03.365: E/AndroidRuntime(28280):    at android.os.Handler.dispatchMessage(Handler.java:107)
05-23 11:25:03.365: E/AndroidRuntime(28280):    at android.os.Looper.loop(Looper.java:194)
05-23 11:25:03.365: E/AndroidRuntime(28280):    at android.app.ActivityThread.main(ActivityThread.java:5371)
05-23 11:25:03.365: E/AndroidRuntime(28280):    at java.lang.reflect.Method.invokeNative(Native Method)
05-23 11:25:03.365: E/AndroidRuntime(28280):    at java.lang.reflect.Method.invoke(Method.java:525)
05-23 11:25:03.365: E/AndroidRuntime(28280):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
05-23 11:25:03.365: E/AndroidRuntime(28280):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
05-23 11:25:03.365: E/AndroidRuntime(28280):    at dalvik.system.NativeStart.main(Native Method)
05-23 11:25:03.365: E/AndroidRuntime(28280): Caused by: java.lang.NullPointerException
05-23 11:25:03.365: E/AndroidRuntime(28280):    at com.example.debitdistance.MyWifiNetwork.onCreate(MyWifiNetwork.java:227)
05-23 11:25:03.365: E/AndroidRuntime(28280):    at android.app.Activity.performCreate(Activity.java:5122)
05-23 11:25:03.365: E/AndroidRuntime(28280):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081)
05-23 11:25:03.365: E/AndroidRuntime(28280):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2307)
05-23 11:25:03.365: E/AndroidRuntime(28280):    ... 11 more

1 个答案:

答案 0 :(得分:2)

这是我在我的项目中使用的代码的一部分:

    // ...
    // The chart
    final GraphicalView cht = ChartFactory.getTimeChartView(ctx, ds, renderer, "MMM");

    // The chart container
    final RelativeLayout lay = (RelativeLayout) getView().findViewById(R.id.chart);
    lay.addView(cht);
    // ...

显然,我使用的是另一个容器,我在片段中(所以在findViewByID之前移除getView()。),使用timeChartView,...
因此,需要进行一些修改以使其适应您的代码。

<强> [编辑]

尝试:这些行

// ...
// The chart
final GraphicalView mChartView = ChartFactory.getDialChartView(MyWifiNetwork.this, category, renderer);

// The chart container
final LinearLayout layout = (LinearLayout) findViewById(R.id.chart);
layout.addView(mChartView);
// ...

将替换所有这些行

layout = (LinearLayout) findViewById(R.id.chart);

if (mChartView != null) {
    layout.removeView(mChartView);
}

mChartView = ChartFactory.getDialChartView(MyWifiNetwork.this, category, renderer);

layout.addView(mChartView, 0, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));

请注意,现在您需要删除这些行:

LinearLayout layout;
View mChartView;