java.lang.IllegalArgumentException: width and height must be > 0 in android

a 夏天 提交于 2020-01-06 03:48:26

问题


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);

    }

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) 

回答1:


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

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

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

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

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);


来源:https://stackoverflow.com/questions/38548155/java-lang-illegalargumentexception-width-and-height-must-be-0-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!