我正在尝试创建一个应用程序,当我使用 autocompletetextview 时建议从地理编码器中获取城市名称,但是当我在 OnTextChanged 方法中更新 arrayList 时应用程序崩溃。
我试图删除代码的某些部分以查看哪些是错误的,我唯一注意到的是问题出在我将数据添加到 arrayList 以及调用方法 adapter.notifyDataSetChanged (); 时。
这是我使用的代码:
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
ArrayList<String> CITTA;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AutoCompleteTextView autocomplete= findViewById(R.id.actv);
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, CITTA );
autocomplete.setAdapter(adapter);
autocomplete.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(final CharSequence s, int start, int before, int count) {
Geocoder geocoder = new Geocoder(MainActivity.this);
try {
List<Address> mylist = geocoder.getFromLocationName(s.toString(), 5);
if (!mylist.isEmpty()) {
for(int i=0;i<mylist.size();i++){
if(!mylist.get(i).getLocality().isEmpty())
CITTA.add(mylist.get(i).getLocality());
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
adapter.notifyDataSetChanged();
}
@Override
public void afterTextChanged(Editable s) {
}
});
这就是我的 xml 中的内容
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:background="@drawable/sfondo"
tools:context=".MainActivity">
<AutoCompleteTextView
android:id="@+id/actv"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="104dp"
android:completionThreshold="1"
android:hint="Digita un paese..."
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
当我尝试使用 AutocompleteTextView 时应用程序崩溃,我尝试了没有 adapter.notifyDataSetChanged() 的代码;它不会立即崩溃,但是当我添加许多字母时它还是会崩溃,我是 Android Studio 世界的新手,我已经遵循了几个教程来获得这个结果,但它不起作用:(
--------- beginning of crash
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.orientation, PID: 14114
java.lang.NullPointerException: Attempt to invoke interface method 'int java.util.List.size()' on a null object reference
at android.widget.ArrayAdapter.getCount(ArrayAdapter.java:388)
at android.widget.AutoCompleteTextView$PopupDataSetObserver$1.run(AutoCompleteTextView.java:1411)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6694)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)