Android:如何更改Button的drawableLeft

时间:2011-11-14 02:18:43

标签: android button drawable

如果我最初将位图设置为drawableLeft(在xml中或通过onStart中的setCompoundDrawablesWithIntrinsicBounds等),则将(setCompoundDrawablesWithIntrinsicBounds再次)更改为另一个位图或AnimationDrawable,从而导致“StackOverflowError”。我没有找到任何API来发布谷歌文档中的原始drawLeft。对此有什么解决方案吗?

public class TestLinearlayoutActivity extends Activity {
    /** Called when the activity is first created. */
    Button locationTitleButton = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        locationTitleButton = (Button) findViewById(R.id.LocationTitleButton);
    }

    @Override
    public void onStart() {
        super.onStart();

        locationTitleButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
               locationTitleButton.setCompoundDrawablesWithIntrinsicBounds(R.drawable.loading, 0, 0, 0);



            }
        });

    }
}

leftDrawable的布局文件中有默认资源。在onClick上面的上述setCompoundDrawablesWithIntrinsicBounds之后,就会出现崩溃

11-14 18:18:13.321: ERROR/AndroidRuntime(2187): FATAL EXCEPTION: main
11-14 18:18:13.321: ERROR/AndroidRuntime(2187): java.lang.StackOverflowError
11-14 18:18:13.321: ERROR/AndroidRuntime(2187):     at android.util.LongSparseArray.get(LongSparseArray.java:87)
11-14 18:18:13.321: ERROR/AndroidRuntime(2187):     at android.util.LongSparseArray.get(LongSparseArray.java:79)
11-14 18:18:13.321: ERROR/AndroidRuntime(2187):     at android.content.res.Resources.getCachedDrawable(Resources.java:1746)
11-14 18:18:13.321: ERROR/AndroidRuntime(2187):     at android.content.res.Resources.loadDrawable(Resources.java:1664)
11-14 18:18:13.321: ERROR/AndroidRuntime(2187):     at android.content.res.Resources.getDrawable(Resources.java:581)
11-14 18:18:13.321: ERROR/AndroidRuntime(2187):     at android.graphics.drawable.AnimationDrawable.inflate(AnimationDrawable.java:265)
11-14 18:18:13.321: ERROR/AndroidRuntime(2187):     at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:788)
11-14 18:18:13.321: ERROR/AndroidRuntime(2187):     at android.graphics.drawable.Drawable.createFromXml(Drawable.java:729)
11-14 18:18:13.321: ERROR/AndroidRuntime(2187):     at android.content.res.Resources.loadDrawable(Resources.java:1694)
11-14 18:18:13.321: ERROR/AndroidRuntime(2187):     at android.content.res.Resources.getDrawable(Resources.java:581)
11-14 18:18:13.321: ERROR/AndroidRuntime(2187):     at android.graphics.drawable.AnimationDrawable.inflate(AnimationDrawable.java:265)
11-14 18:18:13.321: ERROR/AndroidRuntime(2187):     at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:788)
11-14 18:18:13.321: ERROR/AndroidRuntime(2187):     at android.graphics.drawable.Drawable.createFromXml(Drawable.java:729)
11-14 18:18:13.321: ERROR/AndroidRuntime(2187):     at android.content.res.Resources.loadDrawable(Resources.java:1694)
11-14 18:18:13.321: ERROR/AndroidRuntime(2187):     at android.content.res.Resources.getDrawable(Resources.java:581)
11-14 18:18:13.321: ERROR/AndroidRuntime(2187):     at android.graphics.drawable.AnimationDrawable.inflate(AnimationDrawable.java:265)
11-14 18:18:13.321: ERROR/AndroidRuntime(2187):     at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:788)
11-14 18:18:13.321: ERROR/AndroidRuntime(2187):     at android.graphics.drawable.Drawable.createFromXml(Drawable.java:729)
11-14 18:18:13.321: ERROR/AndroidRuntime(2187):     at android.content.res.Resources.loadDrawable(Resources.java:1694)
11-14 18:18:13.321: ERROR/AndroidRuntime(2187):     at android.content.res.Resources.getDrawable(Resources.java:581)
11-14 18:18:13.321: ERROR/AndroidRuntime(2187):     at android.graphics.drawable.AnimationDrawable.inflate(AnimationDrawable.java:265)
11-14 18:18:13.321: ERROR/AndroidRuntime(2187):     at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:788)
11-14 18:18:13.321: ERROR/AndroidRuntime(2187):     at android.graphics.drawable.Drawable.createFromXml(Drawable.java:729)
11-14 18:18:13.321: ERROR/AndroidRuntime(2187):     at android.content.res.Resources.loadDrawable(Resources.java:1694)
11-14 18:18:13.321: ERROR/AndroidRuntime(2187):     at android.content.res.Resources.getDrawable(Resources.java:581)
11-14 18:18:13.321: ERROR/AndroidRuntime(2187):     at android.graphics.drawable.AnimationDrawable.inflate(AnimationDrawable.java:265)
11-14 18:18:13.321: ERROR/AndroidRuntime(2187):     at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:788)
11-14 18:18:13.321: ERROR/AndroidRuntime(2187):     at android.graphics.drawable.Drawable.createFromXml(Drawable.java:729)
11-14 18:18:13.321: ERROR/AndroidRuntime(2187):     at android.content.res.Resources.loadDrawable(Resources.java:1694)
11-14 18:18:13.321: ERROR/AndroidRuntime(2187):     at android.content.res.Resources.getDrawable(Resources.java:581)
11-14 18:18:13.321: ERROR/AndroidRuntime(2187):     at android.graphics.drawable.AnimationDrawable.inflate(AnimationDrawable.java:265)
11-14 18:18:13.321: ERROR/AndroidRuntime(2187):     at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:788)
11-14 18:18:13.321: ERROR/AndroidRuntime(2187):     at android.graphics.drawable.Drawable.createFromXml(Drawable.java:729)
11-14 18:18:13.321: ERROR/AndroidRuntime(2187):     at android.content.res.Resources.loadDrawable(Resources.java:1694)
11-14 18:18:13.321: ERROR/AndroidRuntime(2187):     at android.content.res.Resources.getDrawable(Resources.java:581)
11-14 18:18:13.321: ERROR/AndroidRuntime(2187):     at android.graphics.drawable.AnimationDrawable.inflate(AnimationDrawable.java:265)
11-14 18:18:13.321: ERROR/AndroidRuntime(2187):     at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:788)
11-14 18:18:13.321: ERROR/AndroidRuntime(2187):     at android.graphics.drawable.Drawable.createFromXml(Drawable.java:729)
11-14 18:18:13.321: ERROR/AndroidRuntime(2187):     at android.content.res.Resources.loadDrawable(Resources.java:1694)
11-14 18:18:13.321: ERROR/AndroidRuntime(2187):     at android.content.res.Resources.getDrawable(Resources.java:581)
11-14 18:18:13.321: ERROR/AndroidRuntime(2187):     at android.graphics.drawable.AnimationDrawable.inflate(AnimationDrawable.java:265)
11-14 18:18:13.321: ERROR/AndroidRuntime(2187):     at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:788)
11-14 18:18:13.321: ERROR/AndroidRuntime(2187):     at android.graphics.drawable.Drawable.createFromXml(Drawable.java:729)
11-14 18:18:13.321: ERROR/AndroidRuntime(2187):     at android.content.res.Resources.loadDrawable(Resources.java:1694)
11-14 18:18:13.321: ERROR/AndroidRuntime(2187):     at android.content.res.Resources.getDrawable(Resources.java:581)
11-14 18:18:13.321: ERROR/AndroidRuntime(2187):     at android.graphics.drawable.AnimationDrawable.inflate(AnimationDrawable.java:265)
11-14 18:18:13.321: ERROR/AndroidRuntime(2187):     at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:788)
11-14 18:18:13.321: ERROR/AndroidRuntime(2187):     at android.graphics.drawable.Drawable.createFromXml(Drawable.java:729)
11-14 18:18:13.321: ERROR/AndroidRuntime(2187):     at android.content.res.Resources.loadDrawable(Resources.java:1694)
11-14 18:18:13.321: ERROR/AndroidRuntime(2187):     at android.content.res.Resources.getDrawable(Resources.java:581)
11-14 18:18:13.321: ERROR/AndroidRuntime(2187):     at android.graphics.drawable.AnimationDrawable.inflate(AnimationDrawable.java:265)
11-14 18:18:13.321: ERROR/AndroidRuntime(2187):     at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:788)
11-14 18:18:13.321: ERROR/AndroidRuntime(2187):     at android.graphics.drawable.Drawable.createFromXml(Drawable.java:729)
11-14 18:18:13.321: ERROR/AndroidRuntime(2187):     at android.content.res.Resources.loadDrawable(Resources.java:1694)
11-14 18:18:13.321: ERROR/AndroidRuntime(2187):     at android.content.res.Resources.getDrawable(Resources.java:581)
11-14 18:18:13.321: ERROR/AndroidRuntime(2187):     at and

0 个答案:

没有答案