setCompoundDrawablesRelativeWithIntrinsicBounds

在Android 2.2版本中,遇到调用setCompoundDrawablesRelativeWithIntrinsicBounds方法时抛出NoSuchMethodError异常。错误堆栈显示在onResume()方法中。解决方案是通过判断系统版本,当版本低于API 14时,使用setCompoundDrawables()代替,从而兼容低版本系统。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

android 2.2 无法使用:setCompoundDrawablesRelativeWithIntrinsicBounds

错误代码:

 FATAL EXCEPTION: main
 java.lang.NoSuchMethodError: android.widget.RadioButton.setCompoundDrawablesRelativeWithIntrinsicBounds
  at com.vkoov.zjth.LatestActivity01.onResume(Unknown Source)
  at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1149)
  at android.app.Activity.performResume(Activity.java:3823)
  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3118)
  at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:170)
  at android.app.LocalActivityManager.dispatchResume(LocalActivityManager.java:518)
  at android.app.ActivityGroup.onResume(ActivityGroup.java:58)
  at com.vkoov.YaloeActivity.onResume(Unknown Source)
  at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1149)
  at android.app.Activity.performResume(Activity.java:3823)
  at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3118)
  at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3143)
  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2684)
  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
  at android.os.Handler.dispatchMessage(Handler.java:99)
  at android.os.Looper.loop(Looper.java:123)
  at android.app.ActivityThread.main(ActivityThread.java:4627)
  at java.lang.reflect.Method.invokeNative(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:521)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636)
  at dalvik.system.NativeStart.main(Native Method)

解决办法:

int sysVersion = Integer.parseInt(VERSION.SDK);
   if (sysVersion < 14) {
    drawable.setBounds(0, 0, 30,30);
    ((RadioButton) (getParent().findViewById(R.id.main_tab_zjth)))
      .setCompoundDrawables(null, drawable, null, null);
   } else {
    ((RadioButton) (getParent().findViewById(R.id.main_tab_zjth)))
      .setCompoundDrawablesRelativeWithIntrinsicBounds(null,
        drawable, null, null);
   }

转载于:https://my.oschina.net/arrom/blog/299160

/* * Copyright (C) 2020 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.android.quickstep.views; import android.content.Context; import android.content.res.Configuration; import android.graphics.Rect; import android.util.AttributeSet; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.FrameLayout; import androidx.annotation.IntDef; import androidx.annotation.Nullable; import com.android.launcher3.DeviceProfile; import com.android.launcher3.Insettable; import com.android.launcher3.R; import com.android.launcher3.config.FeatureFlags; import com.android.launcher3.util.DisplayController; import com.android.launcher3.util.MultiPropertyFactory.MultiProperty; import com.android.launcher3.util.MultiValueAlpha; import com.android.launcher3.util.NavigationMode; import com.android.quickstep.TaskOverlayFactory.OverlayUICallbacks; import com.android.quickstep.util.LayoutUtils; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; /** * View for showing action buttons in Overview */ public class OverviewActionsView<T extends OverlayUICallbacks> extends FrameLayout implements OnClickListener, Insettable { private final Rect mInsets = new Rect(); @IntDef(flag = true, value = { HIDDEN_NON_ZERO_ROTATION, HIDDEN_NO_TASKS, HIDDEN_NO_RECENTS, HIDDEN_SPLIT_SCREEN, HIDDEN_SPLIT_SELECT_ACTIVE, HIDDEN_ACTIONS_IN_MENU, HIDDEN_DESKTOP }) @Retention(RetentionPolicy.SOURCE) public @interface ActionsHiddenFlags { } public static final int HIDDEN_NON_ZERO_ROTATION = 1 << 0; public static final int HIDDEN_NO_TASKS = 1 << 1; public static final int HIDDEN_NO_RECENTS = 1 << 2; public static final int HIDDEN_SPLIT_SCREEN = 1 << 3; public static final int HIDDEN_SPLIT_SELECT_ACTIVE = 1 << 4; public static final int HIDDEN_ACTIONS_IN_MENU = 1 << 5; public static final int HIDDEN_DESKTOP = 1 << 6; @IntDef(flag = true, value = { DISABLED_SCROLLING, DISABLED_ROTATED, DISABLED_NO_THUMBNAIL}) @Retention(RetentionPolicy.SOURCE) public @interface ActionsDisabledFlags { } public static final int DISABLED_SCROLLING = 1 << 0; public static final int DISABLED_ROTATED = 1 << 1; public static final int DISABLED_NO_THUMBNAIL = 1 << 2; private static final int INDEX_CONTENT_ALPHA = 0; private static final int INDEX_VISIBILITY_ALPHA = 1; private static final int INDEX_FULLSCREEN_ALPHA = 2; private static final int INDEX_HIDDEN_FLAGS_ALPHA = 3; private static final int INDEX_SHARE_TARGET_ALPHA = 4; private static final int INDEX_SCROLL_ALPHA = 5; private static final int NUM_ALPHAS = 6; public @interface SplitButtonHiddenFlags { } public static final int FLAG_IS_NOT_TABLET = 1 << 0; public @interface SplitButtonDisabledFlags { } public static final int FLAG_SINGLE_TASK = 1 << 0; private MultiValueAlpha mMultiValueAlpha; private Button mSplitButton; @ActionsHiddenFlags private int mHiddenFlags; @ActionsDisabledFlags protected int mDisabledFlags; @SplitButtonHiddenFlags private int mSplitButtonHiddenFlags; @SplitButtonDisabledFlags private int mSplitButtonDisabledFlags; @Nullable protected T mCallbacks; @Nullable protected DeviceProfile mDp; private final Rect mTaskSize = new Rect(); public OverviewActionsView(Context context) { this(context, null); } public OverviewActionsView(Context context, @Nullable AttributeSet attrs) { this(context, attrs, 0); } public OverviewActionsView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr, 0); } @Override protected void onFinishInflate() { super.onFinishInflate(); android.util.Log.e("RecentsView_wangw", "onFinishInflate "); mMultiValueAlpha = new MultiValueAlpha(findViewById(R.id.action_buttons), NUM_ALPHAS); mMultiValueAlpha.setUpdateVisibility(true); findViewById(R.id.action_screenshot).setOnClickListener(this); findViewById(R.id.recents_clear_all_btn).setOnClickListener(this); mSplitButton = findViewById(R.id.action_split); mSplitButton.setOnClickListener(this); } /** * Set listener for callbacks on action button taps. * * @param callbacks for callbacks, or {@code null} to clear the listener. */ public void setCallbacks(T callbacks) { mCallbacks = callbacks; } @Override public void onClick(View view) { if (mCallbacks == null) { return; } int id = view.getId(); if (id == R.id.action_screenshot) { mCallbacks.onScreenshot(); } else if (id == R.id.action_split) { mCallbacks.onSplit(); }else if (id == R.id.recents_clear_all_btn) { mCallbacks.clearAllRecentTask(); } } @Override protected void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); updateVerticalMargin(DisplayController.getNavigationMode(getContext())); } @Override public void setInsets(Rect insets) { mInsets.set(insets); updateVerticalMargin(DisplayController.getNavigationMode(getContext())); updatePadding(); } public void updateHiddenFlags(@ActionsHiddenFlags int visibilityFlags, boolean enable) { android.util.Log.e("RecentsView_wangw", "updateHiddenFlags enable = "+enable+", visibilityFlags = "+visibilityFlags); if (enable) { mHiddenFlags |= visibilityFlags; } else { mHiddenFlags &= ~visibilityFlags; } boolean isHidden = mHiddenFlags != 0; mMultiValueAlpha.get(INDEX_HIDDEN_FLAGS_ALPHA).setValue(isHidden ? 0 : 1); } /** * Updates the proper disabled flag to indicate whether OverviewActionsView should be enabled. * Ignores DISABLED_ROTATED flag for determining enabled. Flag is used to enable/disable * buttons individually, currently done for select button in subclass. * * @param disabledFlags The flag to update. * @param enable Whether to enable the disable flag: True will cause view to be disabled. */ public void updateDisabledFlags(@ActionsDisabledFlags int disabledFlags, boolean enable) { android.util.Log.e("RecentsView_wangw", "updateDisabledFlags enable = "+enable+", disabledFlags = "+disabledFlags); if (enable) { mDisabledFlags |= disabledFlags; } else { mDisabledFlags &= ~disabledFlags; } boolean isEnabled = (mDisabledFlags & ~DISABLED_ROTATED) == 0; LayoutUtils.setViewEnabled(this, true); updateSplitButtonEnabledState(); } /** * Updates the proper flags to indicate whether the "Split screen" button should be hidden. * * @param flag The flag to update. * @param enable Whether to enable the hidden flag: True will cause view to be hidden. */ public void updateSplitButtonHiddenFlags(@SplitButtonHiddenFlags int flag, boolean enable) { android.util.Log.e("RecentsView_wangw", "updateSplitButtonHiddenFlags enable = "+enable+", flag = "+flag); if (enable) { mSplitButtonHiddenFlags |= flag; } else { mSplitButtonHiddenFlags &= ~flag; } if (mSplitButton == null) return; boolean shouldBeVisible = mSplitButtonHiddenFlags == 0; mSplitButton.setVisibility(shouldBeVisible ? VISIBLE : GONE); findViewById(R.id.action_split_space).setVisibility(shouldBeVisible ? VISIBLE : GONE); } /** * Updates the proper flags to indicate whether the "Split screen" button should be disabled. * * @param flag The flag to update. * @param enable Whether to enable the disable flag: True will cause view to be disabled. */ public void updateSplitButtonDisabledFlags(@SplitButtonDisabledFlags int flag, boolean enable) { android.util.Log.e("RecentsView_wangw", "updateSplitButtonDisabledFlags enable = "+enable+", flag = "+flag); if (enable) { mSplitButtonDisabledFlags |= flag; } else { mSplitButtonDisabledFlags &= ~flag; } updateSplitButtonEnabledState(); } public MultiProperty getContentAlpha() { return mMultiValueAlpha.get(INDEX_CONTENT_ALPHA); } public MultiProperty getVisibilityAlpha() { return mMultiValueAlpha.get(INDEX_VISIBILITY_ALPHA); } public MultiProperty getFullscreenAlpha() { return mMultiValueAlpha.get(INDEX_FULLSCREEN_ALPHA); } public MultiProperty getShareTargetAlpha() { return mMultiValueAlpha.get(INDEX_SHARE_TARGET_ALPHA); } public MultiProperty getIndexScrollAlpha() { return mMultiValueAlpha.get(INDEX_SCROLL_ALPHA); } /** * Offsets OverviewActionsView horizontal position based on 3 button nav container in taskbar. */ private void updatePadding() { // If taskbar is in overview, overview action has dedicated space above nav buttons setPadding(mInsets.left, 0, mInsets.right, 0); } /** Updates vertical margins for different navigation mode or configuration changes. */ public void updateVerticalMargin(NavigationMode mode) { if (mDp == null) { return; } LayoutParams actionParams = (LayoutParams) findViewById( R.id.action_buttons).getLayoutParams(); actionParams.setMargins( actionParams.leftMargin, mDp.overviewActionsTopMarginPx, actionParams.rightMargin, getBottomMargin()); } private int getBottomMargin() { if (mDp == null) { return 0; } if (mDp.isTablet && FeatureFlags.ENABLE_GRID_ONLY_OVERVIEW.get()) { return mDp.stashedTaskbarHeight; } // Align to bottom of task Rect. return mDp.heightPx - mTaskSize.bottom - mDp.overviewActionsTopMarginPx - mDp.overviewActionsHeight; } /** * Updates device profile and task size for this view to draw with. */ public void updateDimension(DeviceProfile dp, Rect taskSize) { mDp = dp; mTaskSize.set(taskSize); updateVerticalMargin(DisplayController.getNavigationMode(getContext())); requestLayout(); mSplitButton.setCompoundDrawablesRelativeWithIntrinsicBounds( (dp.isLandscape ? R.drawable.ic_split_horizontal : R.drawable.ic_split_vertical), 0, 0, 0); } /** * Enables/disables the "Split" button based on the status of mSplitButtonDisabledFlags and * mDisabledFlags. */ private void updateSplitButtonEnabledState() { if (mSplitButton == null) { return; } boolean isParentEnabled = (mDisabledFlags & ~DISABLED_ROTATED) == 0; boolean shouldBeEnabled = mSplitButtonDisabledFlags == 0 && isParentEnabled; android.util.Log.e("RecentsView_wangw", "updateSplitButtonEnabledState shouldBeEnabled = "+shouldBeEnabled); mSplitButton.setEnabled(shouldBeEnabled); } }
最新发布
07-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值