[英]Cannot return value from a method with void result type
I'm trying to get a specific value from Firebase database, and I did it before, but now when I'm trying something a bit different, it says "Cannot return value from a method with void result type". 我正在尝试从Firebase数据库中获取特定值,而我之前是这样做的,但是现在当我尝试一些不同的操作时,它说“无法从具有无效结果类型的方法中返回值”。
I can guess that I'm missing some programming rules because I'm a new Java programmer. 我可以猜到我缺少一些编程规则,因为我是一名新的Java程序员。
This is my code: 这是我的代码:
public int getVotesForEvent(final String event_title) {
firebaseRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.child("Votes").exists()) {
for (DataSnapshot event : dataSnapshot.child("Votes").getChildren()) {
String event_title2 = event.getKey();
if (event_title2.equals(event_title)) {
int count=Integer.parseInt(event.getValue().toString());
return count;
}
}
}
else {
int count = 0 ;
return count;
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
}
The error appears where I'm trying to return count. 错误出现在我要返回计数的位置。
After reading comments, this is my code and now the method doesn't effect the declared variable count, it keeps 0: 阅读注释后,这是我的代码,现在该方法不影响声明的变量计数,它保持0:
int count=0;
public int getVotesForEvent(final String event_title) {
firebaseRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.child("Votes").exists()) {
boolean flag=false;
for (DataSnapshot event : dataSnapshot.child("Votes").getChildren()) {
String event_title2 = event.getKey();
if (event_title2.equals(event_title)) {
flag=true;
Toast.makeText(EventInfo.this, ""+Integer.parseInt(event.child("event_votes").getValue().toString()), Toast.LENGTH_SHORT).show();
count=Integer.parseInt(event.child("event_votes").getValue().toString());
}
}
if(flag!=true)
{
count=0;
}
}
else
{
count=0;
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
return count;
}
What am I doing wrong now? 我现在在做什么错?
In java method with void
means that method return nothing. 在具有void
java方法中,该方法不返回任何内容。
onDataChange
return type is void
, you can not return value count
. onDataChange
返回类型为void
,无法返回值count
。
Remove return statement and Try saving count
value in a field if you need it 删除返回语句,并在需要时尝试将count
数值保存在字段中
Your onDataChange method is marked void, yet you are trying to return from it. 您的onDataChange方法被标记为无效,但是您正在尝试从中返回。 Remove the return lines and save the value in a variable for further usage. 删除返回行并将值保存在变量中以备将来使用。
As others have written, you cant return from void method type. 正如其他人所写的那样,您不能从void方法类型返回。 But i think I you are getting confused here because of following, there are two methods here in your code, 但我想我在这里感到困惑,是因为遵循以下规则,您的代码中有两种方法,
public int getVotesForEvent(final String event_title)
public void onDataChange(DataSnapshot dataSnapshot)
The first one will accept return type as integer (ie int). 第一个将接受返回类型为整数(即int)。 Other one expects no return type. 其他人期望没有返回类型。
The second method is where you have written 'return count' and hence it fails. 第二种方法是您编写“返回计数”的地方,因此失败了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.