Android - Error when inflating custom View in XML

android custom view
android create custom view with xml layout
android view xml
android custom view recyclerview
android constraintlayout custom view
android.view.inflateexception: binary xml file line
android custom view layout params
android custom view constructor

I have a custom view but I'm unable to use it because something related to namespaces that causing an Exception:

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.pc.easycalc, PID: 30694
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.pc.easycalc/com.example.pc.easycalc.MainActivity}: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class MyDisplay

...
Caused by: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class MyDisplay
                   Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class MyDisplay
                   Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.MyDisplay" on path: DexPathList[[zip file "/data/app/com.example.pc.easycalc-1/base.apk",

My custom view:

package com.example.pc.easycalc;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.widget.TextView;


public class MyDisplay extends android.support.v7.widget.AppCompatTextView {

    public MyDisplay(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // ..
    }

    public MyDisplay(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        // ..
    }

    public MyDisplay(Context context) {
        super(context);
        // ..
    }
}

Java code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main); // here

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.pc.easycalc.MainActivity">


    <include layout="@layout/content_main" />


</android.support.design.widget.CoordinatorLayout>

content_main.xml (where my custom view is used)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/root"
    >

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_weight="10"
        android:id="@+id/display"
        style="DisplayContainer"
        >

        <TextView
            android:id="@+id/hotDisplay"
            android:textColor="@color/quasiWhite"
            android:textSize="24sp"
            android:text="12 +"
            style="@style/DisplayStyle"
            />

        <MyDisplay
            android:id="@+id/resDisplay"
            android:textSize="60sp"
            android:text="0.0000000"
            style="@style/DisplayStyle"
            />

    </LinearLayout>

    ...

Thank you in advance!

PD: There is a couple of threads about this but I could not solve my problem with a custom view.

You need to use the fully-qualified name of your view. Use <com.example.pc.easycalc.MyDisplay> in your layout xml. When a fully qualified name is not specified, the LayoutInflater defaults to searching in android.widget and android.view, and your view does not exist in those packages.

Custom View inflating failed � Issue #1423 � JakeWharton/butterknife , ImageViewNumber Caused by: android.view.InflateException: Binary XML file line #21: Error inflating class com.me.me.myapp. Questions: I am new to Android. Whenever I run app on emulator it say”Unfortunalty stopped” when I run and checked the ADT logs it says 05-01 15:43:26.597: E/dalvikvm-heap(1022): Out of memory on a 36864016-byte allocation. 05-01 15:43:26.687: W/dalvikvm(1022): threadid=1: thread exiting with uncaught exception (group=0x40a71930) 05-01 15:43:26.817: E/AndroidRuntime(1022): FATAL EXCEPTION

The problem is defined as:

Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.MyDisplay"

you are declaring the view in your layout:

  <MyDisplay
        android:id="@+id/resDisplay"
        android:textSize="60sp"
        android:text="0.0000000"
        style="@style/DisplayStyle"
        />

but you have to set the complete package ( fully-qualified name ), i think that must be :

 <com.example.pc.easycalc.MyDisplay
        android:id="@+id/resDisplay"
        android:textSize="60sp"
        android:text="0.0000000"
        style="@style/DisplayStyle"
        />

Unable to inflate a layout which has custom controls with mixed , CustomTextView 05-21 18:31:12.620 15267 15267 E mono-rt : Caused by: android.view.InflateException: Binary XML file line #1: Error inflating� the button selector is the cause of the problem .. if you read the remaining of the exception, the root cause will be mentioned .. as resource id can’t be found !

Call it using :

<com.example.pc.easycalc.MyDisplay
        android:id="@+id/resDisplay"
        android:textSize="60sp"
        android:text="0.0000000"
        style="@style/DisplayStyle"
        />

android.view.InflateException: Binary XML file line #28: Error , InflateException: Binary XML file line #28: Error inflating class <unknown> rInflate(LayoutInflater.java:739) W/WindowManager( 197): at android.view. The sad part is, custom view of ActionBar is parsed _before_ activity's onCreate() is� Dismiss Join GitHub today. GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.

It will give you an exception because except Google Library views no one can create a view that won't show the package name front of layout name:

So add the package name of your class front of layout name...


com. ___ .MyDisplay

Error inflating class custom view — Xamarin Community Forums, Views.View,Android.Views.ViewGroup) [0x00021] in c:\Code\Dvit\Dvit. InflateException: Binary XML file line #1: Error inflating class Dvit. Caused by: java.lang.NoSuchMethodException: <init> [class android.content.Context, interface android.util.AttributeSet] there is no method which has context and attributes only. you need to pass all the required parameters.

Custom View Components, The EditText is created with these parameters when it is inflated from an XML layout file, thus, our constructor needs to both take them and pass� View Binding Part of Android Jetpack. View binding is a feature that allows you to more easily write code that interacts with views. Once view binding is enabled in a module, it generates a binding class for each XML layout file present in that module.

Creating custom and compound views in Android - Tutorial, Define additional attributes for your custom Views. 6. Exercise: In this class you inflate the layout file and implement the View connection logic To define additional attributes create an attrs.xml file in your res/values folder. man you are awesome!!!!! 3 days of killing my brain and so simple answer!!!

Custom Views in Android with Kotlin (KAD 06), Learn to create custom views on Android in a simple way thanks to the on where and how the view is being created (by code, using XML, if a theme is set… ) If you know that you are only inflating your view from Kotlin code, for example, you @JvmOverloads crashes for me every second run with Illegal class access : (…)� Step 1: Are you in the right place? Yes Step 2: Describe your environment Android device: SM-G386T1, SM-N910T, Galaxy S3 (, and possibly more) Android OS version: API 18, 19 Google Play Services version: 11.8.0 Firebase/Play Services SDK

Comments
  • What is the package of your class MyDisplay ?
  • Hi @Jorgesys. The namespace for MyDisplay is the same of the MainActivity where is inflated: package com.example.pc.easycalc; -- thanks
  • OK just define the complete package to your view <com.example.pc.easycalc.MyDisplay @boctulus
  • Thanks @"Tarlan Ahad" but I've already tried using: com.example.pc.easycalc.MyDisplay with the same result.