我正在尝试在我的Firebase数据库中搜索信息,但无法这样做。
这是我要从中搜索的课程:
private FirebaseRecyclerAdapter<DjProfile, ResultsViewHolder> firebaseRecyclerAdapter;
public static Query query;
public static String searchData;
DatabaseReference mProfileDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_attendee_landing);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final RecyclerView recyclerView = findViewById(R.id.results_recycler);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
final EditText t = (EditText) findViewById(R.id.DjNameET);
mProfileDatabase = FirebaseDatabase.getInstance().getReference();
t.setOnEditorActionListener(new TextView.OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
if(i == EditorInfo.IME_ACTION_SEARCH)
{
searchData = t.getText().toString();
return true;
}
return false;
}
});
ImageButton imgB= (ImageButton) findViewById(R.id.searchBtn);
imgB.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view)
{
query = mProfileDatabase.orderByChild("djprofile")
.startAt(searchData)
.endAt(searchData + "\uf8ff");
}
});
FirebaseRecyclerOptions<DjProfile> firebaseRecyclerOptions = new FirebaseRecyclerOptions.Builder<DjProfile>()
.setQuery(query, DjProfile.class)
.build();
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<DjProfile, ResultsViewHolder>(firebaseRecyclerOptions)
{
@NonNull
@Override
public ResultsViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.searchitem_list, viewGroup, false);
return new ResultsViewHolder(view);
}
@Override
protected void onBindViewHolder(@NonNull ResultsViewHolder holder, int position, @NonNull DjProfile model) {
holder.setDjProfile(model);
}
};
recyclerView.setAdapter(firebaseRecyclerAdapter);
}
@Override
protected void onStart()
{
super.onStart();
firebaseRecyclerAdapter.startListening();
}
这是我得到的例外:(说发生在onStart()方法中)
java.lang.NullPointerException:尝试在null上调用虚拟方法'com.google.firebase.database.ChildEventListener com.google.firebase.database.Query.addChildEventListener(com.google.firebase.database.ChildEventListener)'对象引用
任何关于为什么为空的帮助将不胜感激!
答案 0 :(得分:1)
要能够初始化适配器,您需要有一个有效的查询。为了能够创建查询,您需要拥有searchData
。这意味着firebaseRecyclerAdapter
的初始化应该在onClick
内部进行。
imgB.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view)
{
query = mProfileDatabase.orderByChild("djprofile")
.startAt(searchData)
.endAt(searchData + "\uf8ff");
FirebaseRecyclerOptions<DjProfile> firebaseRecyclerOptions = new FirebaseRecyclerOptions.Builder<DjProfile>()
.setQuery(query, DjProfile.class)
.build();
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<DjProfile, ResultsViewHolder>(firebaseRecyclerOptions)
{
@NonNull
@Override
public ResultsViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.searchitem_list, viewGroup, false);
return new ResultsViewHolder(view);
}
@Override
protected void onBindViewHolder(@NonNull ResultsViewHolder holder, int position, @NonNull DjProfile model) {
holder.setDjProfile(model);
}
};
recyclerView.setAdapter(firebaseRecyclerAdapter);
}
});