1

This seems like a simple thing, but it does not run. What am I doing wrong?

I'm subclassing ImageView, but if I put it inside XML layouts, it gives:

    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myappSubclass/com.example.myappSubclass.MyActivity}:

android.view.InflateException: Binary XML file line #21: Error inflating class com.example.myappSubclass.ImageViewSubclass

Here is my code:

package com.example.myappSubclass;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;

import java.util.jar.Attributes;

/**
 * Created by me on 2014-07-11.
 */


public class ImageViewSubclass extends ImageView {
    public Context thisContext;

    public ImageViewSubclass(Context context, AttributeSet attrs, Context thisContext) {
        super(context, attrs);
        this.thisContext = thisContext;
    }

    public ImageViewSubclass(Context context, AttributeSet attrs, int defStyle, Context thisContext) {
        super(context, attrs, defStyle);
        this.thisContext = thisContext;
    }

    public ImageViewSubclass(Context context) {
        super(context);
        this.thisContext = context;
    }


    public void changeImage(){

        this.setImageLevel(0);

    }

And inside the XML I have it simply:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
        >
    <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="Hello World, MyActivity"
            />


    <Button android:layout_width="match_parent" android:layout_height="50dp"
            android:layout_marginLeft="50dp"
            android:layout_marginRight="50dp"
            android:text="Test"
            />


    <com.example.myappSubclass.ImageViewSubclass android:layout_width="fill_parent"
                                                           android:layout_height="fill_parent"/>

    <!--<ImageView android:layout_width="fill_parent" android:layout_height="fill_parent"/>-->


</LinearLayout>
mskw
  • 10,063
  • 9
  • 42
  • 64

1 Answers1

3

Custom views must have predefined constructors to be able to inflate them from an XML layout. You should remove thisContext from all of them.

public ImageViewSubclass(Context context) {
    super(context);
    ...
}

public ImageViewSubclass(Context context, AttributeSet attrs) {
    super(context, attrs);
    ...
}

public ImageViewSubclass(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    ...
}

Why did you add that extra parameter anyway? All views have a getContext() method if that's what you needed.

matiash
  • 54,791
  • 16
  • 125
  • 154
  • @mskw At least in the question you have an extra parameter in each one. – matiash Jul 11 '14 at 21:37
  • Right, i just saw it. Intellij added those for me, I missed the fact that I ask it to also add thisContext to the constructors. Thanks for your answer. – mskw Jul 11 '14 at 21:39
  • I'm still confused about what Context does thats why! – mskw Jul 11 '14 at 21:40
  • And try using smaller image to see if it is a memory-related issue. It happened to me last day. – Lazy Jul 11 '14 at 21:41
  • 1
    @mskw It's confusing at first, but you'll get the hang of it. :) Check http://stackoverflow.com/questions/3572463/what-is-context-in-android for example. – matiash Jul 11 '14 at 21:42