java Android Studio:使用带有适配器和地理编码器的AutocompletrTextView时应用程序崩溃
我试图创建一个应用程序,当我使用autocompletetextview时,它会建议从地理编码器中获取城市名称,但当我在ContextChanged方法中更新arrayList时,该应用程序会崩溃
我试着删除代码的某些部分,看看哪个是错的, 我注意到的唯一一件事是,在向arrayList添加数据和调用方法适配器时,问题都存在。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, 安卓.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"?>
<安卓.support.constraint.ConstraintLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:app="http://schemas.安卓.com/apk/res-auto"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
安卓:background="@drawable/sfondo"
tools:context=".MainActivity">
<AutoCompleteTextView
安卓:id="@+id/actv"
安卓:layout_width="0dp"
安卓:layout_height="wrap_content"
安卓:layout_marginTop="104dp"
安卓:completionThreshold="1"
安卓: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" />
</安卓.support.constraint.ConstraintLayout>
当我尝试使用AutocompleteTextView时,应用程序崩溃,我尝试了没有适配器的代码。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 安卓.widget.ArrayAdapter.getCount(ArrayAdapter.java:388)
at 安卓.widget.AutoCompleteTextView$PopupDataSetObserver$1.run(AutoCompleteTextView.java:1411)
at 安卓.os.Handler.handleCallback(Handler.java:873)
at 安卓.os.Handler.dispatchMessage(Handler.java:99)
at 安卓.os.Looper.loop(Looper.java:193)
at 安卓.app.ActivityThread.main(ActivityThread.java:6694)
at java.lang.reflect.Method.invoke(Native Method)
at com.安卓.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:858)
共 (0) 个答案