添加实时数据库后活动崩溃

时间:2018-10-22 14:44:24

标签: java android firebase firebase-realtime-database

我有一个活动,在我的仪表中使用自定义库,这是我的代码

package com.smart_farming.smart_farming_app;

public class DeviceCards extends AppCompatActivity {

    View view =  null;
    private pl.pawelkleczkowski.customgauge.CustomGauge moistureGauge, humidityGauge, temperatureGauge;
    int soil;
    int humidity;
    int temp;

    private static final String TAG = "DeviceCards";

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

        moistureGauge = view.findViewById(R.id.soil_moisture_gauge);
        humidityGauge = view.findViewById(R.id.humidity_gauge);
        temperatureGauge = view.findViewById(R.id.temp_gauge);
        TextView lols = view.findViewById(R.id.textView2);

        FirebaseDatabase database = FirebaseDatabase.getInstance();
        DatabaseReference myRef = database.getReference("soilMoisture").child("moisture");

        myRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                String value = dataSnapshot.getValue(String.class);
                int  resultSoil = Integer.parseInt(value);
                moistureGauge.setValue(resultSoil);
            }

            @Override
            public void onCancelled(DatabaseError error) {
                // Failed to read value
                Log.w(TAG, "Failed to read value.", error.toException());
            }
        });
    }
}

这是我的Firebase架构

schema

这也是正常的

enter image description here

现在有错误,但当我打开应用程序时活动崩溃。

1 个答案:

答案 0 :(得分:0)

更改自

moistureGauge = view.findViewById(R.id.soil_moisture_gauge);
humidityGauge = view.findViewById(R.id.humidity_gauge);
temperatureGauge = view.findViewById(R.id.temp_gauge);
TextView lols = view.findViewById(R.id.textView2);

收件人

moistureGauge = findViewById(R.id.soil_moisture_gauge);
humidityGauge = findViewById(R.id.humidity_gauge);
temperatureGauge = findViewById(R.id.temp_gauge);
TextView lols = findViewById(R.id.textView2); 

由于其活动findViewById足够。