0

No, no one answered that question, and the problem still remains... This question here is about another symptom to the same problem (please see comments below):


In Monodroid atleast, when inflating a custom view from a layout, sometimes it needs to be wrapped in a ViewGroup (ie, LinearLayout) in order to not get an exception, and other times does not.

I was wondering if anyone is familiar with this situation, and if it happens in "raw" Android as well (ie, no Monodroid) ?

I always first try without, as in

TextView1.axml

<?xml version="1.0" encoding="utf-8"?>
<Monodroid.Activity1.TextView1
  android:id="@+id/text_view1"
  android:layout_width="300dp"
  android:layout_height="50dp"/>

but if I get an inflation exception, then I'll have to wrap it up

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/ll_container"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">

  <Monodroid.Activity1.TextView1
    android:id="@+id/text_view1"
    android:layout_width="300dp"
    android:layout_height="50dp"/>

</LinearLayout>

where

public class TextView1 : TextView
{
    public TextView1 (Context context) : base(context) { }
    public TextView1 (Context context, IAttributeSet attributes) : base(context, attributes) { }
    public TextView1 (Context context, IAttributeSet attributes, int defStyle) : base(context, attributes, defStyle) { }
}

Thank you.


This layout file inflates with no containing viewgroup:

<?xml version="1.0" encoding="utf-8"?>
<fieldinspection.droid.views.custom.FieldInput
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/RecordDataFieldInput"
  style="@style/FieldInput"
  android:layout_marginRight="0dip"/>

and this one (inner class PagedFragmentFieldInput) does not (it needs to be within a LinearLayout or else inflation exception):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/ll_record_data_field_input2_container"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"  >

  <FieldInspection.Droid.Views.ComplaintView.PagedFragmentRecordDataFieldBox.PagedFragmentFieldInput
    android:id="@+id/RecordDataFieldInput"
    style="@style/FieldInput"
    android:layout_marginRight="0dip"/>

</LinearLayout>

Its read as PagedFragment-RecordDataFieldBox, its a RecordDataFieldBox thats within a Fragment thats within a ViewPager.

samus
  • 6,102
  • 6
  • 31
  • 69
  • Isn't this the same question as your previous: http://stackoverflow.com/questions/15092785/single-view-layout-files-does-compiler-autowrap-with-layout-viewgroup – Cheesebaron Jun 30 '13 at 22:06
  • @Cheesebaron No, they're not the same question. They each have they're own angle on the same problem of custom view inflation. The old question asks if the **compiler auto-wraps a "naked" custom view in a viewgroup during inflation**, and the second is asking why only **sometimes a custom view needs to be nested within a viewgroup**, and other times not, for inflation to not throw exceptions. – samus Jul 01 '13 at 01:12
  • @Cheesebaron If you could please have a look at my answer to stackoverflow.com/questions/15161261/viewpager-focus-issue/… and look for the layout file PagedFragmentRecordNoteBoxInput.axml, do you have any idea why I need a LinearLayout around it, but not for RecordNoteBoxInput.axml ? – samus Jul 02 '13 at 01:18
  • Can you provide the entire stack trace from your inflation? – Cheesebaron Jul 02 '13 at 19:44
  • @Cheesebaron I updated the post by adding an answer that explains the error condition(search the link for your handle "Cheesbaron"), and included a stacktrace of the exception as well (thanks man) http://stackoverflow.com/questions/15161261/viewpager-focus-issue/17435410#17435410 – samus Jul 02 '13 at 21:20

1 Answers1

1

I took your first sample and tried it out here. I get no error wrapping it or not.

TextViewInherit.cs:

using Android.Content;
using Android.Util;
using Android.Widget;

namespace InflationShiz
{
    public class TextViewInherit : TextView
    {
        public TextViewInherit(Context context, IAttributeSet attrs) :
            this(context, attrs, 0)
        {
        }

        public TextViewInherit(Context context, IAttributeSet attrs, int defStyle) :
            base(context, attrs, defStyle)
        {
        }
    }
}

One.axml:

<?xml version="1.0" encoding="utf-8"?>
<inflationshiz.TextViewInherit
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

Two.axml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <inflationshiz.TextViewInherit
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
</LinearLayout>

Both work when I inflate in my Activity like so:

var one = LayoutInflater.Inflate(Resource.Layout.One, null);
var two = LayoutInflater.Inflate(Resource.Layout.Two, null);

I find it hard to reproduce your issue; Your code is scattered over 3 different SO questions and even more scattered because you have created answers to your own question where you try to elaborate on your initial questions.

Cheesebaron
  • 24,131
  • 15
  • 66
  • 118
  • Interesting, are you using Mono ? I'm almost certain its happening during some translation phase. – samus Jul 02 '13 at 21:56
  • Yes. I've tried both with Xamarin.Android 4.7.8 and 4.7.11 both worked just fine. – Cheesebaron Jul 02 '13 at 22:05
  • Oh, yeah, about that, these inflation issues arise in quite a few places. This basic example suffices as a model problem, however my most recent issue with the EditText's loosing focus has been solved, and the solution provided contained yet another instance of this inflation issue. I figured to illustrate the issue in an actual working piece of code, rather than a example model. – samus Jul 02 '13 at 22:07
  • Thanks for verifying at leat. Well, I'm using a viewPager and Fragments, so that could make a difference. – samus Jul 02 '13 at 22:09
  • I really doubt that is the issue. However what I think you are doing wrong is to create a `View`, inside of that nesting inflations of other `View`s in the ctors. This means the `View` you are using is not done initializing; In that state you are trying to inflate additional `View`s into it. Not really the ideal way to do it, growing the View Hierarchy and causing troubles that way. – Cheesebaron Jul 02 '13 at 22:12
  • Do you know of any strategies for effectively debugging Mono/.NET and Adroid/Java to obtain more details. Its seems that "android.view.InflateException: Binary XML file line #5: Error inflating class x.y.z" is a bit too vague. I'm aware of some debugging environment variables and system properties that can be set via adb in, but was wondering if there any other ways to get the exact java exception message (it appears that mono is eating it, and rethrowing or something). – samus Jul 02 '13 at 22:16
  • Check what logcat is saying. – Cheesebaron Jul 02 '13 at 22:17
  • Ohh man, I will take a look and analyze my view construction. Thanks! – samus Jul 02 '13 at 22:18
  • Logcat runs 24/7 - its got its own monitor ! – samus Jul 02 '13 at 22:18
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/32783/discussion-between-cheesebaron-and-samus-arin) – Cheesebaron Jul 02 '13 at 22:18
  • I'm getting web-blocked from discussion :( – samus Jul 02 '13 at 22:20
  • Hopping on different subnet – samus Jul 02 '13 at 22:25
  • 4.0.3 is super old. You should really upgrade! – Cheesebaron Jul 02 '13 at 22:25
  • ugh, I got to chat, but virtual keyboard not working on kindle fire 4.2.2 – samus Jul 02 '13 at 22:29