在PopupWindow中添加AutoCompleteTextView时应用程序崩溃了?

时间:2016-07-11 10:32:31

标签: android popupwindow autocompletetextview

您好我想在弹出窗口中添加autocompletetextview。这样,当我输入城市名称时,它应该作为建议。这可能在PopUpWindow中。正在尝试这个。

View addresspopupView =addresslayoutInflater.inflate(R.layout.addressinput, null);
     AutoCompleteTextView city = (AutoCompleteTextView) addresspopupView.findViewById(R.id.city);
        String[] CITY ={"Mumbai", "Ahemadabad", "Kolkata", "Chennai", "Thane"};
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_dropdown_item_1line, CITY);
        city.setAdapter(adapter);
    PopupWindow addresspopupWindow = new PopupWindow(addresspopupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
                addresspopupWindow.showAtLocation(addresspopupView, Gravity.CENTER, 0, 0);
  

logcat的

FATAL EXCEPTION: main
android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRootImpl$W@424c2b38 is not valid; is your activity running?
at android.view.ViewRootImpl.setView(ViewRootImpl.java:706)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:356)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:234)
at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:153)
at android.view.Window$LocalWindowManager.addView(Window.java:559)
at android.widget.PopupWindow.invokePopup(PopupWindow.java:1013)
at android.widget.PopupWindow.showAsDropDown(PopupWindow.java:922)
at android.widget.ListPopupWindow.show(ListPopupWindow.java:595)
at android.widget.AutoCompleteTextView.showDropDown(AutoCompleteTextView.java:1093)
at android.widget.AutoCompleteTextView.updateDropDownForFilter(AutoCompleteTextView.java:956)
at android.widget.AutoCompleteTextView.onFilterComplete(AutoCompleteTextView.java:938)
at android.widget.Filter$ResultsHandler.handleMessage(Filter.java:285)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4921)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(Native Method)

这是我的addressInput.xml

<LinearLayout 
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:elevation="@dimen/dimen_4dp"
android:orientation="vertical"
android:padding="@dimen/dimen_4dp">


<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/addressPopUptitle"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:gravity="center_horizontal"/>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <EditText
        android:id="@+id/addressLine1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginRight="2dp"
        android:layout_weight="1"
        android:hint="Address Line 1" />


    <EditText
        android:id="@+id/addressLine2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:hint="Address Line 2" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <EditText
        android:id="@+id/landMark"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginRight="2dp"
        android:layout_weight="1"
        android:hint="Landmark" />


    <EditText
        android:id="@+id/locality"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:hint="Locality" />

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <!--<EditText-->
        <!--android:id="@+id/city"-->
        <!--android:layout_width="0dp"-->
        <!--android:layout_height="wrap_content"-->
        <!--android:layout_marginRight="2dp"-->
        <!--android:layout_weight="1"-->
        <!--android:hint="City" />-->

    <AutoCompleteTextView
        android:id="@+id/city"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginRight="2dp"
        android:layout_weight="1"
        android:hint="City" />

    <EditText
        android:id="@+id/pin"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:hint="PIN" />

</LinearLayout>

<EditText
    android:id="@+id/state"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="State" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <Button
        android:id="@+id/summitAddrPopUp"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="SUMMIT"
        android:textColor="@color/green" />

    <Button
        android:id="@+id/cancelAddrPopUp"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="CANCEL"
        android:textColor="@color/red" />


</LinearLayout>

OnCreateView:

  @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    Log.d("ClientBookingScreen", "Inside OnCreateView of ClientBookingScreen");
    rootView = inflater.inflate(R.layout.bookingrequest, container, false);
    intialize(rootView);
    intialSetUp();
    loadconfigs();
    initializeAddressPopUp();
    settingClickListener();
    return rootView;
}

onCreateView()中的方法:

private void intialize(View rootView) {
    //Setting toolbar
    myCalendar = Calendar.getInstance(TimeZone.getTimeZone("Asia/Calcutta"));
    pickUpDate = (TextView) rootView.findViewById(R.id.pickUpDateInput);
    pickUpTime = (TextView) rootView.findViewById(R.id.pickUpTimeInput);
    returnDate = (TextView) rootView.findViewById(R.id.returnDateInput);
    returnTime = (TextView) rootView.findViewById(R.id.returnTimeInput);
    pickUpAddress = (TextView) rootView.findViewById(R.id.pickUpAddressInput);
    pickUpAddress.setMovementMethod(new ScrollingMovementMethod());
    returnAddress = (TextView) rootView.findViewById(R.id.returnAddressInput);
    returnAddress.setMovementMethod(new ScrollingMovementMethod());
    description = (EditText) rootView.findViewById(R.id.descriptionInput);
    description.setMovementMethod(new ScrollingMovementMethod());
    book = (Button) rootView.findViewById(R.id.bookButton);

    //Spinners
    dutyType = (Spinner) rootView.findViewById(R.id.dutyTypeInput);
    vehicleCategory = (Spinner) rootView.findViewById(R.id.vehicleCategoryInput);
    vehicleType = (Spinner) rootView.findViewById(R.id.vehicleTypeInput);

    //creating reflection of spinner to limit his height
    createReflectionOfSpinner(dutyType);
    createReflectionOfSpinner(vehicleCategory);
    createReflectionOfSpinner(vehicleType);
    isDrawerOpened = false;

    //CheckBox
    btcCheckBox = (CheckBox) rootView.findViewById(R.id.btccheckBox);
}

private void loadconfigs() {
    categoryMap = new HashMap<String, ArrayList<String>>();
    guestAddressMap = new HashMap<String, String>();
    guestaddressLabel = new ArrayList<String>();
    categoryName = new ArrayList<String>();
    dutyTypeName = new ArrayList<String>();
    new LoadConfigs().execute();
}

 private void initializeAddressPopUp() {
    //Address Input Popup
    addresslayoutInflater = (LayoutInflater) getActivity().getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    addresspopupView = addresslayoutInflater.inflate(R.layout.addressinput, null);
    addressLine1 = (EditText) addresspopupView.findViewById(R.id.addressLine1);
    addressLine2 = (EditText) addresspopupView.findViewById(R.id.addressLine2);
    landmark = (EditText) addresspopupView.findViewById(R.id.landMark);
    locality = (EditText) addresspopupView.findViewById(R.id.locality);
    city = (AutoCompleteTextView) addresspopupView.findViewById(R.id.city);
    String[] CITY ={"Mumbai", "Ahemadabad", "Kolkata", "Chennai", "Thane"};

// AutoCompleteTextView textView =(AutoCompleteTextView)findViewById(R.id.actv_country);         ArrayAdapter adapter = new ArrayAdapter(addresspopupView.getContext(),android.R.layout.simple_dropdown_item_1line,CITY);         city.setAdapter(适配器);

    pin = (EditText) addresspopupView.findViewById(R.id.pin);
    state = (EditText) addresspopupView.findViewById(R.id.state);
    title = (TextView) addresspopupView.findViewById(R.id.addressPopUptitle);
    addressSummit = (Button) addresspopupView.findViewById(R.id.summitAddrPopUp);
    addressCancel = (Button) addresspopupView.findViewById(R.id.cancelAddrPopUp);


    //AddressLabelList Popoup
    addressLabelInflater = (LayoutInflater) getActivity().getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    listOfaddressTitle = addressLabelInflater.inflate(R.layout.addresslabellist, null);
    addressLabelListView = (ListView) listOfaddressTitle.findViewById(R.id.addressLabelListView);
    selectedListok = (Button) listOfaddressTitle.findViewById(R.id.addressLabelListButton);
}

private void settingClickListener(){

    pickUpDate.setOnClickListener(this);
    pickUpTime.setOnClickListener(this);
    returnDate.setOnClickListener(this);
    returnTime.setOnClickListener(this);
    pickUpAddress.setOnClickListener(this);
    returnAddress.setOnClickListener(this);
    addressSummit.setOnClickListener(this);
    addressCancel.setOnClickListener(this);
    book.setOnClickListener(this);
    btcCheckBox.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (((CheckBox) v).isChecked()) {
                Toast.makeText(getActivity(),
                        "CHECKED", Toast.LENGTH_LONG).show();
                isBTC = true;
            } else {
                Toast.makeText(getActivity(),
                        "UNCHECKED", Toast.LENGTH_LONG).show();
                isBTC = false;
            }
        }
    });

    //on item listener for Spinners
    dutyType.setOnItemSelectedListener(this);
    vehicleCategory.setOnItemSelectedListener(this);
    vehicleType.setOnItemSelectedListener(this);
}

任何建议请...

1 个答案:

答案 0 :(得分:0)

试试这个,

更改onCreateView编码,如下所示。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{
View addresspopupView =inflater.inflate(R.layout.addressinput, container, false);

AutoCompleteTextView city = (AutoCompleteTextView) addresspopupView.findViewById(R.id.city);

String[] CITY ={"Mumbai", "Ahemadabad", "Kolkata", "Chennai", "Thane"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_dropdown_item_1line, CITY);
city.setAdapter(adapter);

PopupWindow addresspopupWindow = new PopupWindow(addresspopupView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
addresspopupWindow.showAtLocation(addresspopupView, Gravity.CENTER, 0, 0);

return addresspopupView;
}

已编辑:

像这样在oncreateView方法中复制粘贴这些行。

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        // Inflate the layout for this fragment
        Log.d("ClientBookingScreen", "Inside OnCreateView of ClientBookingScreen");
        rootView = inflater.inflate(R.layout.bookingrequest, container, false);
    AutoCompleteTextView city = (AutoCompleteTextView) rootView.findViewById(R.id.city);

    String[] CITY ={"Mumbai", "Ahemadabad", "Kolkata", "Chennai", "Thane"};
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_dropdown_item_1line, CITY);
    city.setAdapter(adapter);
    intialize(rootView);
    intialSetUp();
    loadconfigs();
    initializeAddressPopUp();
    settingClickListener();
    return rootView;
}

可能是你夸大布局错误的方式。试试上面的代码并告诉我。