Use generics in databinding's xml - android-databinding

<layout 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"
tools:context="com.ihu.intelligentmedicinechest.activity.MainActivity">
<data>
<variable
name="items"
type="java.util.Collection<? extends com.ihu.intelligentmedicinechest.adapter.binder.ItemBinder>"/>
</data>
</layout>
Use generics in data binding XML is wrong. Follow is the exception:
android.databinding.tool.util.LoggedErrorException: Found data binding errors.
****/ data binding error ****msg:cannot find type argument for ?extendscom.tvvbbb.adapter.binder.ItemBinder in java.util.Collection
loc:56:30 - 56:34
****\ data binding error ****
How can I process it?

Related

emre1512 Noty RelativeLayout creation in Kotlin

I have a simple MainActivity with a simple main layout like this:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
... />
</androidx.constraintlayout.widget.ConstraintLayout>
I need to activate Noty on this activity. What I need to know is how to create RelativeLayout "yourLayout" in its simple exmple in Kotlin.
Noty.init(YourActivity.this, "Your warning message", yourLayout, Noty.WarningStyle.SIMPLE).show();
Thank you!
Add an android:id attribute to your ConstraintLayout tag:
android:id="#+id/root"
Now you can fetch a reference to that ConstraintLayout in code:
val root: ConstraintLayout = findViewById(R.id.root)
Noty.init(this#MainActivity, "Your warning message", root, Noty.WarningStyle.SIMPLE).show()

App crash caused by data binding related to java.lang.Runnable android.databinding.ViewDataBinding.mRebindRunnable

The exception is the following:
java.lang.NullPointerException: Attempt to read from field 'java.lang.Runnable android.databinding.ViewDataBinding.mRebindRunnable' on a null object reference
at android.databinding.ViewDataBinding.access$100(ViewDataBinding.java:51)
at android.databinding.ViewDataBinding$5.onViewAttachedToWindow(ViewDataBinding.java:142)
at android.view.View.dispatchAttachedToWindow(View.java:14525)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2836)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2843)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2843)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2843)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2843)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2843)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2843)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2843)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1364)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Let me try my best to make the description brief and clear.
First, this is related to a layout added dynamically. The activity's data binding works perfectly without any problem.
Here is the layout foo_view:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="foo"
type="net.mydomain.myapp.MyActivity.Foo" />
<import type="android.view.View" />
</data>
<!--For the sake of debugging, no data binding is actually used-->
...
</layout>
In MyActivity.java:
public class MyActivity extends AppCompatActivity {
public class Foo{
...
}
...
addFoo(){
FooViewBinding binding = DataBindingUtil.inflate(LayoutInflater.from(this), R.layout.foo_view, myGridLayout, true);
Foo foo = new Foo();
binding.setFoo(foo);
}
}
The app cashes immediately upon the start of MyActivity. The crash is not caused by a specific line of code.
Could anyone shed some light on this?
[Edit] 2016-06-30
The crash happens on a Lollipop, but same code works fine on a Jelly Bean
Just in case someone has the same problem, this errors occurs when you change the tag (i.e. view.setTag(object)) of a data-binding enabled view, as the data binding library uses the tag for itself.
in my case,
If I use <androidx.constraintlayout.widget.Group/> in 'merge'it gives an error on some screens.
<merge tools:parentTag="androidx.constraintlayout.widget.ConstraintLayout">
<androidx.constraintlayout.widget.Group
android:id="#+id/constraint_group_active"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="#{data != null}"
app:constraint_referenced_ids="txt_offering_slogan,view_extra_gift,iv_offering,txt_offering_title,txt_offering_subtitle,txt_buy_extra_gift"
tools:visibility="visible" />
</merge>

Gradle flavors, applicationId and not resolved custom view attributes

I have a custom view based on GridLayout:
package com.mycompany.myapp;
...
public class GridLayoutForceSizeCells extends GridLayout
{
}
I have defined an xml with custom attributes for this class:
<?xml version="1.0" encoding="utf-8" ?>
<resources>
<declare-styleable name="GridLayoutForceSizeCells">
<attr name="totalImages" format="integer" />
<attr name="desiredColumnCount" format="integer" />
<attr name="spacing" format="dimension" />
<attr name="autoGenerate" format="boolean" />
</declare-styleable>
</resources>
Then, I have a layout using this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:wheel="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.mycompany.myapp.GridLayoutForceSizeCells
xmlns:grid="http://schemas.android.com/apk/res/com.mycompany.myapp"
android:padding="3dp"
android:id="#+id/cellsGrid"
android:layout_width="match_parent"
android:layout_height="match_parent"
grid:desiredColumnCount="20"
grid:totalImages="0"
grid:spacing="1dp"
android:orientation="horizontal"/>
</LinearLayout>
The problem I am encountering is that when I change applicationId through a gradle flavor custom attributes are not resolved. I have read in gradlew help that the application package is the one used for R when it is decoupled using gradlew applicationId entry. So, applicationId should not affect my custom attributes package. But when I set applicationId to anything different from com.mycompany.myapp an error arises claiming that attributes can't be resolved.
Has anybody found a solution to this?
Cheers.
Finally fix it using xmlns:grid="http://schemas.android.com/apk/res-auto" to let it resolve the package for me in the layout file instead of the full package name.

Adding android:layoutAnimation to a LinearLayout causes FC

I have the following XML in menu.xml, it's a LinearLayout that I need to animate, so I use the layoutAnimation property. Without this property the layout shows flawlesly, but with this property set I get a nasty forceclose and I don't understand why:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/bkgrnd"
android:layoutAnimation="#anim/menu_anim" <=== adding this results in FC
...etc...
anim/menu_anim.xml:
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="500">
</alpha>
</set>
Help please! Thanks!
You cant add an animation directly to a layout. you have to create one more xml file in your anim folder which points to the animation xml (menu_anim) as below.
<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
android:delay="30%"
android:animation="#anim/menu_anim"
/>
lets call the above xml as anim_controller.xml
now in your linear layout use
android:layoutAnimation="#anim/anim_controller"

Why is my WCF Data Services method not appearing in the OData collections list?

When I view the root of my WCF Data Services service (http://localhost/MyService.svc/) in a browser I see this:
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<service xml:base="http://localhost/MyService.svc/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:app="http://www.w3.org/2007/app" xmlns="http://www.w3.org/2007/app">
<workspace>
<atom:title>Default</atom:title>
</workspace>
</service>
I would expect to see a list of collections.
When I go to the $metadata URL I see this:
<?xml version="1.0" encoding="iso-8859-1" standalone="yes"?>
<edmx:Edmx Version="1.0" xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx">
<edmx:DataServices xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" m:DataServiceVersion="1.0">
<Schema Namespace="MyApp" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://schemas.microsoft.com/ado/2007/05/edm">
<ComplexType Name="Package">
<Property Name="Id" Type="Edm.String" Nullable="true" />
</ComplexType>
</Schema>
<Schema Namespace="MyApp" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://schemas.microsoft.com/ado/2007/05/edm">
<EntityContainer Name="PackageService" m:IsDefaultEntityContainer="true">
<FunctionImport Name="GetQueryablePackages" ReturnType="Collection(MyApp.Package)" m:HttpMethod="GET" />
</EntityContainer>
</Schema>
</edmx:DataServices>
</edmx:Edmx>
Why might my GetQueryablePackages collection not be appearing?
I'm using these access settings:
config.SetEntitySetAccessRule("*", EntitySetRights.AllRead);
config.SetServiceOperationAccessRule("*", ServiceOperationRights.All);
Service operations (the function import in the EDM) is not exposed in the service document. Only entity sets are exposed there.
If you want your data to be exposed in the service document make an entity set out of it. Depending on the provider model this differs. Typically it means exposing a property of type IQueryable on your context class. Note that T has to be an entity type (must have a key).
Can you share the context definition where you have defined the IQueryable <> properties. There are 2 things that come to my mind: First the properties must be of type IQueryable<> or some type that derives from it. Second, the element type refered by the IQueryable<> must be an entity type i.e. they must have key properties declared in them.
Hope this helps.
Thanks
Pratik
Or you can create an extension method like this:
public static class TestEntitiesExtensions
{
public static IEnumerable<Package> GetQueryablePackages(this TestEntities context)
{
var uri = new Uri(context.BaseUri, "GetQueryablePackages");
return context.Execute<Package>(uri);
}
}