Teo
Teo

Reputation: 3442

android custom component to layout

I would like to add my custom ImageView to the xml layout.

main.xml :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <com.android.gag.TouchImageView
        android:id="@+id/Image1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:adjustViewBounds="true"
        android:clickable="true"
        android:scaleType="center" />

    <ImageButton
        android:id="@+id/imageButton1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />

</RelativeLayout>

TouchImageView, as its name implies, extends the ImageView class.

Main.java

touchImageView = (TouchImageView)findViewById(R.id.Image1);

My app crashes. Logcat output:

10-16 20:38:20.275: E/AndroidRuntime(11354): FATAL EXCEPTION: main
10-16 20:38:20.275: E/AndroidRuntime(11354): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.gag/com.android.gag.Main}: android.view.InflateException: Binary XML file line #6: Error inflating class com.android.gag.TouchImageView
10-16 20:38:20.275: E/AndroidRuntime(11354):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
idRuntime(11354): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class com.android.gag.TouchImageView
10-16 20:38:20.275: E/AndroidRuntime(11354):    at android.view.LayoutInflater.createView(LayoutInflater.java:589)
10-16 20:38:20.275: E/AndroidRuntime(11354):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)

Please, could anyone help me? I am a beginner, so answers like '' replace "class library" with "Android library project"'' are pointless because they are too vague and I don't know where to start and where to go.

EDIT: Link to my TouchImageView class

Upvotes: 0

Views: 458

Answers (1)

mportuesisf
mportuesisf

Reputation: 5617

The code for your custom View class is missing two constructors. From the Android docs for the View class:

View(Context context, AttributeSet attrs)

Constructor that is called when inflating a view from XML.

View(Context context, AttributeSet attrs, int defStyle)

Perform inflation from XML and apply a class-specific base style.

It's crashing because it can't find the constructor it needs, so it can't inflate the view.

Implement both of these constructors for your TouchImageView class and see if the problem goes away.

Upvotes: 2

Related Questions