我正在构建一个应用程序,其中一部分是向Firebase提交设备ID,如果提交了该设备ID,它将上传虚拟类设备状态,稍后将由NodeMCU更改和使用。 但是问题是,当我创建一个数据库引用并将设备ID放在firebase上时,如果尝试将另一个addOnCompleteListener放入其中以推送虚拟设备状态类,则会出现一些错误。
我提交代码的功能和提交代码时的虚拟设备状态对象。
void submitCode(String scannedCode){
code=scannedCode;
databaseReference.setValue(code).addOnCompleteListener(
this, new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()){
DeviceStatus deviceStatus = new DeviceStatus();
deviceStatus.deviceID = code;
FirebaseDatabase.getInstance().getReference("DeviceStatus").child(code).setValue(deviceStatus).addOnCompleteListener(
this, new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> subTask) {
if (subTask.isSuccessful()) {
Toast.makeText(getApplicationContext(), "Product ID Successfully Entered", Toast.LENGTH_LONG).show();
finish();
startActivity(new Intent(getApplicationContext(), switchStatus.class));
}
}
}
);
}
}
}
);
}
这里的databaseReference是一个全局变量,如下:
DatabaseReference databaseReference;
并初始化为
databaseReference = FirebaseDatabase.getInstance().getReference("DeviceStatus").child(devideID);
Android Studio编辑器警告说:
Cannot resolve method 'addOnCompleteListener(anonymous com.google.android.gms.tasks.OnCompleteListener<java.lang.Void>, anonymous com.google.android.gms.tasks.OnCompleteListener<java.lang.Void>)'
构建错误提示
error: no suitable method found for addOnCompleteListener(<anonymous OnCompleteListener<Void>>,<anonymous OnCompleteListener<Void>>)
method Task.addOnCompleteListener(Executor,OnCompleteListener<Void>) is not applicable
(argument mismatch; <anonymous OnCompleteListener<Void>> cannot be converted to Executor)
method Task.addOnCompleteListener(Activity,OnCompleteListener<Void>) is not applicable
(argument mismatch; <anonymous OnCompleteListener<Void>> cannot be converted to Activity)
更新的功能:
void submitCode(String scannedCode){
code=scannedCode;
databaseReference.setValue(code).addOnCompleteListener(
this, new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()){
// sendSwitchStatus(code);
DeviceStatus deviceStatus = new DeviceStatus();
deviceStatus.deviceID = code;
FirebaseDatabase.getInstance().getReference("DeviceStatus").child(code).setValue(deviceStatus).addOnCompleteListener( getBaseContext(), new OnCompleteListener<Void>(){
public void onComplete(@Nullable Task<void> subTask){
if (subTask.isSuccessful()) {
Toast.makeText(getApplicationContext(), "Product ID Successfully Registered", Toast.LENGTH_LONG).show();
finish();
startActivity(new Intent(getApplicationContext(), switchStatus.class));
}
}
});
}
}
}
);
}
答案 0 :(得分:1)
在第二次设置addOnCompleteListener
时使用活动上下文。
public class MainActivity extends AppCompatActivity {
private int toggle = 0;
DatabaseReference databaseReference;
Activity activity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
activity = this;
databaseReference = FirebaseDatabase.getInstance().getReference("DeviceStatus").child("");
}
void submitCode(String scannedCode){
String code = scannedCode;
databaseReference.setValue(code).addOnCompleteListener(this, new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
FirebaseDatabase.getInstance().getReference("DeviceStatus").child("").setValue("").addOnCompleteListener(activity, new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
}
});
}
});
}
}
由于对Activity
的引用有些困惑,因此我写了一些与您的示例代码类似的示例代码(仅将示例中的某些变量替换为“”)。请检查并随时询问任何进一步的信息。