0

I have a custom component that actually wraps another component. Its layout is:

<AutoCompleteTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="@drawable/text_view_background" android:textCursorDrawable="@null" 
android:textColor="@android:color/black" android:inputType="textNoSuggestions"
android:paddingLeft="7dp"/>

In the component's code I'm trying to inflate it:

inflate(context,R.layout.results_auto_complete,this);
resultsAutoComplete=(AutoCompleteTextView)getChildAt(0);

But I'm getting a ClassCastException and it says the first child is a RelativeLayout! I traced all the children of this relative layout and it's actually the layout of the widget whose configuration activity contains my custom component! When I tested the component with a simple test activity everything worked!

So why is it happening and what can I do about this? Thanks.

user940016
  • 2,878
  • 7
  • 35
  • 56

2 Answers2

0

If your AutoCompleteTextView is a standalone xml file (your code is the root xml tag) of results_auto_complete.xml. It is the result of the inflation, NO need to use getChildAt(i).

if <AutoCompleteTextView> is a child element in an XML file, use should assign it an Id: android:id="@+id/your_view_id". Then after the inflation, use:

this.findViewById(R.your_view_id);

where this is current activity which loads the component view.

Update, try this:

LayoutInflater mInflater = (LayoutInflater)etContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
resultsAutoComplete=(AutoCompleteTextView)mInflater.inflate(R.layout.your_view_id, this, true);
CuongKe
  • 55
  • 1
  • 6
  • It's the root element, and when I'm using the test activity it's indeed the result of the inflation and I don't need to call `getChildAt`. I added it just because of the strange result I'm getting when the component is in the configuration activity of a widget. – user940016 Nov 02 '12 at 08:00
  • Sorry, it didn't work. The `inflate` function returns my component (which is expected because according to the documentation, it returns the root view group that's supplied to the function, if supplied), and its first child is again the root of the app widget... – user940016 Nov 02 '12 at 09:20
  • Can you provide the nested xml showing components you used within xml file. – CuongKe Nov 02 '12 at 09:23
  • It's what I wrote in my original message. The layout only includes the AutoCompleteTextView. – user940016 Nov 02 '12 at 10:15
0

It seems the problem is that I have 2 layouts with the same identifiers from different projects (one project is linked to the other), so when I'm trying to inflate a layout from one of the projects, I'm getting the layout with the same identifier in the other project. Anyway, thanks for trying to help.

user940016
  • 2,878
  • 7
  • 35
  • 56