I'm trying to create an application that suggests city names taken from the geocoder when I use the autocompletetextview, but the application crashes when I update the arrayList within the OnTextChanged method.
I have tried to remove some parts of the code to see which is wrong, the only things I noticed is that the problem lies both when I add data to the arrayList and when I invoke the method adapter.notifyDataSetChanged ();
This is the code i used:
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) {
}
});
And this is what's in my 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>
the app crashes when i try to use the AutocompleteTextView, i tried the code without the adapter.notifyDataSetChanged(); and it doesn't crash immediately, but it crashes anyway when I add many letters, I'm new to the world of Android studio and I've followed several tutorials to get this result, but it doesn't work :(
--------- 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)