所以我在我的应用程序中有搜索功能,它适用于数据库中的内容。但是当项目不在我的数据库中时,我会收到致命的错误。
搜索功能在触发按钮时有一个edittext和一个按钮,它会检查数据库以获取对edittext的引用。
这是我的代码。
public void searchfunction() {
mArraylist.clear();
final ChildEventListener msearchevent = new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String msearchname = dataSnapshot.child("name").getValue(String.class);
final String url = dataSnapshot.child("url").getValue(String.class);
if (msearchname.toLowerCase().contains(searchapps.toLowerCase())) {
marrayAdapter2 = new ArrayAdapter(MainActivity.this, android.R.layout.simple_list_item_1, mArraylist);
mlv.setAdapter(marrayAdapter2);
mArraylist.add(msearchname);
mArraylist2.add(url);
marrayAdapter2.notifyDataSetChanged();
mlv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
DownloadFileFromURL dl = new DownloadFileFromURL(mContext, apkNames, mCurrentPhotoPath);
dl.execute(mArraylist2.get(position));
}
});
}else {
Toast.makeText(MainActivity.this, "No results found!",Toast.LENGTH_SHORT).show();
}
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
marrayAdapter2.notifyDataSetChanged();
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
marrayAdapter2.notifyDataSetChanged();
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
marrayAdapter2.notifyDataSetChanged();
}
@Override
public void onCancelled(FirebaseError firebaseError) {
marrayAdapter2.notifyDataSetChanged();
}
};
mListRef.child("Filmapps").addChildEventListener(msearchevent);
我的按钮单击是在GetView功能中处理的 这是我点击按钮的代码
mgoButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
searchapps = msearch.getText().toString().trim();
searchfunction();
InputMethodManager inputManager = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
}
});
06-27 17:51:16.708 2938-2938/com.example.harrops.h20droidapp2 E/UncaughtException: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toLowerCase()' on a null object reference
at com.example.harrops.h20droidapp2.actvities.MainActivity$3.onChildAdded(MainActivity.java:287)
at com.firebase.client.core.ChildEventRegistration.fireEvent(ChildEventRegistration.java:48)
at com.firebase.client.core.view.DataEvent.fire(DataEvent.java:45)
at com.firebase.client.core.view.EventRaiser$1.run(EventRaiser.java:38)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
06-27 17:51:16.712 2938-3140 / com.example.harrops.h20droidapp2 D / FA:记录事件(FE):_ae,Bundle [{_ o = crash,_sc = MainActivity,_si = 7357998494779803214,timestamp = 1498585876708,致命= 1}]
答案 0 :(得分:2)
getValue docs
传入的类的实例,使用此数据填充 快照,或
null if there is no data at this location.
所以添加无效检查
if (msearchname!=null && url!=null && msearchname.toLowerCase().contains(searchapps.toLowerCase())) {
注意:&&
是一个短路运算符,如果有任何错误的表达式值,则意味着不会执行if
。