尝试在具有Firebase Realtime数据库的Recyclerview中添加searchView,但尽管CompatBar中显示图标,但我应用searchView时数据库活动崩溃
Main3Activity.java
private RecyclerView mPeopleRV;
private DatabaseReference mDatabase;
private FirebaseRecyclerAdapter<News, NewsViewHolder> mPeopleRVAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news);
}
private void firebaseSearch(String searchText) {
Query firebaseSearchQuery = mDatabase.orderByChild("title").startAt(searchText).endAt(searchText + "\uf8ff");
setTitle("News");
MyDatabaseUtil.getDatabase();
//"News" here will reflect what you have called your database in Firebase.
mDatabase = FirebaseDatabase.getInstance().getReference().child("S");
mDatabase.keepSynced(true);
mPeopleRV = (RecyclerView) findViewById(R.id.myRecycleView);
// FirebaseDatabase.getInstance().setPersistenceEnabled(true);
DatabaseReference personsRef = FirebaseDatabase.getInstance().getReference().child("S");
Query personsQuery = personsRef.orderByKey();
mPeopleRV.hasFixedSize();
mPeopleRV.setLayoutManager(new LinearLayoutManager(this));
FirebaseRecyclerOptions personsOptions = new FirebaseRecyclerOptions.Builder<News>().setQuery(personsQuery, News.class).build();
mPeopleRVAdapter = new FirebaseRecyclerAdapter<News, NewsViewHolder>(personsOptions) {
@Override
protected void onBindViewHolder(Main3Activity.NewsViewHolder holder, final int position, final News model) {
holder.setTitle(model.getTitle());
holder.setMandi(model.getMandi());
holder.setMarket(model.getMarket());
holder.setUrdu(model.getUrdu());
holder.setBachat(model.getBachat());
holder.setDesc(model.getDesc());
holder.setImage(getBaseContext(), model.getImage());
holder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String url = model.getUrl();
Intent intent = new Intent(getApplicationContext(), NewsWebView.class);
intent.putExtra("id", url);
startActivity(intent);
}
});
}
@Override
public Main3Activity.NewsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.news_row, parent, false);
return new Main3Activity.NewsViewHolder(view);
}
};
mPeopleRV.setAdapter(mPeopleRVAdapter);
}
@Override
public void onStart() {
super.onStart();
mPeopleRVAdapter.startListening();
}
@Override
public void onStop() {
super.onStop();
mPeopleRVAdapter.stopListening();
}
public class NewsViewHolder extends RecyclerView.ViewHolder {
View mView;
public NewsViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setTitle(String title) {
TextView post_title = (TextView) mView.findViewById(R.id.post_title);
post_title.setText(title);
}
public void setUrdu(String urdu) {
TextView post_urdu = (TextView) mView.findViewById(R.id.Urdu);
post_urdu.setText(urdu);
}
public void setMarket(String market) {
TextView post_market = (TextView) mView.findViewById(R.id.market);
post_market.setText(market);
}
public void setMandi(String mandi) {
TextView post_mandi = (TextView) mView.findViewById(R.id.mundi);
post_mandi.setText(mandi);
}
public void setBachat(String bachat) {
TextView post_bachat = (TextView) mView.findViewById(R.id.bachat);
post_bachat.setText(bachat);
}
public void setDesc(String desc) {
TextView post_desc = (TextView) mView.findViewById(R.id.post_desc);
post_desc.setText(desc);
}
public void setImage(Context ctx, String image) {
ImageView post_image = (ImageView) mView.findViewById(R.id.post_image);
Picasso.with(ctx).load(image).into(post_image);
}
}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
MenuItem item = menu.findItem(R.id.search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
firebaseSearch(query);
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
return false;
}
});
return super.onCreateOptionsMenu(menu);
}
@Override
public void onBackPressed() {
super.onBackPressed();
}
}
Menu.xml
> <menu 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"
tools:context=".Main3Activity">
<item
android:id="@+id/search"
android:icon="@drawable/ic_home" android:title="search"
app:showAsAction="always"
app:actionViewClass="android.support.v7.widget.SearchView" />
</Menu>
Logcat错误 java.lang.RuntimeException:无法启动活动ComponentInfo {e.sszj_capricorn.requirementdatarecyclerviewtextview / e.sszz_capricorn.requirementdatarecyclerviewtextview.Main3Activity}:java.lang.NullPointerException:尝试调用虚拟方法'void void com.firebase.Re.Adapter。在空对象引用上的startListening()' 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2726) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2787) 在android.app.ActivityThread.-wrap12(ActivityThread.java) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1504) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:154) 在android.app.ActivityThread.main(ActivityThread.java:6247) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:872) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762) 原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void com.firebase.ui.database.FirebaseRecyclerAdapter.startListening()' 在e.sszj_capricorn.requirementdatarecyclerviewtextview.Main3Activity.onStart(Main3Activity.java:96) 在android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1249) 在android.app.Activity.performStart(Activity.java:6774) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2689) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2787) 在android.app.ActivityThread.-wrap12(ActivityThread.java) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1504) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:154) 在android.app.ActivityThread.main(ActivityThread.java:6247) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:872) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762) 应用已终止。