1

I'm trying to add a subclass of View to my activity_main.xml like this.

However, the device and emulator drop out of the app straight away. The error appears to be Error inflating class com.example.androidtest.PuzzleView

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

<com.example.androidtestapp.PuzzleView
    android:id="@+id/puzzleView"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="60"/>

<include
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="40"
    layout="@layout/activity_input"/>

</LinearLayout>

and

package com.example.androidtestapp;


public class PuzzleView extends View {


public PuzzleView(Context context) {
    super(context);
    setFocusable(true);
    setFocusableInTouchMode(true);


}
}
Community
  • 1
  • 1
  • Could you post the error too? – Iain_b Aug 15 '12 at 14:37
  • I have fixed this but I cannot post the answer for another 7 hours! I just needed to add all three constructor overrides to the class, like [this](http://stackoverflow.com/questions/3739661/android-error-inflating-class). – rickyoswald Aug 15 '12 at 14:38
  • 2
    You forget about constructor? `public PuzzleView(Context context, AttributeSet attrs) ` – KoVadim Aug 15 '12 at 14:39
  • Sorry, I didn't see these comments before I answered. After I made my first comment I remembered I hade similar problems in the past. Sorry. – Iain_b Aug 15 '12 at 14:41

1 Answers1

2

I think the problem is you need to have a constructor with AttributeSet attrs as a parameter

public PuzzleView(Context context, AttributeSet attrs) {
    super(context, attrs);
    //code
}
Iain_b
  • 1,043
  • 5
  • 13
  • Yes, I had the same problem, as I know you have to implement all constructors for the extended class, in order to work properly. – Rolice Aug 15 '12 at 14:48