2012-08-30 67 views
0

我剛開始在Mono上爲Android使用Steema TChart。我想這個簡單的代碼,它工作正常:LinearLayout內部的TChart生成「寬度和高度必須> 0」

protected override void OnCreate(Bundle bundle) 
{ 
    base.OnCreate(bundle); 

    var tChart1 = new Steema.TeeChart.TChart(this); 
    var bar1 = new Steema.TeeChart.Styles.Bar(); 
    tChart1.Series.Add(bar1); 
    bar1.Add(3, "Pears", Color.Red); 
    bar1.Add(4, "Apples", Color.Blue); 
    bar1.Add(2, "Oranges", Color.Green); 
    var theme = new Steema.TeeChart.Themes.BlackIsBackTheme(tChart1.Chart); 
    theme.Apply(); 

    SetContentView(tChart1); 
} 

...但只要我嘗試添加圖表到另一種觀點認爲,像LinearLayout中我得到的錯誤「寬度和高度必須> 0 」。有誰知道如何解決這個問題?

這裏是我試過的代碼(它工作得很好,如果我只擁有的LinearLayout和按鈕):

protected override void OnCreate(Bundle bundle) 
{ 
    base.OnCreate(bundle); 

    var tChart1 = new Steema.TeeChart.TChart(this); 
    var bar1 = new Steema.TeeChart.Styles.Bar(); 
    tChart1.Series.Add(bar1); 
    bar1.Add(3, "Pears", Color.Red); 
    bar1.Add(4, "Apples", Color.Blue); 
    bar1.Add(2, "Oranges", Color.Green); 
    var theme = new Steema.TeeChart.Themes.BlackIsBackTheme(tChart1.Chart); 
    theme.Apply(); 

    var container = new LinearLayout(this); 
    container.Orientation = Orientation.Vertical; 
    var b = new Button(this); 
    b.Text = "Test"; 
    container.AddView(b); 
    container.AddView(tChart1); 

    SetContentView(container); 
} 

這裏是調用堆棧:

08-29 19:39:52.489 I/MonoDroid(24907): java.lang.IllegalArgumentException: width and height must be > 0 
08-29 19:39:52.489 I/MonoDroid(24907): at android.graphics.Bitmap.createBitmap(Bitmap.java:603) 
08-29 19:39:52.489 I/MonoDroid(24907): at android.graphics.Bitmap.createBitmap(Bitmap.java:585) 
08-29 19:39:52.489 I/MonoDroid(24907): at steema.teechart.TChart.n_onDraw(Native Method) 
08-29 19:39:52.489 I/MonoDroid(24907): at steema.teechart.TChart.onDraw(TChart.java:55) 
08-29 19:39:52.489 I/MonoDroid(24907): at android.view.View.draw(View.java:11025) 
08-29 19:39:52.489 I/MonoDroid(24907): at android.widget.FrameLayout.draw(FrameLayout.java:450) 
08-29 19:39:52.489 I/MonoDroid(24907): at android.view.ViewGroup.drawChild(ViewGroup.java:2887) 
08-29 19:39:52.489 I/MonoDroid(24907): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489) 
08-29 19:39:52.489 I/MonoDroid(24907): at android.view.ViewGroup.drawChild(ViewGroup.java:2885) 
08-29 19:39:52.489 I/MonoDroid(24907): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489) 
08-29 19:39:52.489 I/MonoDroid(24907): at android.view.ViewGroup.drawChild(ViewGroup.java:2885) 
08-29 19:39:52.489 I/MonoDroid(24907): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489) 
08-29 19:39:52.489 I/MonoDroid(24907): at android.view.ViewGroup.drawChild(ViewGroup.java:2885) 
08-29 19:39:52.489 I/MonoDroid(24907): at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2489) 
08-29 19:39:52.489 I/MonoDroid(24907): at android.view.View.draw(View.java:11028) 
08-29 19:39:52.489 I/MonoDroid(24907): at android.widget.FrameLayout.draw(FrameLayout.java:450) 
08-29 19:39:52.489 I/MonoDroid(24907): at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2274) 
08-29 19:39:52.489 I/MonoDroid(24907): at android.view.ViewRootImpl.draw(ViewRootImpl.java:2041) 
08-29 19:39:52.489 I/MonoDroid(24907): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1647) 
08-29 19:39:52.489 I/MonoDroid(24907): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2459) 
08-29 19:39:52.489 I/MonoDroid(24907): at android.os.Handler.dispatchMessage(Handler.java:99) 
08-29 19:39:52.489 I/MonoDroid(24907): at android.os.Looper.loop(Looper.java:137) 
08-29 19:39:52.489 I/MonoDroid(24907): at android.app.ActivityThread.main(ActivityThread.java:4514) 
08-29 19:39:52.489 I/MonoDroid(24907): at java.lang.reflect.Method.invokeNative(Native Method) 
08-29 19:39:52.489 I/MonoDroid(24907): at java.lang.reflect.Method.invoke(Method.java:511) 
08-29 19:39:52.489 I/MonoDroid(24907): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790) 
08-29 19:39:52.489 I/MonoDroid(24907): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557) 
08-29 19:39:52.489 I/MonoDroid(24907): at dalvik.system.NativeStart.main(Native Method) 

回答

1

這是因爲您需要在向您的視圖添加圖表時提供LayoutParams,例如:

container.AddView(tChart1,new ViewGroup.LayoutParams(300,400));

或:

container.AddView(tChart1,新ViewGroup.LayoutParams(ViewGroup.LayoutParams.FillParent,ViewGroup.LayoutParams.FillParent));

+0

啊......就這麼簡單。我以爲我嘗試過,但我試圖將LayoutParams發送到SetContentView而不是AddView。謝謝! –

相關問題