findById在"mImageView"& "mCropOverlayView"的自定義視圖中回傳 "null"。
public class CropImageView extends FrameLayout {
private ImageView mImageView。
private CropOverlayView mCropOverlayView;
public CropImageView(@NonNull Context context) {
super(context)。
init(context)。
}
public CropImageView(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init(context)。
}
public CropImageView(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
super(context, attrs, defStyleAttr)。
init(context)。
}
private void init(Context context) {
View view = inflate(getContext(), R.layout.crop_image_view, this) 。
mImageView = view.findViewById(R.id.img_crop);
mCropOverlayView = view.findViewById(R.id.overlay_crop)。
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas)。
}
public void setImageBitmap(Bitmap bitmap) {
mImageView.setImageBitmap(bitmap)。
mCropOverlayView.setBitmap(bitmap);
}
public void crop(CropListener listener, boolean needStretch){
if (listener == null)
return;
mCropOverlayView.crop(listener, needStretch)。
}
這是我為自定義視圖準備的XML檔案,我正試圖在我的自定義視圖中充氣。
<?xml version="1.0" encoding="utf-8"? >
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"/span>
android:layout_width="match_parent"。
android:layout_height="match_parent"/span>>
<ImageView
android:id="@ id/img_crop"。
android:layout_width="match_parent"。
android:layout_height="match_parent"。
android:adjustViewBounds="true"。
android:scaleType="fitCenter"/span> />
<com.wildma.idcardcamera.cropper.CropOverlayView。
android:id="@ id/overlay_crop"
android:layout_width="match_parent"。
android:layout_height="match_parent"。
android:layout_alignBottom="@ id/img_crop"/span> />
</RelativeLayout>
uj5u.com熱心網友回復:
Hello @Syed Arsalan Kazmi
我創建了一個演示來快速測驗你的問題,findViewById()的值不是空的。螢屏上的截圖。http://prntscr.com/1sdnvuh
開發環境。Android Studio 4.2.2, Android support lib, SDK 30
uj5u.com熱心網友回復:
發生的情況是inflate()沒有附加充氣的視圖,如果最后一個引數不是空的,它會回傳這個值。最簡單的解決方法是傳遞null而不是這個:
View view = inflate(getContext(),R.layout.crop_image_view,null)。
回傳的視圖將是RelativeLayout。你可能想把它添加到你的視圖層次結構中:
addView(view);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/306693.html
標籤:
