java.lang.IllegalArgumentException:宽度和高度必须>在Android中为0 [英] java.lang.IllegalArgumentException: width and height must be > 0 in android

查看:172
本文介绍了java.lang.IllegalArgumentException:宽度和高度必须>在Android中为0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从用户的MultiAutoCompleteTextView中获取文本,并以类似气泡的格式显示它们,但是我正在获取 :宽度和高度在Android中必须> 0

I am trying to get the text from the MultiAutoCompleteTextView from the user and show them in the bubble like format but I am getting : width and height must be > 0 in android

    final MultiAutoCompleteTextView tags = (MultiAutoCompleteTextView) findViewById(R.id.multiAutoCompleteTextView1);
    String[] TAGS = getResources().getStringArray(R.array.countries);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, TAGS);
    tags.setAdapter(adapter);
    tags.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
    tags.setThreshold(1);
    String contactName = tags.getText().toString();
    final SpannableStringBuilder sb = new SpannableStringBuilder();
    TextView tv = createContactTextView(contactName);
    BitmapDrawable bd = (BitmapDrawable) convertViewToDrawable(tv);
    bd.setBounds(0, 0, bd.getIntrinsicWidth(), bd.getIntrinsicHeight());

    sb.append(contactName + ",");
    sb.setSpan(new ImageSpan(bd), sb.length() - (contactName.length() + 1), sb.length() - 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    Toast.makeText(getApplicationContext(),sb,Toast.LENGTH_SHORT).show();

}

    public TextView createContactTextView(String text){
        //creating textview dynamically
        TextView tv = new TextView(this);
        tv.setText(text);
        tv.setTextSize(20);
        tv.setBackgroundResource(R.drawable.oval);
        tv.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.ic_clear_search_api_holo_light, 0);
        return tv;
    }

    public static Object convertViewToDrawable(View view) {
        int spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        view.measure(spec, spec);
        view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
        Bitmap b = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(b);
        c.translate(-view.getScrollX(), -view.getScrollY());
        view.draw(c);
        view.setDrawingCacheEnabled(true);
        Bitmap cacheBmp = view.getDrawingCache();
        Bitmap viewBmp = cacheBmp.copy(Bitmap.Config.ARGB_8888, true);
        view.destroyDrawingCache();
        return new BitmapDrawable(viewBmp);

    }

当我尝试运行此错误时,出现此错误 宽度和高度必须大于0.

when i try t run this i get this error width and height must be greater than 0.

  E/AndroidRuntime: FATAL EXCEPTION: main
                                               Process: com.androidbegin.train, PID: 11263
                                               java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidbegin.train/com.androidbegin.loginregister.WorkNow}: java.lang.IllegalArgumentException: width and height must be > 0
                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
                                                   at android.app.ActivityThread.access$800(ActivityThread.java:155)
                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                   at android.os.Looper.loop(Looper.java:135)
                                                   at android.app.ActivityThread.main(ActivityThread.java:5343)
                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                   at java.lang.reflect.Method.invoke(Method.java:372)
                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
                                                Caused by: java.lang.IllegalArgumentException: width and height must be > 0
                                                   at android.graphics.Bitmap.createBitmap(Bitmap.java:836)
                                                   at android.graphics.Bitmap.createBitmap(Bitmap.java:815)
                                                   at android.graphics.Bitmap.createBitmap(Bitmap.java:782)
                                                   at com.androidbegin.loginregister.WorkNow.convertViewToDrawable(WorkNow.java:57)
                                                   at com.androidbegin.loginregister.WorkNow.onCreate(WorkNow.java:34)
                                                   at android.app.Activity.performCreate(Activity.java:6010)
                                                   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413) 
                                                   at android.app.ActivityThread.access$800(ActivityThread.java:155) 
                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317) 
                                                   at android.os.Handler.dispatchMessage(Handler.java:102) 

推荐答案

我认为主要问题是spec被设置为0

I think the main problem is that specis being set to 0

int spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);

然后,在这里,视图的宽度和高度将为0 ...

Then, here, view will have 0 as width and height...

view.measure(spec, spec);
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());

因此,在创建位图时,会出现Exception的原因,因为view.getMeasuredWidth()view.getMeasuredHeight()都为0,而不能为

So, when creating the Bitmap, an Exception will happen because view.getMeasuredWidth() and view.getMeasuredHeight() are both 0 and they can't be

Bitmap b = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);

如果可能,请尝试更改为以下内容:

If possible, try to change to something like:

int spec = View.MeasureSpec.makeMeasureSpec(ViewGroup.LayoutParams.WRAP_CONTENT, View.MeasureSpec.UNSPECIFIED);

这篇关于java.lang.IllegalArgumentException:宽度和高度必须&gt;在Android中为0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆