0

我将显示一个弹出屏幕,用于从用户那里获取一些描述。除了EditText,我几乎可以毫无问题地添加所有控件(文本视图、按钮、复选框等)。它给了我一个错误,如下所示。

Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class <unknown> 

代码端

    public RouteAdapter(Activity a, RouteResultDto list, boolean editMode, BaseFollower userInfo, boolean isFavoriteStatus) {
    this.activity = a;
    this.routeList=list;
    this.editMode = editMode;
    this.userInfo = userInfo;
    this.isFavorite = isFavoriteStatus;
    inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    imageLoader=new ImageLoader(activity.getApplicationContext());

//throws the exception at line below
    lnPopupMain = (LinearLayout) LayoutInflater.from(activity).inflate(R.layout.popup_route_edit, null);

    float width = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200, activity.getResources().getDisplayMetrics());
    float height = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 250, activity.getResources().getDisplayMetrics());
    popupEditRoute = new PopupWindow(lnPopupMain, (int)width, (int)height, true);

    popupEditRoute.setBackgroundDrawable(new BitmapDrawable());
    popupEditRoute.setOutsideTouchable(true);
}

布局面

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:padding="10dip"
              android:layout_width="200dip"
              android:layout_height="160dip"
              android:background="@color/black"
              android:id="@+id/lnPopupMain"
              android:focusable="true"
        >

    <EditText android:layout_width="match_parent"
              android:layout_height="80dip"
              android:id="@+id/txtRouteEdit"
            android:background="@color/white">
    </EditText>
</LinearLayout>

(如果我使用 TextView 或 Button 而不是 EditText,也不例外。EditText 有什么问题?)

4

2 回答 2

0

LayoutInflater 不对它运行的线程做任何假设。在其文档中没有提到这一点。它的代码接缝也与线程无关。

另一方面,由 LayoutInflater 创建的视图可能会在其构造函数中实例化处理程序。好吧,他们可能不应该那样做,但是没有要求他们不在构造函数中创建/使用处理程序。

总的来说,我想说的是,因为没有明确要求视图在其构造函数中不使用处理程序和 Loopers,所以你不能假设从非 UI 线程膨胀视图是安全的。

您实际上可以创建HandlerThread并尝试在其中膨胀视图。但我会说这是非常冒险的,因为在三星 Galaxy S 示例中,视图假定该线程在视图生命周期内将处于活动状态,并将使用其 Looper 处理所有消息。这可能会导致以后崩溃。

于 2012-12-06T13:14:36.797 回答
0

解决。我不知道为什么,但你应该在监听器中创建弹出窗口。

        imgEditOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                lnPopupMain = (LinearLayout)inflater.inflate(R.layout.popup_route_edit, null);
                float width = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200, activity.getResources().getDisplayMetrics());
                float height = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 250, activity.getResources().getDisplayMetrics());
                popupEditRoute = new PopupWindow(lnPopupMain, (int)width, (int)height, true);
                popupEditRoute.setBackgroundDrawable(new BitmapDrawable());
                popupEditRoute.setOutsideTouchable(true);
                popupEditRoute.showAtLocation(lnPopupMain, Gravity.CENTER, 10, 10);
            }
        });
于 2012-12-06T13:17:13.947 回答