Android EditText上的AsyncTask LayoutFlater充气异常
我基本上有密码Android EditText上的AsyncTask LayoutFlater充气异常,android,android-asynctask,android-edittext,layout-inflater,inflate-exception,Android,Android Asynctask,Android Edittext,Layout Inflater,Inflate Exception,我基本上有密码 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.onPostExecut
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);
newasynctask(){
@凌驾
受保护的视图doInBackground(整数…选项卡ID){
返回mInflater.flate(R.layout.layou\u name,空);
}
@凌驾
受保护的void onPostExecute(查看结果){
super.onPostExecute(结果);
root_layout.addView(结果);
}
}.执行(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>
这样我就得到了InflateException:Binary-XML文件行#x:Error-inflating类
。
如果我删除EditText
,它不会出现问题
我找不到原因(花了2小时)
有人知道原因吗?
希望有一个解决方案能改变这两者
android:layout_width="match_parent"
到
它可能会帮助您。使用AsyntTask而不是AsyntTask,例如:
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.
}
}
}
除此之外,它还有一些陷阱:
AsyncLayoutFlater类还将处理不能异步膨胀的视图的情况。这样的视图类并没有处理程序实现,若您试图在AsyncTask或Thread类中对它们进行膨胀(如您的情况),则会引发异常。例如,EditText只能在UI线程中膨胀。将请求looper.loop();呼叫
AsyncLayoutFlater将在UI线程中膨胀这样的视图,并异步休息
最终,您无法在seprate线程中膨胀EditText。这只是一个解决你想要达到的目标的方法。
同样,您也可以尝试HandlerThread,但效果不太好。在工作线程上扩展视图不是最好的主意,为什么需要它?因为加载所有内容需要一段时间。即使在我从服务器上得到了我所需要的东西之后,这也不会起作用,因为Android UI框架不是线程安全的。找到了这一个,我猜我必须限制显示多少
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.
}
}
}