0

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)
Mettthew
  • 1
  • 1
  • Try posting here your error trace from your Logcat – L2_Paver Oct 16 '19 at 00:54
  • Also you can't really fully rely on Geocoder to give you the address because it requires internet connection and sometimes produces this kind of problem https://stackoverflow.com/questions/7109240/service-not-available-geocoder-android – L2_Paver Oct 16 '19 at 01:00
  • Sorry, but i’m new in this world, for debugging i usually building Apk and put it in my Telephone and i can’t see Logcat error in this way... How can i see these Errors? – Mettthew Oct 16 '19 at 15:50
  • On android phone(not Telephone btw mind your terminology)>Developer Options(On) >> USB Debugging(On) >> Connect your Phone to PC via a usb cable >> Wait and Accept the ADB permission >> Check if your device is display in the upper middle part of android studio >> click the play button – L2_Paver Oct 17 '19 at 00:23
  • debugging reference https://developer.android.com/studio/debug – L2_Paver Oct 17 '19 at 00:25
  • ok, thank you so much! now i edited the post and show all crash errors – Mettthew Oct 17 '19 at 20:12
  • read this to understand logcat errors https://stackoverflow.com/questions/6065258/how-to-interpret-logcat – L2_Paver Oct 18 '19 at 00:21
  • Try finding this at Logcat `com.example.MyProject.YOUR_FUNCTION(MainActivity.java:290)` `com.example.MyProject.onCreate(MainActivity.java:216)` and click the blue text it will tell you where is your error. I try you code above and it works fine on me. cannot tell the problem – L2_Paver Oct 18 '19 at 00:44
  • Ok, the error that make the app crashes was that ArrayList CITTA wasn’t create with new ArrayList, now i Fix it, and i doesen’t crash anymore, but now it Doesen’t show me any suggestions when i tipe something – Mettthew Oct 18 '19 at 10:57
  • What you add on the list doesn't meet the character you fill on autocompleteTextview(ATCV). example you input the word "Lake" on the ATCV, the word "Lake" is use to get the locality and add it on arraylist, ACTV only display all item on the arraylist that with the character "Lake" if the one you add on the arraylist doesnt have word "Lake" the ACTV wont display it, But it is exist on the Arraylist – L2_Paver Oct 18 '19 at 14:00
  • I think it's better to use EditText that call custom dialog with EditText use as search box and listview to display result. – L2_Paver Oct 18 '19 at 14:06

0 Answers0