创建一个从 Firebase 加载的可搜索微调器
Creating a Searchable spinner that Loads from Fire Base
我正在尝试创建一个可搜索的微调器,用于从 firebase 加载县信息。我是 android 开发新手,我的工作基于 youtube 上的各种视频和堆栈溢出 post。这是我目前的 activity:
public class Preferences extends AppCompatActivity {
SearchableSpinner spcounties;
private DatabaseReference mDatabase;
List<String> countyList = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_preferences);
spcounties = (SearchableSpinner) findViewById(R.id.spcounties);
mDatabase = FirebaseDatabase.getInstance().getReference();
mDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
String county = ds.child("County").getValue(String.class);
countyList.add(county);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, countyList);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spcounties.setAdapter(adapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(Preferences.this, databaseError.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
}
我的数据库结构如下:
现在,当单击可搜索的微调器时,我的应用程序崩溃了。
堆栈跟踪:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.jws.documentmarketplace, PID: 17515
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:454)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:416)
at android.widget.AbsListView.obtainView(AbsListView.java:2405)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1417)
at android.widget.ListView.onMeasure(ListView.java:1323)
at android.view.View.measure(View.java:25185)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6821)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1552)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:842)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:721)
at android.view.View.measure(View.java:25185)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6821)
at android.widget.Fra
我更改了这一行,它起作用了。
mDatabase = FirebaseDatabase.getInstance().getReference("Counties/");
我正在尝试创建一个可搜索的微调器,用于从 firebase 加载县信息。我是 android 开发新手,我的工作基于 youtube 上的各种视频和堆栈溢出 post。这是我目前的 activity:
public class Preferences extends AppCompatActivity {
SearchableSpinner spcounties;
private DatabaseReference mDatabase;
List<String> countyList = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_preferences);
spcounties = (SearchableSpinner) findViewById(R.id.spcounties);
mDatabase = FirebaseDatabase.getInstance().getReference();
mDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
String county = ds.child("County").getValue(String.class);
countyList.add(county);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, countyList);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spcounties.setAdapter(adapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(Preferences.this, databaseError.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
}
我的数据库结构如下:
现在,当单击可搜索的微调器时,我的应用程序崩溃了。
堆栈跟踪:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.jws.documentmarketplace, PID: 17515
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:454)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:416)
at android.widget.AbsListView.obtainView(AbsListView.java:2405)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1417)
at android.widget.ListView.onMeasure(ListView.java:1323)
at android.view.View.measure(View.java:25185)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6821)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1552)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:842)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:721)
at android.view.View.measure(View.java:25185)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6821)
at android.widget.Fra
我更改了这一行,它起作用了。
mDatabase = FirebaseDatabase.getInstance().getReference("Counties/");