2016-05-16 133 views
0

我的應用程序允許用戶在Google地圖上添加標記時添加有關地點的詳細信息。這些詳細信息存儲在Firebase中,其中的所有詳細信息都不爲空。Android/Firebase:爲什麼onDataChange()返回空值?

這裏是更新的樹:

{ 
    "Algiers" : { 
    "Description" : "Capital of Algeria", 
    "Latitude" : 36.77202162442747, 
    "Longitude" : 3.0560945346951485, 
    "Rating" : "9", 
    "Type" : "Town" 
    }, 
    "Brussels" : { 
    "Description" : "Capital of Belgium", 
    "Latitude" : 50.850103621427685, 
    "Longitude" : 4.3520765751600266, 
    "Rating" : "8", 
    "Type" : "Town" 
    }, 
    "Burger King" : { 
    "Description" : "Fast but not healthy food", 
    "Latitude" : 51.236331, 
    "Longitude" : -0.575385, 
    "Rating" : 5.29, 
    "Type" : "Restaurant" 
    }, 
    "Chippy Pizza" : { 
    "Description" : "Best American pizza ever", 
    "Latitude" : 36.72722332469427, 
    "Longitude" : 3.0647138133645058, 
    "Rating" : "10", 
    "Type" : "Restaurant" 
    }, 
    "European Parliament" : { 
    "Description" : "Some decisions about Europe are done there", 
    "Latitude" : 50.83868986886192, 
    "Longitude" : 4.375510700047016, 
    "Rating" : "8", 
    "Type" : "Museum" 
    }, 
    "Friary Center McDonalds" : { 
    "Description" : "Good food", 
    "Latitude" : 51.23544690507358, 
    "Longitude" : -0.5764660611748695, 
    "Rating" : "7.4", 
    "Type" : "Restaurant" 
    }, 
    "Guildford Town Center" : { 
    "Description" : "Town center of Guildford", 
    "Latitude" : 51.23622, 
    "Longitude" : -0.570409, 
    "Rating" : 8.74, 
    "Type" : "Town" 
    }, 
    "Guildfords Castle" : { 
    "Description" : "Very old castle", 
    "Latitude" : 51.23428622281793, 
    "Longitude" : -0.5723629519343376, 
    "Rating" : "8", 
    "Type" : "Museum" 
    }, 
    "Guildfords Hospital" : { 
    "Description" : "Treats all types of pathologies", 
    "Latitude" : 51.240506, 
    "Longitude" : -0.60838, 
    "Rating" : "9", 
    "Type" : "Hospital" 
    }, 
    "Guildfords Museum" : { 
    "Description" : "Museum with several exhibitions", 
    "Latitude" : 51.233797, 
    "Longitude" : -0.573549, 
    "Rating" : "7.84", 
    "Type" : "Museum" 
    }, 
    "Martyrs Monument" : { 
    "Description" : "Describes the History of Algeria", 
    "Latitude" : 36.745560268045196, 
    "Longitude" : 3.0698012933135033, 
    "Rating" : "10", 
    "Type" : "Museum" 
    }, 
    "Military Hospital" : { 
    "Description" : "Excellent Hospital", 
    "Latitude" : 36.71973881144668, 
    "Longitude" : 3.063882999122143, 
    "Rating" : "10", 
    "Type" : "Hospital" 
    }, 
    "Mount Alvernia Hospital" : { 
    "Description" : "Small and correct hospital", 
    "Latitude" : 51.23583085374619, 
    "Longitude" : -0.5647089332342148, 
    "Rating" : "7", 
    "Type" : "Hospital" 
    }, 
    "Mustapha Bacha Hospital" : { 
    "Description" : "Complete Hospital", 
    "Latitude" : 36.76243892522928, 
    "Longitude" : 3.053263798356056, 
    "Rating" : "7", 
    "Type" : "Hospital" 
    }, 
    "Nandos Guildford" : { 
    "Description" : "Tasty but expensive chicken", 
    "Latitude" : 51.236343270137674, 
    "Longitude" : -0.5743494629859924, 
    "Rating" : "9", 
    "Type" : "Restaurant" 
    }, 
    "Pachéco Hospital" : { 
    "Description" : "Excellent Hospital", 
    "Latitude" : 50.854205318946484, 
    "Longitude" : 4.349818155169487, 
    "Rating" : "10", 
    "Type" : "Hospital" 
    }, 
    "Paris" : { 
    "Description" : "Capital", 
    "Latitude" : "48.84191426189115", 
    "Longitude" : "2.3034103587269783", 
    "Rating" : "8", 
    "Type" : "Town" 
    }, 
    "Starbucks Coffee" : { 
    "Description" : "All types of coffee are found there", 
    "Latitude" : 51.235304, 
    "Longitude" : -0.575049, 
    "Rating" : 7.84, 
    "Type" : "Restaurant" 
    }, 
    "Test 2" : { 
    "Description" : "Test", 
    "Latitude" : "26.567103702363973", 
    "Longitude" : "2.967230938374996", 
    "Rating" : "8", 
    "Type" : "Town" 
    }, 
    "Test Overflow" : { 
    "Description" : "Test", 
    "Latitude" : "31.29925316955398", 
    "Longitude" : "2.439718544483185", 
    "Rating" : "10", 
    "Type" : "Town" 
    }, 
    "Test3" : { 
    "Description" : "Testf", 
    "Latitude" : "45.24608905762172", 
    "Longitude" : "3.4947332739830017", 
    "Rating" : "8", 
    "Type" : "Town" 
    } 
} 

我使用的火力地堡文檔檢索從我的數據庫中的數據(使用onDataChange())。

然而,我的應用程序崩潰並打印了我以下錯誤:

05-16 14:37:15.772 13332-13332/org.com1032.flagged_v2 E/AndroidRuntime: FATAL EXCEPTION: main 
                     Process: org.com1032.flagged_v2, PID: 13332 
                     java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference 
                      at org.com1032.flagged_v2.MainActivity$6.onDataChange(MainActivity.java:877) 
                      at com.firebase.client.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:56) 
                      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:739) 
                      at android.os.Handler.dispatchMessage(Handler.java:95) 
                      at android.os.Looper.loop(Looper.java:135) 
                      at android.app.ActivityThread.main(ActivityThread.java:5219) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at java.lang.reflect.Method.invoke(Method.java:372) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 

一些日誌後,我意識到,那個特定鍵的所有值都爲空。

下面是從火力地堡檢索數據的代碼:

firebaseMarkers.addValueEventListener(new ValueEventListener() { 
        @Override 
        public void onDataChange(DataSnapshot dataSnapshot) { 
         for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) { 

          String descPath = dataSnapshot1.getKey() + "/Description"; 
          String latPath = dataSnapshot1.getKey() + "/Latitude"; 
          String lonPath = dataSnapshot1.getKey() + "/Longitude"; 
          String ratePath = dataSnapshot1.getKey() + "/Rating"; 
          String typePath = dataSnapshot1.getKey() + "/Type"; 


          Log.v("INSERTING?", "MAYBE"); 
          Log.v("the KEY", dataSnapshot1.getKey()); 
          Log.v("TYPE:", dataSnapshot.child(typePath) + ""); 
          Log.v("NAME:", dataSnapshot1.getKey()); 
          Log.v("LATITUDE:", dataSnapshot.child(latPath).getValue().toString()); 
          Log.v("LONGITUDE:", dataSnapshot.child(lonPath).getValue().toString()); 
          Log.v("RATING:", dataSnapshot.child(ratePath).getValue().toString()); 
          Log.v("DESCRIPTION", dataSnapshot.child(descPath).getValue().toString()); 

           markersDatabase.insertData(dataSnapshot.child(typePath).getValue().toString(), dataSnapshot1.getKey(), 
            Double.parseDouble(dataSnapshot.child(latPath).getValue().toString()), Double.parseDouble(dataSnapshot.child(lonPath).getValue().toString()), 
            dataSnapshot.child(ratePath).getValue().toString(), dataSnapshot.child(descPath).getValue().toString()); 




          Log.v("INSERTED!", "YES"); 

         } 
        } 


        @Override 
        public void onCancelled(FirebaseError firebaseError) { 
         Log.v("Firebase Error", "Wow, you're asking very deep"); 
        } 
       }); 

每次我添加數據,一切都存儲在手機中的本地數據庫(所有重複之後被刪除)。

這裏是將數據添加到火力代碼:

fire.child(markerName.toString()).setValue(markerName.toString()); 
          fire.child(markerName.toString()).child("Description").setValue(markerDescription.toString()); 
          fire.child(markerName.toString()).child("Latitude").setValue(String.valueOf(point.latitude)); 
          fire.child(markerName.toString()).child("Longitude").setValue(String.valueOf(point.longitude)); 
          fire.child(markerName.toString()).child("Rating").setValue(markerRating.toString()); 
          fire.child(markerName.toString()).child("Type").setValue(markerType.toString()); 

          MarkerOptions marker = new MarkerOptions().position(new LatLng(point.latitude, point.longitude)).title(markerName); 
          map.addMarker(marker); 

          markersDatabase.insertData(markerType, markerName, point.latitude, point.longitude, markerRating, markerDescription); 

預先感謝您。

+2

您已經在您的問題中包含了JSON樹圖片的鏈接。請將實際的JSON替換爲文本,您可以通過點擊Firebase數據庫中的導出按鈕輕鬆獲取該文本。將JSON作爲文本可以搜索,使我們能夠輕鬆使用它來測試您的實際數據,並將其用於我們的答案中,並且通常只是一件好事。 –

+0

感謝您的建議,我更新了我的問題。 – Imad

+0

您的錯誤消息表明您在'MainActivity.java:877'的'null'引用上調用'toString()'。在那裏設置一個斷點並檢查確切的引用是否爲null。 –

回答

1

所以基本上,我自己找到了答案。

問題在於將數據插入到Firebase中。我正在使用setValue()方法,這與我使用的值不是最好的方法。

最好的方法是創建一個新的HashMap在那裏你會打算把你的路徑和值,如下面的例子:

Map<String, String> map2 = new HashMap<String, String>(); 
map2.put("Description", markerDescription); 
map2.put("Latitude", String.valueOf(point.latitude)); 
map2.put("Longitude", String.valueOf(point.longitude)); 
map2.put("Rating", markerRating); 
map2.put("Type", markerType); 
Firebase fire = new Firebase("********").child(markerName); 
fire.setValue(map2); 

這基本上是插入數據的最佳方式(至少在我的情況)。

+0

對不起,但我不明白答案...您是否意識到爲什麼Firebase開始按預期工作了?在我的情況下,我第一次NULL,但第二次和下一個沒有問題的工作 – Jaco

+0

我有同樣的問題。我寫信給Firebase,它正在讀取null。但是,我確信數據不是空的。問題是我作爲POJO編寫,不能使用地圖..... – tccpg288

+0

嗨@ tccpg288,我找到了一個解決方案:frank van puffelen建議看到如果您要調用onDataChange方法兩次,因爲第二次可以得到空值。另一種解決方案可能是添加處理程序new Handler()。postDelayed(new Runnable(){ @Override public void run(){*調用您的方法,其中有ondatachange */ } },1000); '如果'dataSnapshot.getValue()'爲空,你可以得到數據 – Jaco

0

同樣的事情發生在我身上。這些數據在Firebase中看起來完全相同。在調試時,我可以將數據提取出來,但是在運行時它會返回null。出於某種原因,將散列圖保存到Firebase的作品。

順便說一句,如果你使用你可以節省自己一些鑄造。

Map<String, Object> markerMap = new HashMap<String, Object>();