45024

how to use custom ImageView

Question:

I have found the following code on how to get display height and width from ImageView: <a href="https://stackoverflow.com/questions/3855218/trying-to-get-the-display-size-of-an-image-in-an-imageview" rel="nofollow">Trying to get the display size of an image in an ImageView</a> . When I try to use it in the layout snippet below, I get following error.

<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" />

ERROR:

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

<strong>UPDATE</strong>

From the graphics Layout I also get the error

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)

<strong>SECOND UPDATE</strong>

the errored line in question is the second of

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

The complete code is here on stack overflow at <a href="https://stackoverflow.com/questions/3855218/trying-to-get-the-display-size-of-an-image-in-an-imageview" rel="nofollow">Trying to get the display size of an image in an ImageView</a> . It's the response with the most votes.

Answer1:

Make sure you have provided the right constructor for the XML. Your custom Views constructor should have the following method signature:

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

I'm guessing it currently looks like this:

public SizeAwareImageView(Context context){ // todo }

Also I'd double check you meant to set the XML parameter android:background, for ImageView you actually set android:src.

Recommend

  • Android Application Crashes With Orientation Changes
  • Unable to instantiate android.gms.maps.SupportMapFragment
  • app crash after i select the 3rd function [duplicate]
  • Error inflating class android.support.v7.app.MediaRouteButton
  • Map Fragment not working in Android4.4 kitkat
  • NoSuchMethodError on ews-java-api
  • Class not found using the boot class loader
  • Unmarshalling unknown type code exception while resuming
  • How do I access an unhandled exception in an MVC Error view?
  • Replace value with Factor in r data.table
  • FB SDK and cURL: Unknown SSL protocol error in connection to graph.facebook.com:443
  • Using $this when not in object context
  • How do I fake an specific browser client when using Java's Net library?
  • How reduce the height of an mschart by breaking up the y-axis
  • How to redirect a user to a different server and include HTTP basic authentication credentials?
  • Javascript Callbacks with Object constructor
  • Perl system calls when running as another user using sudo
  • Can a Chrome extension content script make an jQuery AJAX request for an html file that is itself a
  • Weird JavaScript statement, what does it mean?
  • Why winpcap requires both .lib and .dll to run?
  • Apache 2.4 - remove | delete | uninstall
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • Is there a mandatory requirement to switch app.yaml?
  • Cannot Parse HTML Data Using Android / JSOUP
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Unit Testing MVC Web Application in Visual Studio and Problem with QTAgent
  • Benchmarking RAM performance - UWP and C#
  • Load html files in TinyMce
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • Hits per day in Google Big Query
  • coudnt use logback because of log4j
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Java static initializers and reflection
  • Linking SubReports Without LinkChild/LinkMaster
  • unknown Exception android
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass
  • Observable and ngFor in Angular 2
  • Unable to use reactive element in my shiny app