Java DoucMont.getString未提供输出(Firestore)

Java DoucMont.getString未提供输出(Firestore),java,android,firebase,google-cloud-firestore,Java,Android,Firebase,Google Cloud Firestore,我刚刚编写了一个基本代码来从Firestore数据库中获取多个文档。任务正在获得成功,您可以在日志中看到document.getId()&document.getData()值,但在这之后,当我使用document.getString()时,它会给出一个空白输出。我一直在努力寻找我的错误,但我无法,我有一个项目提交明天到期。忽略以下给定代码中的所有textview声明: private FirebaseFirestore db; private int month,year; priva

我刚刚编写了一个基本代码来从Firestore数据库中获取多个文档。任务正在获得成功,您可以在日志中看到
document.getId()
&
document.getData()
值,但在这之后,当我使用
document.getString()
时,它会给出一个空白输出。我一直在努力寻找我的错误,但我无法,我有一个项目提交明天到期。忽略以下给定代码中的所有textview声明:

    private FirebaseFirestore db;
private int month,year;
private Calendar calendar;
private  String email;
private String name,points;
private int i,j;
private String q,p,r;
public String[][] data= new String[10][10];
private TextView rankingTextView,a,b;
public TextView t01,t02,t03,t10,t11,t12,t20,t21,t22,t30,t31,t32,t40,t41,t42,t50,t51,t52,t60,t61,t62,t70,t71,t72,t80,t81,t82,t90,t91,t92;

private static final String TAG = "MyApp";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_leaderboard);
    calendar=Calendar.getInstance();
    t01=(TextView)findViewById(R.id.t00);
    t02=(TextView)findViewById(R.id.t01);
    t03=(TextView)findViewById(R.id.t02);
    t10=(TextView)findViewById(R.id.t10);
    t11=(TextView)findViewById(R.id.t11);
    t12=(TextView)findViewById(R.id.t12);
    t20=(TextView)findViewById(R.id.t20);
    t21=(TextView)findViewById(R.id.t21);
    t22=(TextView)findViewById(R.id.t22);
    t30=(TextView)findViewById(R.id.t30);
    t31=(TextView)findViewById(R.id.t31);
    t32=(TextView)findViewById(R.id.t32);
    t40=(TextView)findViewById(R.id.t40);
    t41=(TextView)findViewById(R.id.t41);
    t42=(TextView)findViewById(R.id.t42);
    t50=(TextView)findViewById(R.id.t50);
    t51=(TextView)findViewById(R.id.t51);
    t52=(TextView)findViewById(R.id.t52);
    t60=(TextView)findViewById(R.id.t60);
    t61=(TextView)findViewById(R.id.t61);
    t62=(TextView)findViewById(R.id.t62);
    t70=(TextView)findViewById(R.id.t70);
    t71=(TextView)findViewById(R.id.t71);
    t72=(TextView)findViewById(R.id.t72);
    t80=(TextView)findViewById(R.id.t80);
    t81=(TextView)findViewById(R.id.t81);
    t82=(TextView)findViewById(R.id.t82);
    db= FirebaseFirestore.getInstance();
    email = getIntent().getStringExtra("email");
    month = calendar.get(Calendar.MONTH);
    month = month + 1;
    year = calendar.get(Calendar.YEAR);
    i=0;
    j=0;
    dothis();
    Toast.makeText(this, "MO", Toast.LENGTH_SHORT).show();
    t01.setText(data[0][0]);
    t02.setText(data[0][1]);
    t11.setText(data[1][0]);
    t12.setText(data[1][1]);
    t21.setText(data[2][0]);
    t22.setText(data[2][1]);
    t31.setText(data[3][0]);
    t32.setText(data[3][1]);
    t41.setText(data[4][0]);
    t42.setText(data[4][1]);
    t51.setText(data[5][0]);
    t52.setText(data[5][1]);
    }
private void dothis()
{i=0;
j=0;

    db.collection("users")
           // .whereGreaterThanOrEqualTo("points" + Integer.toString(month) + Integer.toString(year), "0")
            .orderBy("points" + Integer.toString(month) + Integer.toString(year)).limit(10)
            .get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    if (task.isSuccessful()) {
                        for (QueryDocumentSnapshot document : task.getResult()) {
                            if(document.exists()) {
                                Log.d(TAG, document.getId() + " => " + document.getData() + " ");
                                data[i][j] = document.getString("username");
                                data[i][j + 1] = document.getString("points" + Integer.toString(month) + Integer.toString(year));
                                i++;
                                Toast.makeText(Leaderboard.this, "OK", Toast.LENGTH_SHORT).show();
                            }
                        }
                    } else {
                        Log.d(TAG, "Error getting documents: ", task.getException());
                    }
                }
            });

}
私有FirebaseFirestore数据库;
私人int月,年;
私人日历;
私人字符串电子邮件;
私有字符串名称,点;
私人int i,j;
私有字符串q,p,r;
公共字符串[][]数据=新字符串[10][10];
私有文本视图排名文本视图,a、b;
公共文本视图t01、t02、t03、t10、t11、t12、t20、t21、t22、t30、t31、t32、t40、t41、t42、t50、t51、t52、t60、t61、t62、t70、t71、t72、t80、t81、t82、t90、t91、t92;
私有静态最终字符串TAG=“MyApp”;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_排行榜);
calendar=calendar.getInstance();
t01=(TextView)findViewById(R.id.t00);
t02=(TextView)findViewById(R.id.t01);
t03=(TextView)findViewById(R.id.t02);
t10=(TextView)findViewById(R.id.t10);
t11=(TextView)findViewById(R.id.t11);
t12=(TextView)findViewById(R.id.t12);
t20=(TextView)findViewById(R.id.t20);
t21=(TextView)findViewById(R.id.t21);
t22=(TextView)findViewById(R.id.t22);
t30=(文本视图)findViewById(R.id.t30);
t31=(文本视图)findViewById(R.id.t31);
t32=(文本视图)findViewById(R.id.t32);
t40=(TextView)findViewById(R.id.t40);
t41=(TextView)findViewById(R.id.t41);
t42=(TextView)findViewById(R.id.t42);
t50=(TextView)findViewById(R.id.t50);
t51=(TextView)findViewById(R.id.t51);
t52=(TextView)findViewById(R.id.t52);
t60=(TextView)findViewById(R.id.t60);
t61=(TextView)findViewById(R.id.t61);
t62=(TextView)findViewById(R.id.t62);
t70=(TextView)findViewById(R.id.t70);
t71=(TextView)findViewById(R.id.t71);
t72=(TextView)findViewById(R.id.t72);
t80=(TextView)findViewById(R.id.t80);
t81=(TextView)findViewById(R.id.t81);
t82=(TextView)findViewById(R.id.t82);
db=FirebaseFirestore.getInstance();
email=getIntent().getStringExtra(“电子邮件”);
month=calendar.get(calendar.month);
月=月+1;
year=calendar.get(calendar.year);
i=0;
j=0;
dothis();
Toast.makeText(这个“MO”,Toast.LENGTH_SHORT).show();
t01.setText(数据[0][0]);
t02.setText(数据[0][1]);
t11.setText(数据[1][0]);
t12.setText(数据[1][1]);
t21.setText(数据[2][0]);
t22.setText(数据[2][1]);
t31.setText(数据[3][0]);
t32.setText(数据[3][1]);
t41.setText(数据[4][0]);
t42.setText(数据[4][1]);
t51.setText(数据[5][0]);
t52.setText(数据[5][1]);
}
私人空间
{i=0;
j=0;
数据库收集(“用户”)
//。其中大于或等于(“点数”+整数.toString(月)+整数.toString(年),“0”)
.orderBy(“点”+整数.toString(月)+整数.toString(年))。限制(10)
.get()
.addOnCompleteListener(新的OnCompleteListener(){
@凌驾
未完成的公共void(@NonNull任务){
if(task.issusccessful()){
对于(QueryDocumentSnapshot文档:task.getResult()){
if(document.exists()){
Log.d(标记,document.getId()+“=>”+document.getData()+”);
数据[i][j]=document.getString(“用户名”);
数据[i][j+1]=document.getString(“点”+Integer.toString(月)+Integer.toString(年));
i++;
Toast.makeText(leadboard.this,“OK”,Toast.LENGTH_SHORT.show();
}
}
}否则{
Log.d(标记“获取文档时出错:”,task.getException());
}
}
});
}
}


请不要重复我的问题。在发布之前我搜索了很多。

根据您的评论,您说您何时调用以下代码行:

t01.setText(data[0][0]);
您的
t01
TextView没有设置任何内容,这是正常行为,因为来自Firebase数据库的数据是异步的。这意味着
onComplete()
方法在被调用后立即返回,它返回的
任务的回调将在稍后调用

无法保证需要多长时间。因此,在数据可用之前,可能需要几百毫秒到几秒钟的时间。由于此方法立即返回,因此您试图在
onComplete()
方法外部使用的
data[0][0]
变量的值尚未从回调中填充

基本上,您正在尝试同步使用来自异步API的值。那不是个好主意。您应该按照预期异步处理API


这个问题的一个快速解决方法是只在
onComplete()
方法内将所有这些文本设置为TextView。如果您需要在外部设置它们,我建议您深入到异步世界,并查看我的anwser的最后一部分,在这部分中,我已经解释了如何使用自定义回调来完成。您也可以查看此以便更好地理解。

Null表示没有与您要查找的名称对应的字段。我不会说Null,我会说空白值…它没有给出任何内容请添加您的数据库结构,并用@AlexMamo@AlexMamo完成!。那么您是说
document.getString(“用户名”)
in