android custom component to layout - android

I would like to add my custom ImageView to the xml layout.
main.xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.android.gag.TouchImageView
android:id="#+id/Image1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:clickable="true"
android:scaleType="center" />
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
TouchImageView, as its name implies, extends the ImageView class.
Main.java
touchImageView = (TouchImageView)findViewById(R.id.Image1);
My app crashes. Logcat output:
10-16 20:38:20.275: E/AndroidRuntime(11354): FATAL EXCEPTION: main
10-16 20:38:20.275: E/AndroidRuntime(11354): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.gag/com.android.gag.Main}: android.view.InflateException: Binary XML file line #6: Error inflating class com.android.gag.TouchImageView
10-16 20:38:20.275: E/AndroidRuntime(11354): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
idRuntime(11354): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class com.android.gag.TouchImageView
10-16 20:38:20.275: E/AndroidRuntime(11354): at android.view.LayoutInflater.createView(LayoutInflater.java:589)
10-16 20:38:20.275: E/AndroidRuntime(11354): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
Please, could anyone help me?
I am a beginner, so answers like '' replace "class library" with "Android library project"'' are pointless because they are too vague and I don't know where to start and where to go.
EDIT: Link to my TouchImageView class

The code for your custom View class is missing two constructors. From the Android docs for the View class:
View(Context context, AttributeSet attrs)
Constructor that is called when inflating a view from XML.
View(Context context, AttributeSet attrs, int defStyle)
Perform inflation from XML and apply a class-specific base style.
It's crashing because it can't find the constructor it needs, so it can't inflate the view.
Implement both of these constructors for your TouchImageView class and see if the problem goes away.

Related

Fragment testing error: android.view.InflateException: Binary XML file line #16: Binary XML file line #16: Error inflating class <unknown>

I am trying to test a fragment, following these instructions: https://developer.android.com/training/basics/fragments/testing
However I am getting the following crash when calling launchFragmentInContainer from my test.
Stacktrace:
android.view.InflateException: Binary XML file line #16: Binary XML file line #16: Error inflating class <unknown>
Caused by: android.view.InflateException: Binary XML file line #16: Error inflating class <unknown>
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
...
at android.view.LayoutInflater.$$robo$$android_view_LayoutInflater$createView(LayoutInflater.java:647)
...
at com.myapp.poll.PollHomeFragment.onCreateView(PollHomeFragment.kt:31)
...
Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 13: TypedValue{t=0x2/d=0x7f0301b1 a=-1}
at android.content.res.TypedArray.getDrawableForDensity(TypedArray.java:946)
at android.content.res.TypedArray.getDrawable(TypedArray.java:930)
at android.view.View.__constructor__(View.java:5010)
at android.view.View.<init>(View.java)
at android.widget.TextView.<init>(TextView.java)
...
at com.myapp.poll.PollHomeFragment.onCreateView(PollHomeFragment.kt:31)
Here's the layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".poll.PollActivity">
<TextView
android:id="#+id/tvEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="asdf#asdf.com" />
<TextView <!-- This is line 16 -->
android:id="#+id/tvViewPoll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:gravity="center"
android:minHeight="48dp"
android:text="View Poll" />
<TextView
android:id="#+id/tvCreatePoll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:gravity="center"
android:minHeight="48dp"
android:text="Create Poll" />
OK, from an educated guess based on the stacktrace, I tried removing android:background="?attr/selectableItemBackground" and this resolves the issue.
It appears that ?attr/selectableItemBackground is perhaps not compatible with FragmentScenario or this is a framework bug.
I submitted a bug on the issue tracker: https://issuetracker.google.com/issues/144629519
Update from Google:
Status: Won't Fix (Intended Behavior) The default theme does not have
an app defined attribute ?attr/selectableItemBackground (as Fragment
has no dependency on AppCompat, MDC, etc)
It sounds like you should be passing the correct theme to
FragmentScenario:
launchFragmentInContainer<PollHomeFragment>(themeResId = R.style.Theme_YourTheme)
In Java one also has to pass the theme to the FragmentScenario. Else the Fragment will not know about the current theme and will eventually complain that one should set an AppCompat theme ...
FragmentScenario<SomeFragment> scenario = FragmentScenario.launchInContainer(
SomeFragment.class,
Bundle.EMPTY,
R.style.Theme_Custom,
null
);
For me, using "android:?attr/selectableItemBackground" instead, solved the problem

java.lang.ClassNotFoundException: Didn't find class "androidx.core.view.ViewPager" on path

I just migrated my project to androidx and i am getting an inflation error for inflating viewpager component...I replaced android.support.v4.view.ViewPager with androidx.viewpager.widget.ViewPager
This my xml file
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:padding="#dimen/_1dp"
android:layout_height="wrap_content"
android:background="#color/banner_border_color">
<androidx.viewpager.widget.ViewPager
android:id="#+id/vp_slider"
android:layout_width="match_parent"
android:layout_height="#dimen/_140dp"
android:background="#drawable/thumb_new_background" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom">
<TextView
android:id="#+id/tv_banner_view_more"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="#dimen/_16dp"
android:background="#drawable/drawable_btn_curved"
android:paddingBottom="#dimen/_6dp"
android:paddingLeft="#dimen/_8dp"
android:paddingRight="#dimen/_8dp"
android:paddingTop="#dimen/_6dp"
android:visibility="gone"
android:text="#string/text_view_more"
android:textColor="#color/c_white"
android:textSize="#dimen/_8sp" />
</RelativeLayout>
</FrameLayout>
The Error i'm getting :
Caused by: android.view.InflateException: Binary XML file line #329: Binary XML file line #8: Error inflating class androidx.core.view.ViewPager
Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class androidx.core.view.ViewPager
Caused by: java.lang.ClassNotFoundException: Didn't find class "androidx.core.view.ViewPager" on path: DexPathList[
I figure out problem,
When we merger to androidx then android studio convert viewpager, swipe to refresh layout and some other views to androidx.core for example viewpager its looks like androidx.core.view.ViewPager but correct is androidx.viewpager.widget.ViewPager please check in your xml layouts and see there is right component is added or not if there is androidx.core then check for right one and replace it.
And also import dependency
implementation 'com.google.android.material:material:1.1.0-alpha08
You need to below dependencies in your build.gradle file
implementation 'com.google.android.material:material:1.1.0
You need to add below dependencies in your build.gradle file
implementation 'com.google.android.material:material:1.4.0
I think you need to Invalidate caches and restart
Go to File > Invalidate caches/Restart > ** Invalidate
Then rebuild project

How do i change custom view package name while that custom view xml file in the AAR file

i have aar file that contains drawable, layout and java file. One of xml file that contains Custom view and its class name is as per old project. so when i used in my new project then i get error of (i showed below)
Caused by: java.lang.ClassNotFoundException: Didn't find class "com.techreco.arcmenu.lib.AnimatedPathViewGroup" on path: DexPathList[[zip file "/data/app/com.example.pc28.arcmultiple-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.pc28.arcmultiple-2, /system/lib]]
Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class com.techreco.arcmenu.lib.AnimatedPathViewGroup
so how do i solve this?
My current project package name: com.example.pc28.arcmultiple
and Custom view that is inside AAR file and its package name i cannot access. That custom view is
<merge xmlns:android="http://schemas.android.com/apk/res/android" >
<com.techreco.arcmenu.lib.AnimatedPathViewGroup
android:id="#+id/animated_path"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
/>
<com.techreco.arcmenu.lib.ArcLayout
android:id="#+id/item_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
/> </merge>

View added from github library dont work

I wont to use nice Floating Action Button to my project. I want to use some libraries from github.
I like this FAB: https://github.com/Clans/FloatingActionButton .
I added dependences to gradle.app file:
compile 'com.github.clans:fab:1.6.2'
But my application does not work and crash.
XML:
<com.github.clans.fab.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/plus"
android:layout_gravity="bottom|right"
android:layout_marginBottom="8dp"
android:layout_marginRight="8dp"
app:fab_colorNormal="#DA4336"
app:fab_colorPressed="#E75043"
app:fab_colorRipple="#99FFFFFF"
app:fab_showShadow="true"
app:fab_shadowColor="#66000000"
app:fab_shadowRadius="4dp"
app:fab_shadowXOffset="1dp"
app:fab_shadowYOffset="3dp"
app:fab_size="normal"
/>
LogCat:
FATAL EXCEPTION: main
Process: com.poltavets.app.howtodraw, PID: 31836
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.poltavets.app.howtodraw/com.poltavets.app.howtodraw.view.HowTo}: android.view.InflateException: Binary XML file line #23: Error inflating class com.github.clans.fab.FloatingActionButton
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2462)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2524)
at android.app.ActivityThread.access$800(ActivityThread.java:167)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1419)
at android.os.Handler.dispatchMessage(Handler.java:111)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5546)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:964)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759)
Caused by: android.view.InflateException: Binary XML file line #23: Error inflating class com.github.clans.fab.FloatingActionButton
at android.view.LayoutInflater.createView(LayoutInflater.java:637)
Line 23 of xml is:
<com.github.clans.fab.FloatingActionButton
Java:
public class HowTo extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.howtodraw);
}
}
In ANDROID STUDIOS ITs works:
How i can fix it?
as per the library have you included line in your main parent layout ?
xmlns:fab="http://schemas.android.com/apk/res-auto"

use custom view from library project in my case

I have made an Android main library project, in which I have created a custom view class:
package com.my.android.library.layout;
public class CustomView extends View {
...
I also defined styleable for my CustomView:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="CustomView">
<attr name="title_text" format="string" />
</declare-styleable>
</resources>
Since I don't want to share my source code to others who are using my library project, so I created an distributed library project, in which I added the jar of above main library project to libs/ folder of distributed library project and copied all resource from main library project to distributed library project.
NEXT, I have made an android app project which uses the distributed library project. In the main layout file of app project, I defined the following :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.my.android.library.layout.CustomView
custom:title_text = "THIS IS TITLE"
/>
<RelativeLayout>
When I run my app, I got the following exception:
E/AndroidRuntime(30993): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.my.android.app/com.my.android.app.MainActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class com.my.android.library.layout.CustomView
E/AndroidRuntime(30993): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2071)
E/AndroidRuntime(30993): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2096)
...
Caused by: java.lang.ClassNotFoundException: com.my.android.library.layout.CustomView
E/AndroidRuntime( 2947): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
E/AndroidRuntime( 2947): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
E/AndroidRuntime( 2947): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
E/AndroidRuntime( 2947): at android.view.LayoutInflater.createView(LayoutInflater.java:552)
E/AndroidRuntime( 2947): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
It seems it can not inflate the my CustomView in layout xml. Why? How to get rid of it?
(I checked the main library jar file, there is CustomView class. Please don't just throw me a link of Android website without explanation.)
Since you are adding the View from a jar, you should be able to specify the namespace as:
xmlns:custom="http://schemas.android.com/apk/lib/root_package_name
Where "root_package_name" could be "com.my.android.library.layout" or "com.my.android.library" or something like that.
If that doesn't work, then it's probably and "Order and Export" problem or the way you are adding the library. Make sure the library is included properly:
Android Activity ClassNotFoundException - tried everything
And finally, if that also fails, then you might need to update Android SDK:
ClassNotFoundException: Didn't find class "com.google.android.gms.ads.AdView"
Let's try do it this way:
public CustomView(Context context) {
this(context, null, 0);
}
public CustomView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
//init your stuff here
}
Could you give us full stacktrace of the exception ?

Categories

Resources