通过MapsFragment的AutoCompleteTextView [英] AutoCompleteTextView over MapsFragment
问题描述
在我的应用中,我有一个RelativeLayout-> fragment(SupportMapFragment)| Relativelayout-> AutoCompleteTextView
In my app I have a RelativeLayout->fragment(SupportMapFragment)|Relativelayout->AutoCompleteTextView
搜索应执行Google Maps搜索栏应执行的操作,并从我的搜索关键字返回位置的下拉列表.完成输入并延迟几秒钟后,我的应用崩溃,并显示以下错误:
The search should do what Google Maps' search bar should do and return a dropdown list of locations from my search keyword. After I finish typing and a few second delay, my app crashes with the error:
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 ).这是可以解决的吗?
I understand that this may be because I am not able to inflate a view over a fragment(https://stackoverflow.com/a/19815266/5336014). Is this something that is fixable?
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;
}
}
此外,我在解决此链接的另一个问题时遇到了此错误:为自动完成适配器过滤json结果
Also, I got this error solving another problem which can be found at this link: Filter json results for Autocomplete adapter
谢谢.
推荐答案
ArrayAdapter
的两个参数构造函数,即ArrayAdapter (Context context, int resource)
需要布局资源,而不是TextView
的id.
The two argument constructor of ArrayAdapter
i.e ArrayAdapter (Context context, int resource)
expects layout resource and not id of a TextView
.
您正在传递AutoCompleteTextView
的ID,该ID会尝试膨胀,但由于没有具有该ID的布局而失败.将实例化适配器的行更改为:
You are passing id of your AutoCompleteTextView
that it tries to inflate but fails as there is no layout with that id. Change the line where you instantiate your adapter to:
autoCompView.setAdapter(
new GooglePlacesAutocompleteAdapter(this,
android.R.layout.simple_list_item_1));
这篇关于通过MapsFragment的AutoCompleteTextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!