【问题标题】:No properties to serialize found on class org.json.JSONObject when calling Firebase with getValue使用 getValue 调用 Firebase 时,在类 org.json.JSONObject 上找不到要序列化的属性
【发布时间】:2017-07-28 19:02:08
【问题描述】:

我有一个带有 GridView 的简单主活动,每个项目都有一个 TextView,我想用存储在 Firebase 数据库中的每个项目的内容之一来填充它。当我调用 getValue() 时,问题就出现了。应用程序崩溃,我收到以下错误:

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

我正在使用我制作的自定义适配器将数据解析到 GridView。我的代码如下:

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);
     }
 }

【问题讨论】:

  • 您能分享一下您的数据库结构吗?

标签: java android json firebase firebase-realtime-database


【解决方案1】:

我已经很久没有解决这个问题了,但我想答案也可以帮助其他人。我使用 tableGridAdapter 和相邻的 onItemClickListener 而不是 valueEventListener。解决我的问题的代码如下(我省略了与问题无关的部分代码):

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();
            }
        });
    }

}

【讨论】:

    猜你喜欢
    • 2016-10-11
    • 2018-02-25
    • 2020-05-07
    • 1970-01-01
    • 2019-05-17
    • 2017-04-03
    • 2018-03-08
    • 1970-01-01
    相关资源
    最近更新 更多