1

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.

3
  • What is the package of your class MyDisplay ? Commented Dec 21, 2017 at 23:29
  • Hi @Jorgesys. The namespace for MyDisplay is the same of the MainActivity where is inflated: package com.example.pc.easycalc; -- thanks Commented Dec 22, 2017 at 0:19
  • 1
    OK just define the complete package to your view <com.example.pc.easycalc.MyDisplay @boctulus Commented Dec 22, 2017 at 0:20

4 Answers 4

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

Call it using :

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

Comments

0

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

1 Comment

Thanks @"Tarlan Ahad" but I've already tried using: com.example.pc.easycalc.MyDisplay with the same result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.