简体   繁体   中英

Can't add EditText into Popup in android

I will show a popup screen for getting some description from the user. I can add almost every control (textview, button, checkbox, etc) without any problem except EditText. It gives me an error as below.

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

CODE SIDE

    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);
}

LAYOUT SIDE

<?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>

(There is no any exception if I use TextView or Button instead of EditText. What is the problem with EditText?)

The LayoutInflater does not make any assumptions about what thread it runs on. And nothing is mentioned about this in its documentation. Also its code seams to be thread-agnostic.

On the other hand, Views that are created by LayoutInflater might instantiate Handlers in their constructors. Well, they probably shouldn't do that, but there is no requirement for them to not create/use Handlers in their constructors.

Overall, I'd say that because there is no explicit requirement for Views to not use Handlers and Loopers in their constructors you can't assume inflating Views from non-UI thread is safe.

You can actually create HandlerThread and try inflating Views inside it. But I'd say this is very risky, as in Samsung Galaxy S example the view assumes that this thread will be alive during View lifetime and will process all messages using its Looper. Which might result in crash later on.

Resolved. I dont know why but you should create popup window in listener.

        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);
            }
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM