如果数据没有插入到firebase中,我怎么能发出错误消息(这是我的代码)

Posted

技术标签:

【中文标题】如果数据没有插入到firebase中,我怎么能发出错误消息(这是我的代码)【英文标题】:how can i make error toast message if data is not inserted into firebase (this is my code) 【发布时间】:2022-01-08 14:01:53 【问题描述】:

这是我的二维码扫描仪

databaseReference.child(id).setValue(data).addOnCompleteListener(new OnCompleteListener<Void>() 
        @Override
        public void onComplete(@NonNull Task<Void> task) 
            onBackPressed();
            if (task.isSuccessful()) 
                Toast.makeText(getApplicationContext(), "DATA INSERTED", Toast.LENGTH_SHORT).show();
            
        
    );

成功的任务正在运行

【问题讨论】:

那里有一个if,那么else呢? 我试过把 else 但它不起作用 请展示您对 cutiko 建议的else 的尝试,以及您如何测试它。 我可以评论代码还是只编辑它? @FrankvanPuffelen @hsai 只需编辑您的问题。它下面有一个编辑按钮。也请回复@。 【参考方案1】:

在您的代码中,我看到您使用了错误的 onBackPressed();

databaseReference.child(id).setValue(data).addOnCompleteListener(new OnCompleteListener<Void>() 
    @Override
    public void onComplete(@NonNull Task<Void> task) 
        if (task.isSuccessful()) 
            Toast.makeText(getApplicationContext(), "DATA INSERTED", Toast.LENGTH_SHORT).show();
         else 
            // Error handling here
            Toast.makeText(getApplicationContext(), "DATA INSERT ERROR", Toast.LENGTH_SHORT).show();
        
        onBackPressed(); //This will exit the screen, you should consider whether to use it
    
);

更新: 我已经更新了代码,这是显示片段和活动吐司的最佳方式。我添加了 Log.d 来检查程序在哪里运行以及它是否正常工作

import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

public class BaseFragment extends Fragment 
    private static final int MSG_SHOW_TOAST = 1;

    void displayMessage(String message, boolean isLengthLong) 
        requireActivity().runOnUiThread(new Runnable() 
            @Override
            public void run() 
                int longShow = isLengthLong ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT;
                Toast.makeText(requireActivity(), message, longShow).show();
            
        );
    


public class Demo extends BaseFragment 
    private String TAG = getClass().getSimpleName();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        displayMessage("Ahihi", false);
    

    public void yourMethod() 
        databaseReference.child(id).setValue(data).addOnCompleteListener(new OnCompleteListener<Void>() 
            @Override
            public void onComplete(@NonNull Task<Void> task) 
                // Put the log here, Check the log outputted from mobile on android studio to make sure this is working
                Log.d(TAG, "yourMethod: onComplete: status task is " + task.isSuccessful());
                if (task.isSuccessful()) 
                    displayMessage("DATA INSERTED", true);
                 else 
                    // Put the log here, Check the log outputted from mobile on android studio to make sure this is working
                    Log.d(TAG, "yourMethod: onComplete: status task is ERROR ");
                    displayMessage("DATA INSERT ERROR", true);
                
                onBackPressed(); //This will exit the screen, you should consider whether to use it
            
        );
    

    @Override
    public void onDestroy() 
        super.onDestroy();
    

//=========================================== 下面的代码用于活动

public class BaseActivity extends AppCompatActivity 
    void displayMessage(String message, boolean isLengthLong) 
        runOnUiThread(new Runnable() 
            @Override
            public void run() 
                int longShow = isLengthLong ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT;
                Toast.makeText(getApplicationContext(), message, longShow).show();
            
        );
    


public class Demo extends BaseActivity 
    private String TAG = getClass().getSimpleName();

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        displayMessage("Ahihi", false);
    

    public void yourMethod() 
        databaseReference.child(id).setValue(data).addOnCompleteListener(new OnCompleteListener<Void>() 
            @Override
            public void onComplete(@NonNull Task<Void> task) 
                // Put the log here, Check the log outputted from mobile on android studio to make sure this is working
                Log.d(TAG, "yourMethod: onComplete: status task is " + task.isSuccessful());
                if (task.isSuccessful()) 
                    displayMessage("DATA INSERTED", true);
                 else 
                    // Put the log here, Check the log outputted from mobile on android studio to make sure this is working
                    Log.d(TAG, "yourMethod: onComplete: status task is ERROR ");
                    displayMessage("DATA INSERT ERROR", true);
                
                onBackPressed(); //This will exit the screen, you should consider whether to use it
            
        );
    

    @Override
    public void onDestroy() 
        super.onDestroy();
    

【讨论】:

我试过了,仍然没有错误敬酒先生 你能帮我发送错误吗 没有错误先生,如果二维码错误,它只是没有吐司消息 我更新了我的帖子,请您检查一下 谢谢你等我试试

以上是关于如果数据没有插入到firebase中,我怎么能发出错误消息(这是我的代码)的主要内容,如果未能解决你的问题,请参考以下文章

iOS-集成Firebase发送推送消息到App

geoQuery不输出数据怎么办?

如何为 Firebase 实时数据库上的单个查询启用离线功能?

在recyclerview kotlin中插入firebase快照

单击插入按钮后,我正在使用firebase实时数据库从我的html页面存储数据,但它没有在数据库中添加数据

我们可以将数据插入 Firebase 实时数据库吗?