高度和宽度必须大于0?

时间:2017-12-13 13:56:17

标签: java android

我正在尝试使用片段创建白板类型的应用程序。但是,每当称为高度的片段总是为零时,我的身高就会出现问题,但这段代码在活动中非常有效。当名为height的onSizeChanged函数始终为0.请参阅下面的代码和错误

收到此错误

FATAL EXCEPTION: main
          Process: com.example.ebmacs.vickyy.kids, PID: 5781
          java.lang.IllegalArgumentException: width and height must be > 0
              at android.graphics.Bitmap.createBitmap(Bitmap.java:829)
              at android.graphics.Bitmap.createBitmap(Bitmap.java:808)
              at android.graphics.Bitmap.createBitmap(Bitmap.java:775)
              at com.example.ebmacs.vickyy.kids.Classes.PaintView.onSizeChanged(PaintView.java:82)
              at android.view.View.sizeChange(View.java:16748)
              at android.view.View.setFrame(View.java:16710)
              at android.view.View.layout(View.java:16627)
              at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
              at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
              at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
              at android.view.View.layout(View.java:16630)
              at android.view.ViewGroup.layout(ViewGroup.java:5437)
              at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
              at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
              at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
              at android.view.View.layout(View.java:16630)
              at android.view.ViewGroup.layout(ViewGroup.java:5437)
              at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
              at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
              at android.view.View.layout(View.java:16630)
              at android.view.ViewGroup.layout(ViewGroup.java:5437)
              at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
              at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
              at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
              at android.view.View.layout(View.java:16630)
              at android.view.ViewGroup.layout(ViewGroup.java:5437)
              at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
              at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
              at android.widget.ScrollView.onLayout(ScrollView.java:1525)
              at android.view.View.layout(View.java:16630)
              at android.view.ViewGroup.layout(ViewGroup.java:5437)
              at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
              at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1732)
              at android.widget.LinearLayout.onLayout(LinearLayout.java:1497)
              at android.view.View.layout(View.java:16630)
              at android.view.ViewGroup.layout(ViewGroup.java:5437)
              at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)
              at android.view.View.layout(View.java:16630)
              at android.view.ViewGroup.layout(ViewGroup.java:5437)
              at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
              at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
              at android.view.View.layout(View.java:16630)
              at android.view.ViewGroup.layout(ViewGroup.java:5437)
              at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
              at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
              at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
              at android.view.View.layout(View.java:16630)
              at android.view.ViewGroup.layout(ViewGroup.java:5437)
              at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
              at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
              at android.view.View.layout(View.java:16630)
              at android.view.ViewGroup.layout(ViewGroup.java:5437)
              at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
              at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
              at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
              at android.view.View.layout(View.java:16630)
              at android.view.ViewGroup.layout(ViewGroup.java:5437)
              at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
              at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
              at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2678)
              at android.view.View.layout(View.java:16630)
              at android.view.ViewGroup.layout(ViewGroup.java:5437)
              at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2171)
              at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1931)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1

这是我收到错误的代码

 @Override
public void onSizeChanged(int widthContentView, int heightContentView, int oldWidth, int oldHeight) {
    super.onSizeChanged(widthContentView, heightContentView, oldWidth, oldHeight);
    this.bitmap = Bitmap.createBitmap(widthContentView,heightContentView, Bitmap.Config.ARGB_8888);
    this.canvas = new Canvas(bitmap);
}

0 个答案:

没有答案