0

I Basicly have code

new AsyncTask<Integer, Void, View>() {
@Override
protected View doInBackground(Integer... tabIds) {
    return mInflater.inflate(R.layout.layout_name, null);
}
@Override
protected void onPostExecute(View result) {
    super.onPostExecute(result);
            root_layout.addView(result);
}
}.execute(tabId);

layout_name.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@android:color/white" >


<EditText
    android:id="@+id/text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:inputType="text" />

<Button
    android:id="@+id/do_stuff"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

</LinearLayout>

with that i get InflateException: Binary XML file line #x: Error inflating class <unknown>. if i remove EditText it works with no problems.

I cant find the reason for it (spent 2h on it).

anyone know the reason for this? and hopefully a solution

  • 1
    Inflating a View on a worker thread is not the best idea, why do you need that? – Egor Jan 10 '13 at 12:01
  • 1
    because it takes a while for everything to load. even after i get what i need from the server – user1838668 Jan 10 '13 at 12:07
  • Anyway, this won't work because the Android UI framework is not thread safe. – Egor Jan 10 '13 at 12:17
  • found this one http://stackoverflow.com/questions/6691311/inflate-a-view-in-a-background-thread/6692007#6692007 guess ill have to limit how much to show instead – user1838668 Jan 10 '13 at 13:13

2 Answers2

1

Instead of Using AsynTask use AsyncLayoutInflater for example :

  MyClass extends Fragment implements  AsyncLayoutInflater.OnInflateFinishedListener {

        @Override
        public void onCreate(Bundle savedInstanceState) {
         new AsyncLayoutInflater(context).inflate(childViewId, parent, this);
         //show progress..
        }
        @Override
        public void onInflateFinished(View view, int resid, ViewGroup parent){
            if (view != null) {
             //You have inflated view to use in UI Thread.
            }
        }
    }

Besides this it has some pitfalls: AsyncLayoutInflater class will also handle cases for views that cannot be inflated asynchronously. Such view classes has has no handler implementation and will throw exception if you try to do inflate them in AsyncTask or Thread class (As in your case). For example EditText can be inflated in UI thread only. Will ask for looper.loop(); call. AsyncLayoutInflater will inflate such views in UI thread and rest asynchronously.

Ultimately you cannot inflate EditText in seprate thread. This is Just a workaround for what you are trying to achieve. Also, you can try HandlerThread for the same but won't do much good.

Jayant Arora
  • 1,241
  • 2
  • 15
  • 24
0

change both

 android:layout_width="match_parent"

to

 android:layout_width="wrap_content"

it might help you.

Nirav Tukadiya
  • 3,367
  • 1
  • 18
  • 36