我正在尝试使用google maps api搜索地址,并使用结果填充ListView。我们的想法是使用EditText,每次按下一个键时,使用新文本搜索匹配的地址,并使用新结果填充ListView。我已经检查过findViewById中ListView的id是正确的。
public class SearchActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener{
// List that will be used to show the search results
List<Address> listItems;
// Adapter that will be used to fill the ListView in layout file
ArrayAdapter<Address> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
adapter=new ArrayAdapter<Address>(this,
android.R.layout.simple_list_item_1,
listItems);
final EditText search_txt=(EditText) findViewById(R.id.search_edittxt);
search_txt.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
String searchTerms=search_txt.getText().toString();
if(searchTerms.length()>0)
UpdateResults(searchTerms);
return false;
}
});
}
public void UpdateResults(String address)
{
Geocoder geocoder=new Geocoder(this);
try {
listItems=geocoder.getFromLocationName(address, 5);
} catch (IOException e) {
e.printStackTrace();
}
if(!listItems.isEmpty()) {
ListView searchResultsList = (ListView) findViewById(R.id.search_result_list);
searchResultsList.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
}
xml布局文件是
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.heycabs.heycabs.SearchActivity"
tools:showIn="@layout/activity_search">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:id="@+id/topLayout"
>
<EditText
android:layout_width="260dp"
android:layout_height="match_parent"
android:inputType="textPostalAddress"
android:id="@+id/search_edittxt"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:src="@android:drawable/ic_search_category_default"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_alignStart="@+id/topLayout"
android:layout_alignLeft="@+id/topLayout">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/search_result_list"
android:drawSelectorOnTop="false">
</ListView>
</LinearLayout>
</RelativeLayout>
但每次searchResultsList.setAdapter(adapter)
都会导致NullPointerException。这是堆栈跟踪。
java.lang.NullPointerException
at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:330)
at android.widget.ListView.setAdapter(ListView.java:466)
at com.heycabs.heycabs.SearchActivity.UpdateResults(SearchActivity.java:82)
at com.heycabs.heycabs.SearchActivity$1.onKey(SearchActivity.java:65)
at android.view.View.dispatchKeyEvent(View.java:7239)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1376)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1376)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1376)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1376)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1376)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1376)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1376)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1376)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1376)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1937)
at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1410)
at android.app.Activity.dispatchKeyEvent(Activity.java:2388)
at android.support.v7.app.AppCompatActivity.dispatchKeyEvent(AppCompatActivity.java:533)
at android.support.v7.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:53)
at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.dispatchKeyEvent(AppCompatDelegateImplBase.java:254)
at android.support.v7.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:53)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1864)
at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:4175)
at android.view.ViewRootImpl.deliverKeyEvent(ViewRootImpl.java:4093)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3574)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4849)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4809)
at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:3384)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5409)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
这是因为您尚未初始化listItems。这样做
List<Address> listItems = new ArrayList<Address>();
并且不要在UpdateResults方法中为listItems分配新的引用。所以不要这样:
listItems=geocoder.getFromLocationName(address, 5);
这样做:
listItems.clear();
listItems.addAll(geocoder.getFromLocationName(address, 5));