I am trying to implement Pinch To Zoom on an Android app I am working on. For this, I have made a Java Class called PinchToZoom using the Android developer’s website. However, if I try to launch my app I get an error in my XML file. I am confused as to why though, because I thought I had implemented it correctly. This is the XML code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#222222"
android:orientation="vertical" >
<com.andriesse.athena.PinchToZoom
android:id="@+id/selectedImage"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:adjustViewBounds="true"
android:contentDescription="@string/content" />
</LinearLayout>
I hope you guys can help me out!
EDIT:
I forgot to add the error log before, so here it is:
10-03 13:35:40.207: E/AndroidRuntime(2421): FATAL EXCEPTION: main
10-03 13:35:40.207: E/AndroidRuntime(2421): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.andriesse.athena/com.andriesse.athena.ImageEditing}: android.view.InflateException: Binary XML file line #8: Error inflating class com.andriesse.athena.PinchToZoom
10-03 13:35:40.207: E/AndroidRuntime(2421): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1967)
10-03 13:35:40.207: E/AndroidRuntime(2421): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1992)
10-03 13:35:40.207: E/AndroidRuntime(2421): at android.app.ActivityThread.access$600(ActivityThread.java:127)
10-03 13:35:40.207: E/AndroidRuntime(2421): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1158)
10-03 13:35:40.207: E/AndroidRuntime(2421): at android.os.Handler.dispatchMessage(Handler.java:99)
10-03 13:35:40.207: E/AndroidRuntime(2421): at android.os.Looper.loop(Looper.java:137)
10-03 13:35:40.207: E/AndroidRuntime(2421): at android.app.ActivityThread.main(ActivityThread.java:4441)
10-03 13:35:40.207: E/AndroidRuntime(2421): at java.lang.reflect.Method.invokeNative(Native Method)
10-03 13:35:40.207: E/AndroidRuntime(2421): at java.lang.reflect.Method.invoke(Method.java:511)
10-03 13:35:40.207: E/AndroidRuntime(2421): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-03 13:35:40.207: E/AndroidRuntime(2421): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-03 13:35:40.207: E/AndroidRuntime(2421): at dalvik.system.NativeStart.main(Native Method)
10-03 13:35:40.207: E/AndroidRuntime(2421): Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class com.andriesse.athena.PinchToZoom
10-03 13:35:40.207: E/AndroidRuntime(2421): at android.view.LayoutInflater.createView(LayoutInflater.java:589)
10-03 13:35:40.207: E/AndroidRuntime(2421): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
10-03 13:35:40.207: E/AndroidRuntime(2421): at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
10-03 13:35:40.207: E/AndroidRuntime(2421): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
10-03 13:35:40.207: E/AndroidRuntime(2421): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
10-03 13:35:40.207: E/AndroidRuntime(2421): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
10-03 13:35:40.207: E/AndroidRuntime(2421): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
10-03 13:35:40.207: E/AndroidRuntime(2421): at android.app.Activity.setContentView(Activity.java:1835)
10-03 13:35:40.207: E/AndroidRuntime(2421): at com.andriesse.athena.ImageEditing.onCreate(ImageEditing.java:26)
10-03 13:35:40.207: E/AndroidRuntime(2421): at android.app.Activity.performCreate(Activity.java:4465)
10-03 13:35:40.207: E/AndroidRuntime(2421): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
10-03 13:35:40.207: E/AndroidRuntime(2421): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1931)
10-03 13:35:40.207: E/AndroidRuntime(2421): ... 11 more
10-03 13:35:40.207: E/AndroidRuntime(2421): Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet]
10-03 13:35:40.207: E/AndroidRuntime(2421): at java.lang.Class.getConstructorOrMethod(Class.java:460)
10-03 13:35:40.207: E/AndroidRuntime(2421): at java.lang.Class.getConstructor(Class.java:431)
10-03 13:35:40.207: E/AndroidRuntime(2421): at android.view.LayoutInflater.createView(LayoutInflater.java:561)
10-03 13:35:40.207: E/AndroidRuntime(2421): ... 22 more
This is the key line:
Make sure that your custom listview has these constructors:
I’ve chained them this way so you never have to do anything except in the 3rd constructor. If you have these constructors, and they don’t have external dependencies that can only be resolved at runtime, your custom listview will also preview correctly.