简体   繁体   English

使用 getValue 调用 Firebase 时,在类 org.json.JSONObject 上找不到要序列化的属性

[英]No properties to serialize found on class org.json.JSONObject when calling Firebase with getValue

I have a simple Main Activity with a GridView and every item has a TextView which I want to fill with one of the contents of every item stored in Firebase database.我有一个带有 GridView 的简单 Main Activity,每个项目都有一个 TextView,我想用存储在 Firebase 数据库中的每个项目的内容之一填充它。 The problem emerges when I invoke getValue().当我调用 getValue() 时问题出现了。 The app crashes and I get the following error:该应用程序崩溃,我收到以下错误:

com.google.firebase.database.DatabaseException: No properties to serialize found on class org.json.JSONObject

I am using a custom Adapter I made to parse the data to the GridView.我正在使用我制作的自定义适配器将数据解析到 GridView。 My code is as follows:我的代码如下:

public class MainActivity extends AppCompatActivity {

     public JSONArray tables = new JSONArray();

     @Override
     protected void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.activity_main);

         GridView grid = (GridView) findViewById(R.id.table_grid);

         FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
         DatabaseReference databaseReference = firebaseDatabase.getReference("development/store_1/tables");
         databaseReference.child("items").addValueEventListener(new ValueEventListener() {
             @Override
             public void onDataChange(DataSnapshot dataSnapshot) {
                 Iterable<DataSnapshot> children = dataSnapshot.getChildren();

                 for (DataSnapshot child : children) {
                     JSONObject table = child.getValue(JSONObject.class);
                tables.put(table);
            }
        }

             @Override
             public void onCancelled(DatabaseError databaseError) {
                  String error = databaseError.getMessage().toString();
                  Toast.makeText(MainActivity.this, error, Toast.LENGTH_SHORT).show();
             }
         });

         TableAdapter tableAdapter = new TableAdapter(this, tables, 0);
         grid.setAdapter(tableAdapter);
     }
 }

It has been a long time since I solved this but I guess the answer can help others as well.我解决这个问题已经很长时间了,但我想答案也可以帮助其他人。 Instead of a valueEventListener I used tableGridAdapter and the adjacent onItemClickListener.我没有使用 valueEventListener,而是使用了 tableGridAdapter 和相邻的 onItemClickListener。 The code which solved my problem is the following (I am omiting parts of the code not involved in the problem):解决我的问题的代码如下(我省略了与问题无关的部分代码):

public class MainMenuActivity extends Activity {

    int pending = 0;
    JSONArray tables = new JSONArray();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        pending = getIntent().getIntExtra("Pending Table", 0);
        
        setContentView(R.layout.activity_main_menu_tables);
        GridView tablesGrid = (GridView) findViewById(R.id.main_menu_tables_grid);

        SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);

        FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();
        DatabaseReference databaseReference = firebaseDatabase.getReference(prefs.getString("mode", "pythonanywhere")
            + "/store_" + prefs.getInt("store_id", 0) + "/tables");
        final TableGridAdapter tableGridAdapter = new TableGridAdapter(this, databaseReference, pending);
        tablesGrid.setAdapter(tableGridAdapter);

        tablesGrid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                view.setSelected(true);
                tables = tableGridAdapter.getTables();

                try {
                    table = tables.getJSONObject(i).getInt("id");
                    tableName = tables.getJSONObject(i).getString("name");
                    availability = tables.getJSONObject(i).getInt("active");
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                flag = false;
                createMainMenu();
            }
        });
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Firebase Firestore 分布式计数器 - 未找到要序列化的属性 - Firebase Firestore Distributed Counter - No Properties to Serialize Found 我无法从 Firebase 的孩子那里获取值() - I am unable to getValue() from a child in Firebase Firebase 调用 Stripe 时函数出错 - Firebase Functions error when calling Stripe firebase:CONFIGURATION_NOT_FOUND - firebase : CONFIGURATION_NOT_FOUND 找不到 com.google.android.gms.providerinstaller.dynamite 的本地模块描述符 class。 Flutter Firebase - Local module descriptor class for com.google.android.gms.providerinstaller.dynamite not found. Flutter Firebase W/DynamiteModule:找不到 com.google.firebase.auth 的本地模块描述符类 - W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found 调用 firebase.messaging.getToken() 时出现“请求缺少身份验证凭据”错误 - "Request is missing authentication credential" error when calling firebase.messaging.getToken() 从 Firebase 函数调用 API 时出现“套接字挂断”错误 - Getting "Socket hang up" error when calling an API from Firebase functions 如何序列化 json firestore Timestamp to Date? - How to serialize a json firestore Timestamp to Date? 从连接到 Google Functions 的端点调用时,Firebase ID 令牌具有不正确的“aud”(受众)声明 - Firebase ID token has incorrect "aud" (audience) claim when calling from Endpoint connected to Google Functions
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM