Android: Custom TextView Inflate Exception
Asked Answered
D

2

6

I have created a custom TextView and the only thing I did is I put one "instance" in the layout xml. This causes to crash the app with an "Inflate Exception" :

Here's the complete custom class (actually there is nothing "custom" in it yet...):

 package com.example.TestApp;
...
public  class MyTextView extends TextView {

public MyTextView (Context context) {
    super(context);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);        }


@Override
public void onDraw(Canvas canvas) {

    super.onDraw(canvas);

}

}

The complete layout file:

<?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.TestApp.MyTextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Hello World, MyActivity"
    />
</LinearLayout>

The log is:

java.lang.RuntimeException: Unable to start activity 
ComponentInfo{com.example.TestApp/com.example.TestApp.MyActivity}: 
android.view.InflateException: Binary XML file line #8: Error inflating class 
com.example.TestApp.MyTextView
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:130)
    at android.app.ActivityThread.main(ActivityThread.java:3691)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670)
    at dalvik.system.NativeStart.main(Native Method)

I have read through many posts about these inflate exceptions, but this time it's really the most easist case. The class path used in xml (com.example.TestApp.MyTextView) seems to be correct - Intellj IDEA even suggested it...

Btw: The min target sdk version is set to 10.

Diffractive answered 5/5, 2013 at 8:5 Comment(0)
R
9

add constructor like this

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

Hope this helps

Regimen answered 5/5, 2013 at 8:22 Comment(0)
C
4

add constructor

public MyTextView(Context context, AttributeSet attrs)
Caveat answered 5/5, 2013 at 8:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.