Android / Firebase:onDataChange()用于查询返回空指针异常

时间:2016-09-24 08:28:52

标签: android firebase firebase-realtime-database

我试图通过其id从我的firebase数据库中检索一个对象。但我的应用程序崩溃与空指针异常。请帮帮我。我是Firebase的新手。 此外,我想知道如何查找搜索是否不成功。 Here's my database contents

这是我的数据转发代码。 Query类属于com.google.firebase,而不属于com.firebase.client。

                             @Override
                              public void onClick(View view) {
                                  key = scanContent;
                                  Query q = dbRef.child("idno").equalTo(key);
                                  if (key != null) {
                                      q.addListenerForSingleValueEvent(new com.google.firebase.database.ValueEventListener() {
                                          @Override
                                          public void onDataChange(com.google.firebase.database.DataSnapshot dataSnapshot) {
                                              Item i = dataSnapshot.getValue(Item.class);
                                              id.setText(scanContent);
                                              nam.setText(i.getName());
                                              brand.setText(i.getBrand());
                                              cost.setText(i.getCost());
                                              sell.setText("Add to list of sold items?");
                                          }

                                          @Override
                                          public void onCancelled(DatabaseError databaseError) {
                                              AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                                              builder.setMessage(databaseError.getMessage())
                                                      .setTitle("Error!")
                                                      .setPositiveButton(android.R.string.ok, null);
                                              AlertDialog dialog = builder.create();
                                              dialog.show();
                                          }
                                      });

                                  } else {
                                      search.setError("Enter a valid value");
                                  }
                              }

以下是我的logcat的内容。

09-24 13:32:26.332 9228-9228/com.strokx.user.stockmanager W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x40c03a68)
09-24 13:32:26.352 9228-9228/com.strokx.user.stockmanager E/UncaughtException: java.lang.NullPointerException
                                                                                   at com.strokx.user.stockmanager.SearchFragment$2$1.onDataChange(SearchFragment.java:106)
                                                                                   at com.google.firebase.database.Query$1.onDataChange(Unknown Source)
                                                                                   at com.google.android.gms.internal.zzaie.zza(Unknown Source)
                                                                                   at com.google.android.gms.internal.zzaje.zzcta(Unknown Source)
                                                                                   at com.google.android.gms.internal.zzajh$1.run(Unknown Source)
                                                                                   at android.os.Handler.handleCallback(Handler.java:605)
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:92)
                                                                                   at android.os.Looper.loop(Looper.java:137)
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:4517)
                                                                                   at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                   at java.lang.reflect.Method.invoke(Method.java:511)
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
                                                                                   at dalvik.system.NativeStart.main(Native Method)
09-24 13:32:26.572 9228-9228/com.strokx.user.stockmanager D/FirebaseCrashApiImpl: throwable java.lang.NullPointerException
09-24 13:32:26.672 9228-9228/com.strokx.user.stockmanager E/AndroidRuntime: FATAL EXCEPTION: main
                                                                            java.lang.NullPointerException
                                                                                at com.strokx.user.stockmanager.SearchFragment$2$1.onDataChange(SearchFragment.java:106)
                                                                                at com.google.firebase.database.Query$1.onDataChange(Unknown Source)
                                                                                at com.google.android.gms.internal.zzaie.zza(Unknown Source)
                                                                                at com.google.android.gms.internal.zzaje.zzcta(Unknown Source)
                                                                                at com.google.android.gms.internal.zzajh$1.run(Unknown Source)
                                                                                at android.os.Handler.handleCallback(Handler.java:605)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:92)
                                                                                at android.os.Looper.loop(Looper.java:137)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:4517)
                                                                                at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                at java.lang.reflect.Method.invoke(Method.java:511)
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)

3 个答案:

答案 0 :(得分:0)

我认为你的Item类需要一个默认的空构造函数。

答案 1 :(得分:0)

对Firebase数据库执行查询时,结果将是项目列表。即使您的查询只有一个匹配项,结果也会是一个列表。因此,您的代码需要通过循环dataSnapshot.getChildren()

来处理列表
Query q = dbRef.child("idno").equalTo(key);
if (key != null) {
    q.addListenerForSingleValueEvent(new com.google.firebase.database.ValueEventListener() {
        @Override
        public void onDataChange(com.google.firebase.database.DataSnapshot dataSnapshot) {
            (for DataSnapshot itemSnapshot: dataSnapshot.getChildren()) {
                Item i = dataSnapshot.getValue(Item.class);
                ...

我们最近添加了一个如何对Firebase documentation on filtering data with queries执行此操作的示例。

答案 2 :(得分:0)

修正了它。我的查询错了。虽然修复查询仍然不会使列表视图显示检索到的结果。我使用firebase ui list adapter来修复它。我已经在下面发布了正确的代码,如果有帮助的话。谢谢大家。

 dbRef= FirebaseDatabase.getInstance().getReferenceFromUrl("https://stockmanager-142503.firebaseio.com/Items");


        imgbtn.setOnClickListener(new View.OnClickListener() {
                                      @Override
                                      public void onClick(View view) {
                                          key = scanContent;
                                          Query q = dbRef.orderByChild("idno").equalTo(key);
                                          FirebaseListAdapter<Item> adapter =new FirebaseListAdapter<Item>(getActivity(),Item.class,R.layout.rowitem,q) {
                                              @Override
                                              protected void populateView(View v, Item item, int position) {
                                                  TextView text1=(TextView)v.findViewById(R.id.text1);
                                                  TextView text2=(TextView)v.findViewById(R.id.text2);
                                                  TextView text3=(TextView)v.findViewById(R.id.text3);
                                                  TextView text4=(TextView)v.findViewById(R.id.text4);
                                                  TextView text5=(TextView)v.findViewById(R.id.text5);
                                                  text1.setText(item.getIdno());
                                                  text2.setText(item.getName());
                                                  text3.setText(item.getBrand());
                                                  text4.setText(item.getCost());
                                                  text5.setText(item.getDate());
                                                  solditem=item;
                                              }
                                          };
                                          sell.setText("Add this result to Sold Items List?");
                                      sList.setAdapter(adapter);
                                      }
                                  });