0

我找到了以下关于如何从 ImageView 获取显示高度和宽度的代码:Trying to get the display size of an image in an ImageView。当我尝试在下面的布局片段中使用它时,出现以下错误。

 <com.mycompany.widget.SizeAwareImageView
                android:id="@+id/account_profile_img"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:background="@drawable/avatar"
                android:clickable="true"
                android:gravity="bottom|center_horizontal"
                android:onClick="onEditPhotoClicked" />

错误:

FATAL EXCEPTION: main
 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.company.myapp/com.company.myapp.RegisterActivity}: android.view.InflateException: Binary XML file line #218: Error inflating class com.company.myapp.widget.SizeAwareImageView
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2247)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2297)
    at android.app.ActivityThread.access$700(ActivityThread.java:152)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5328)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
    at dalvik.system.NativeStart.main(Native Method)
 Caused by: android.view.InflateException: Binary XML file line #218: Error inflating class com.company.myapp.widget.SizeAwareImageView
    at android.view.LayoutInflater.createView(LayoutInflater.java:602)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:693)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:760)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:327)
    at android.app.Activity.setContentView(Activity.java:1928)
    at com.company.myapp.RegisterActivity.onCreate(RegisterActivity.java:63)
    at android.app.Activity.performCreate(Activity.java:5250)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
    ... 11 more
 Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
    at java.lang.Class.getConstructorOrMethod(Class.java:460)
    at java.lang.Class.getConstructor(Class.java:431)
    at android.view.LayoutInflater.createView(LayoutInflater.java:567)
    ... 25 more

更新

从图形布局我也得到了错误

java.lang.NullPointerException
Exception details are logged in Window > Show View > Error Log

java.lang.NullPointerException
    at com.company.myapp.widget.SizeAwareImageView.onMeasure(SizeAwareImageView.java:66)
    at android.view.View.measure(View.java:15848)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
    at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1052)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:590)
    at android.view.View.measure(View.java:15848)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
    at android.view.View.measure(View.java:15848)
    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728)
    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:477)
    at android.view.View.measure(View.java:15848)
    at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1217)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
    at android.widget.ScrollView.onMeasure(ScrollView.java:321)
    at android.view.View.measure(View.java:15848)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
    at android.view.View.measure(View.java:15848)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
    at android.view.View.measure(View.java:15848)
    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
    at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
    at android.view.View.measure(View.java:15848)

第二次更新

有问题的错误行是第二个

final Drawable d = getDrawable();
final int origW = d.getIntrinsicWidth();
final int origH = d.getIntrinsicHeight();

完整的代码在Trying to get the display size in an ImageView 的堆栈溢出中。这是得票最多的回应。

4

1 回答 1

3

确保您为 XML 提供了正确的构造函数。您的自定义 Views 构造函数应具有以下方法签名:

public SizeAwareImageView(Context context, AttributeSet attrs){
  // todo
}

我猜它目前看起来像这样:

public SizeAwareImageView(Context context){
  // todo
}

此外,我会仔细检查您是否打算android:background为您实际设置的 ImageView设置 XML 参数android:src

于 2013-09-04T22:35:18.707 回答