AutoCompleteTextView над MapsFragment

Скриншот приложения

В моем приложении у меня есть RelativeLayout->fragment(SupportMapFragment)|Relativelayout->AutoCompleteTextView

Поиск должен делать то, что должна делать панель поиска Google Maps, и возвращать раскрывающийся список местоположений из моего ключевого слова поиска. После того, как я закончу печатать и задержусь на несколько секунд, мое приложение вылетает с ошибкой:

12-12 21:59:13.138 25010-25010/com.hudsoncorp.zahudson.roadtrip E/AndroidRuntime: FATAL EXCEPTION: main
12-12 21:59:13.138 25010-25010/com.hudsoncorp.zahudson.roadtrip E/AndroidRuntime: Process: com.hudsoncorp.zahudson.roadtrip, PID: 25010
12-12 21:59:13.138 25010-25010/com.hudsoncorp.zahudson.roadtrip E/AndroidRuntime: android.content.res.Resources$NotFoundException: Resource ID #0x7f0d0091 type #0x12 is not valid
12-12 21:59:13.138 25010-25010/com.hudsoncorp.zahudson.roadtrip E/AndroidRuntime:     at android.content.res.Resources.loadXmlResourceParser(Resources.java:4165)
12-12 21:59:13.138 25010-25010/com.hudsoncorp.zahudson.roadtrip E/AndroidRuntime:     at android.content.res.Resources.getLayout(Resources.java:2268)
12-12 21:59:13.138 25010-25010/com.hudsoncorp.zahudson.roadtrip E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:413)
12-12 21:59:13.138 25010-25010/com.hudsoncorp.zahudson.roadtrip E/AndroidRuntime:     at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:371)
12-12 21:59:13.138 25010-25010/com.hudsoncorp.zahudson.roadtrip E/AndroidRuntime:     at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
12-12 21:59:13.138 25010-25010/com.hudsoncorp.zahudson.roadtrip E/AndroidRuntime:     at android.widget.AbsListView.obtainView(AbsListView.java:2833)
12-12 21:59:13.138 25010-25010/com.hudsoncorp.zahudson.roadtrip E/AndroidRuntime:     at android.widget.ListPopupWindow$DropDownListView.obtainView(ListPopupWindow.java:1852)
12-12 21:59:13.138 25010-25010/com.hudsoncorp.zahudson.roadtrip E/AndroidRuntime:     at android.widget.ListView.measureHeightOfChildren(ListView.java:1292)
12-12 21:59:13.138 25010-25010/com.hudsoncorp.zahudson.roadtrip E/AndroidRuntime:     at android.widget.ListPopupWindow.buildDropDown(ListPopupWindow.java:1323)
12-12 21:59:13.138 25010-25010/com.hudsoncorp.zahudson.roadtrip E/AndroidRuntime:     at android.widget.ListPopupWindow.show(ListPopupWindow.java:709)
12-12 21:59:13.138 25010-25010/com.hudsoncorp.zahudson.roadtrip E/AndroidRuntime:     at android.widget.AutoCompleteTextView.showDropDown(AutoCompleteTextView.java:1128)
12-12 21:59:13.138 25010-25010/com.hudsoncorp.zahudson.roadtrip E/AndroidRuntime:     at android.widget.AutoCompleteTextView.updateDropDownForFilter(AutoCompleteTextView.java:996)
12-12 21:59:13.138 25010-25010/com.hudsoncorp.zahudson.roadtrip E/AndroidRuntime:     at android.widget.AutoCompleteTextView.onFilterComplete(AutoCompleteTextView.java:978)
12-12 21:59:13.138 25010-25010/com.hudsoncorp.zahudson.roadtrip E/AndroidRuntime:     at android.widget.Filter$ResultsHandler.handleMessage(Filter.java:285)
12-12 21:59:13.138 25010-25010/com.hudsoncorp.zahudson.roadtrip E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
12-12 21:59:13.138 25010-25010/com.hudsoncorp.zahudson.roadtrip E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:145)
12-12 21:59:13.138 25010-25010/com.hudsoncorp.zahudson.roadtrip E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:6938)
12-12 21:59:13.138 25010-25010/com.hudsoncorp.zahudson.roadtrip E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
12-12 21:59:13.138 25010-25010/com.hudsoncorp.zahudson.roadtrip E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
12-12 21:59:13.138 25010-25010/com.hudsoncorp.zahudson.roadtrip E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
12-12 21:59:13.138 25010-25010/com.hudsoncorp.zahudson.roadtrip E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

Я понимаю, что это может быть связано с тем, что я не могу раздуть представление над фрагментом (https://stackoverflow.com/a/19815266/5336014). Это что-то поправимое?

XML-код:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:id="@+id/activity_maps_layout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                tools:context="com.hudsoncorp.zahudson.roadtrip.MapsActivity">

        <fragment xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            xmlns:map="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/map"
            tools:context="com.hudsoncorp.zahudson.roadtrip.MapsActivity"
            android:name="com.google.android.gms.maps.SupportMapFragment"/>

        <RelativeLayout
            android:orientation="vertical"
            android:id="@+id/list_results"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="10dp">

            <AutoCompleteTextView
                android:id="@+id/autoCompleteTextView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="10dp"
                android:hint="Search location"
                android:background="#FFFFFF"
                android:textColor="#00000F"
                android:theme="@android:style/Widget.Material.AutoCompleteTextView"
                android:ems="10"
                android:shadowColor="@color/background_floating_material_dark">
                <requestFocus />
            </AutoCompleteTextView>

        </RelativeLayout>

</RelativeLayout>

При создании:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        buildGoogleApiClient();
        setContentView(R.layout.activity_maps);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

        if (savedInstanceState != null) {
            mIsResolving = savedInstanceState.getBoolean(KEY_IS_RESOLVING);
            mShouldResolve = savedInstanceState.getBoolean(KEY_SHOULD_RESOLVE);
        }

//      Create the LocationRequest object
        mLocationRequest = LocationRequest.create()
                .setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY)
                .setInterval(10 * 1000)           //10 seconds, in milliseconds
                .setFastestInterval(1 * 1000);   //1 second, in milliseconds

        Log.d(TAG, "GoogleApiClient built");

        if (savedInstanceState != null) {
            mSignInProgress = savedInstanceState
                    .getInt(SAVED_PROGRESS, STATE_DEFAULT);
        }

        mShouldResolve = true;
        mGoogleApiClient.connect();
        Log.d(TAG, "Starting sign-in");

        final AutoCompleteTextView autoCompView = (AutoCompleteTextView)
                findViewById(R.id.autoCompleteTextView);

        autoCompView.setAdapter(new GooglePlacesAutocompleteAdapter(this, R.id.autoCompleteTextView));
        autoCompView.setOnItemClickListener(this);

    }

Адаптер:

class GooglePlacesAutocompleteAdapter extends ArrayAdapter implements Filterable {
        private ArrayList resultList;

        private static final String TAG = "Filter";


        public GooglePlacesAutocompleteAdapter(Context context, int textViewResourceId) {
            super(context, textViewResourceId);
        }

        @Override
        public int getCount() {
            Log.d(TAG, resultList.toString());
            return resultList.size();
        }

        @Override
        public String getItem(int index) {
            return (String) resultList.get(index);
        }

        @Override
        public Filter getFilter() {
            Filter filter = new Filter() {

                @Override
                protected FilterResults performFiltering(CharSequence constraint) {

                    ArrayList<String> resultList = null;
                    FilterResults filterResults = new FilterResults();

                    if (constraint != null) {
                        // Retrieve the autocomplete results.
                        Log.d(TAG, "Starting Search");

                        HttpURLConnection conn = null;
                        StringBuilder jsonResults = new StringBuilder();
                        try {
                            KEYWORD = constraint.toString();

                            ENDPOINT = Uri
                                    .parse("https://maps.googleapis.com/maps/api/place/nearbysearch/json")
                                    .buildUpon()
                                    .appendQueryParameter("location", LOCATION)
                                    .appendQueryParameter("radius", "10000")
                                    .appendQueryParameter("keyword", KEYWORD)
                                    .appendQueryParameter("key", API_KEY)
                                    .build();

                            URL url = new URL(ENDPOINT.toString());

                            Log.d(TAG, "URL: " + url);

                            conn = (HttpURLConnection) url.openConnection();
                            InputStreamReader in = new InputStreamReader(conn.getInputStream());

                            // Load the results into a StringBuilder
                            int read;
                            char[] buff = new char[1024];
                            while ((read = in.read(buff)) != -1) {
                                jsonResults.append(buff, 0, read);
                            }
                            Log.d(TAG, jsonResults.toString());
                        } catch (MalformedURLException e) {
                            Log.e(TAG, "Error processing Places API URL", e);
                            return filterResults ;
                        } catch (IOException e) {
                            Log.e(TAG, "Error connecting to Places API", e);
                            return filterResults ;
                        } finally {
                            if (conn != null) {
                                conn.disconnect();
                            }
                        }
                        try {
                            // Create a JSON object hierarchy from the results
                            JSONObject jsonObj = new JSONObject(jsonResults.toString());
                            JSONArray predsJsonArray = jsonObj.getJSONArray("results");

                            // Extract the Place descriptions from the results
                            resultList = new ArrayList(predsJsonArray.length());
                            for (int i = 0; i < predsJsonArray.length(); i++) {
                                System.out.println(predsJsonArray.getJSONObject(i).getString("name"));
                                System.out.println(predsJsonArray.getJSONObject(i).getString("vicinity"));
                                System.out.println("=====================================================");
                                resultList.add(predsJsonArray.getJSONObject(i).getString("name"));
                                resultList.add(predsJsonArray.getJSONObject(i).getString("vicinity"));
                            }
                        } catch (JSONException e) {
                            Log.e(TAG, "Cannot process JSON results", e);
                        }
                        Log.d(TAG, resultList.toString());

                        // Assign the data to the FilterResults
                        filterResults.values = resultList;
                        filterResults.count = resultList.size();
                    }
                    return filterResults;
                }

                @Override
                protected void publishResults(CharSequence constraint, Filter.FilterResults results) {
                    if (results != null && results.count > 0) {
                        resultList = (ArrayList) results.values;
                        notifyDataSetChanged();
                    } else {
                        notifyDataSetInvalidated();
                    }
                }
            };
            return filter;
        }
    }

Кроме того, я получил эту ошибку, решая другую проблему, которую можно найти по этой ссылке: Отфильтровать результаты json для адаптера автозаполнения

Заранее спасибо.


person zahudson95    schedule 13.12.2015    source источник


Ответы (1)


Конструктор с двумя аргументами ArrayAdapter, то есть ArrayAdapter (Context context, int resource), ожидает ресурс макета, а не идентификатор TextView.

Вы передаете идентификатор своего AutoCompleteTextView, который он пытается раздуть, но терпит неудачу, поскольку нет макета с этим идентификатором. Измените строку, в которой вы создаете свой адаптер, на:

autoCompView.setAdapter(
              new GooglePlacesAutocompleteAdapter(this,
              android.R.layout.simple_list_item_1)); 
person Abdullah    schedule 13.12.2015
comment
Взгляните на простую библиотеку, которую я написал, чтобы получить готовый текст для автоматического завершения Places. Это сэкономит вам весь этот код: github.com/abdullah-chhatra/threepio-places- изменить текст - person Abdullah; 13.12.2015
comment
Это сработало мгновенно. Абсолютно никаких проблем. Большое спасибо! И я изучу эту библиотеку, так как результаты кажутся медленными. Любая идея, почему это было бы? - person zahudson95; 13.12.2015