Error inflating class on custom scrollview in XML? - android

I have an app that I am building using Mono Droid. I am trying to make an endless scrollview so that my users can scroll to the bottom and have more items loaded. Here is the class that I am using
public class EndlessScroll : ScrollView
{
public EndlessScroll (Context context) : base (context)
{}
public EndlessScroll(Context context, IAttributeSet attrs) : base(context, attrs)
{}
public EndlessScroll(Context context, IAttributeSet attrs, int defStyle) : base(context, attrs, defStyle)
{}
public interface OnScrollViewListener
{
void onScrollChanged(EndlessScroll v, int l, int t, int oldl, int oldt);
}
public OnScrollViewListener mOnScrollViewListener;
public void setOnScrollViewListener(OnScrollViewListener l)
{
this.mOnScrollViewListener = l;
}
protected void onScrollChanged(int l, int t, int oldl, int oldt)
{
mOnScrollViewListener.onScrollChanged(this, l, t, oldl, oldt);
base.OnScrollChanged(l, t, oldl, oldt);
}
}
Here is my xml file. I converted all this code from Java so there may be something that I am doing wrong.
<com.BirdsIView.BirdsIView.EndlessScroll
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="90"
android:id="#+id/scrollView">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/viewDebug" />
</com.BirdsIView.BirdsIView.EndlessScroll>
Here is my error log
Android.Views.InflateException: Binary XML file line #1: Error inflating class com.BirdsIView.BirdsIView.EndlessScroll
at at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () <IL 0x00011, 0x00068>
at Android.Runtime.JNIE nv.CallNonvirtualVoidMethod (intptr,intptr,intptr,Android.Runtime.JValue[]) [0x00084] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.18-series/3b7ef0a7/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.g.cs:896
at BirdsIView.getFromParse.OnCreate (Android.OS.Bundle) [0x00016] in c:\Users\New User\Desktop\BirdsIView\BirdsIView\BirdsIView\getFromParse.cs:40
at Android.App.Activity.n_OnCreate_Landroid_os_Bundle_ (intptr,intptr,intptr) [0x00011] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.18-series/3b7ef0a7/source/monodroid/src/Mono.Android/platforms/android-19/src/generated/Android.App.Activity.cs:2179
at at (wrapper dynamic-method) object.06c20e74-6eec-438c-a399-394abf9bcd74 (intptr,intptr,intptr)
at --- End of managed exception stack trace ---
at android.view.InflateException: Binary XML file line #1: Error inflating class com.BirdsIView.BirdsIView.EndlessScroll
at at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)

I got this to work. Apparently you need to name the xml like so
<BirdsIView.EndlessScroll
instead of
<com.BirdsIView.BirdsIView.EndlessScroll

Just need to change these functions simple
public EndlessScroll (Context context)
{
super(context);
init();
}
public EndlessScroll(Context context, AttributeSet attrs)
{
super(context, attrs);
init();
}
public EndlessScroll(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
init();
}

Related

HorizontalScrollView in RecycleView scrollRight at the beginning

I'm trying to use HorizontalScrollView in recycle view, so each item will have all his data inside, and i'm trying to start it all the way right. but not matter what i'm trying, i still see the left side of scroll view for few milliseconds before it scroll itself to end
my layout:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<com.cobox.core.ui.views.CustomHorizontalScollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/horizontal_scroll_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none">
<LinearLayout
android:id="#+id/view_home_card_and_button_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="100dp"
android:layout_height="150dp"
android:background="#color/amber_200" />
<android.support.v7.widget.CardView
android:id="#+id/cardView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/half_unit"
app:cardCornerRadius="4dp">
</android.support.v7.widget.CardView>
</LinearLayout>
</com.cobox.core.ui.views.CustomHorizontalScollView>
The scrollView:
public class CustomHorizontalScollView extends HorizontalScrollView {
public OnScrollChangedListener mOnScrollChangedListener;
public CustomHorizontalScollView(Context context) {
super(context);
}
public CustomHorizontalScollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomHorizontalScollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
if (mOnScrollChangedListener != null) {
mOnScrollChangedListener.onScrollChanged(l, t, oldl, oldt);
}
}
public void setOnScrollChangedListener(OnScrollChangedListener onScrollChangedListener) {
this.mOnScrollChangedListener = onScrollChangedListener;
}
public interface OnScrollChangedListener {
void onScrollChanged(int l, int t, int oldl, int oldt);
}
}
and the relevant part on code:
public class HomeScreenCardView extends FrameLayout implements
HomeCardActionsListener {
public HomeScreenCardView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.context = context;
init(attrs, defStyle);
}
...
private void init(AttributeSet attrs, int defStyle) {
WindowManager wm = ((Activity) context).getWindowManager();
Display d = wm.getDefaultDisplay();
ViewGroup.LayoutParams layoutParams2 = mCardView.getLayoutParams();
float dip = 8f;
float px = getPxFromDp(dip);
layoutParams2.width = (int) (d.getWidth() - px * 2);
mCardView.setLayoutParams(layoutParams2);
horizontalScrollView.post(new Runnable() {
#Override
public void run() {
horizontalScrollView.setSmoothScrollingEnabled(false);
horizontalScrollView.fullScroll(View.FOCUS_RIGHT);
horizontalScrollView.setSmoothScrollingEnabled(true);
}
});
}
try
In your java code
horizontalScrollView.setLayoutDirection(View.FOCUS_RIGHT);
and in XML
android:layoutDirection="rtl"
Replace your code of scrolling with code below
ViewTreeObserver vto = horizontalScrollView.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener(){
#Override
public void onGlobalLayout() {
horizontalScrollView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
horizontalScrollView.setSmoothScrollingEnabled(false);
horizontalScrollView.scrollTo(horizontalScrollView.getWidth(), 0);
horizontalScrollView.setSmoothScrollingEnabled(false);
}
});

exception in list view in android

I added a header to a list view
in the past When scrolling up or down throw exception
That was solved with a custom view list:
public MyFixedListView(Context context) {
super(context);
}
public MyFixedListView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyFixedListView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public MyFixedListView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
#Override
protected void dispatchDraw(Canvas canvas) {
try {
super.dispatchDraw(canvas);
} catch (IndexOutOfBoundsException e) {
e.printStackTrace();
}
}
But now the Android 6.0.1 (marshmallow) , once again, the show was former error:
in my stack trase :
java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 : at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
add header :
private void Create_Header_Sjd_init() {
LayoutInflater inflater = (LayoutInflater)getSystemService(LAYOUT_INFLATER_SERVICE);
convertViewHeader = inflater.inflate(R.layout.row_idea_list , null);
convertViewHeader.setBackgroundColor(getResources().getColor(R.color.gray_light_light));
listView.addHeaderView(convertViewHeader, null, false);
}
resovle this error by sub code :
listView.setFooterDividersEnabled(false);
listView.addFooterView(new View(this), null, false);
IndexOutOfBoundsException,maybe you can check you code about list of data or item view
If your list size id Zero then for adding header to list view you have set this..
listView.setAdapter(null);

How to apply a custom view in Xamarin .axml for Android

I've been working on implementing the fading action bar and parallax scrolling into my Android UI in my Xamarin app. I needed to extend my ScrollView, and now it's called a NotifyingScrollView. How do i style my .axml now. I can't just write NotifyingScrollView in my axml,
namespace NightOut.UiComponents
{
class NotifyingScrollView : ScrollView
{
public interface IOnScrollChangedListener
{
void OnScrollChanged(ScrollView who, int l, int t, int oldl, int oldt);
}
private IOnScrollChangedListener mOnScrollChangedListener { get; set; }
public NotifyingScrollView(Context context) : base(context)
{
}
public NotifyingScrollView(Context context, IAttributeSet attrs) : base(context, attrs)
{
}
public NotifyingScrollView(Context context, IAttributeSet attrs, int defStyle) : base(context, attrs, defStyle)
{
}
[Android.Runtime.Register("onScrollChanged", "(IIII)V", "GetOnScrollChanged_IIIIHandler")]
protected override void OnScrollChanged(int l, int t, int oldl, int oldt)
{
base.OnScrollChanged(l, t, oldl, oldt);
if (mOnScrollChangedListener != null)
{
mOnScrollChangedListener.OnScrollChanged(this, l, t, oldl, oldt);
}
}
}
}
Generic example:
namespace My.Namespace
{
class MyCustomView : View
{
}
}
Is used in following way in .axml
<my.namespace.MyCustomView />
Or in your specific example:
<nightout.uicomponents.NotifyingScrollView />
Do mind case, namespace in .axml is lowercase, classname is same case as C# declaration.

Extending view group gives childcount 0?

I am extending a frame layout
and when in the constructor (after it loaded the view from xml) i call getChildCount()
I get 0. How to fix this ?
public class DisabledFrameLayout extends FrameLayout {
public DisabledFrameLayout(Context context) {
super(context);
init(context, null,0);
}
public DisabledFrameLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context,attrs,defStyle);
}
public DisabledFrameLayout(Context context, AttributeSet attrs) {
super(context, attrs);
init(context,attrs,0);
}
private void init(Context context, AttributeSet attrs, int defStyle) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DisabledFrameLayout, 0, 0);
if(a.hasValue(R.styleable.DisabledFrameLayout_disable_descendants)) {
boolean disable = a.getBoolean(R.styleable.DisabledFrameLayout_disable_descendants, false);
if(disable) {
disableDescendants(this);
}
}
a.recycle();
}
private void disableDescendants(ViewGroup v) {
for (int i=0; i<v.getChildCount();i++) {
if(v.getChildAt(i) instanceof ViewGroup) {
disableDescendants((ViewGroup)v.getChildAt(i));
}
v.setEnabled(false);
v.setFocusable(false);
v.setFocusableInTouchMode(false);
}
}
}
xml
<com.lenabru.DisabledFrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:disable_descendants="true"
>
<include
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
layout="#layout/fragment_p" />
</com.lenabru.DisabledFrameLayout>
In state of constructing your FrameLayout, children are still not fully attached to the view. So, calling getChildCount() will return you 0.
If you want to iterate over child views and update them, do it inside onLayout() or onMeasure().
Refs:
http://blog.denevell.org/android-custom-views-onlayout-onmeasure.html
ViewGroup - check this example code.

Android: inflated view do not show in customed ViewGroup

I made a customed view by extending ViewGroup, my problems is:
when adding a child( addView(chileView) ) view into ViewGroup, if the child view is a inflated view, the child view will not show. but when the child view is hard-code view( like, btn = new Button(ctx) ), it will show!
The Code, not show:
public class EdgeSwiper extends ViewGroup{
public EdgeSwiper(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
public EdgeSwiper(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public EdgeSwiper(Context context) {
super(context);
init(context);
}
View v;
private void init(Context context) {
v = View.inflate(context,R.layout.aview,null);
this.addView(v);
}
#Override
protected void onLayout(boolean arg0, int a, int b, int c, int d) {
v.layout(a, b, c, d);
}
}

Categories

Resources