Reputation: 395
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.
Upvotes: 1
Views: 4439
Reputation: 2643
Call it using :
<com.example.pc.easycalc.MyDisplay
android:id="@+id/resDisplay"
android:textSize="60sp"
android:text="0.0000000"
style="@style/DisplayStyle"
/>
Upvotes: 1
Reputation: 370
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
Upvotes: 0
Reputation: 126445
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"
/>
Upvotes: 1
Reputation: 2019
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.
Upvotes: 1