MapsFragment上的AutoCompleteTextView

时间:2015-12-13 04:17:24

标签: java android xml android-arrayadapter layout-inflater

Screenshot of app

在我的应用程序中,我有一个RelativeLayout->片段(SupportMapFragment)| Relativelayout-> AutoCompleteTextView

搜索应该做什么谷歌地图'搜索栏应该执行并返回搜索关键字中的位置下拉列表。在我完成输入并延迟几秒后,我的应用程序崩溃并出现错误:

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>

OnCreate中:

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

另外,我遇​​到了这个错误,解决了另一个问题,可以在这个链接找到:Filter json results for Autocomplete adapter

提前致谢。

1 个答案:

答案 0 :(得分:1)

ArrayAdapter的两个参数构造函数,即ArrayAdapter (Context context, int resource)需要布局资源,而不是TextView的id。

您正在传递其AutoCompleteTextView的ID,它会尝试通知但会失败,因为没有该ID的布局。将实例化适配器的行更改为:

autoCompView.setAdapter(
              new GooglePlacesAutocompleteAdapter(this,
              android.R.layout.simple_list_item_1));