英文:
Firebase on Android : DataSnapshot.getValue() causes current activity to end
问题
这是我的用户类:
public class User {
public String name;
public int number;
public String title;
public String company;
public String location;
public Bitmap image;
public String name_title_company_location;
public static int uid = 0;
public User() {
// DataSnapshot.getValue(User.class) 需要默认构造函数
}
public User(String name, int number, String title, String company, String location, Bitmap image) {
this.name = name;
this.number = number;
this.title = title;
this.company = company;
this.location = location;
this.image = image;
this.name_title_company_location = name + "_" + title + "_" + company + "_" + location;
uid++;
}
}
这是我的Firebase数据库:
users
1
company: "Indifair"
image
location: "Bangalore"
name: "Anupam Singh"
name_title_company_location: "Anupam Singh_Engineer_Indifair_Bangalore"
number: 956156494
title: "Engineer"
我正在使用以下代码从数据库中读取数据:
public class QueryActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_query);
Button bt = (Button) findViewById(R.id.button3);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
EditText t1 = (EditText) findViewById(R.id.editText10);
EditText t2 = (EditText) findViewById(R.id.editText11);
EditText t3 = (EditText) findViewById(R.id.editText13);
EditText t4 = (EditText) findViewById(R.id.editText14);
final String s1 = t1.getText().toString();
final String s2 = t2.getText().toString();
final String s3 = t3.getText().toString();
final String s4 = t4.getText().toString();
mDatabase.child("users").orderByChild("name_title_company_location")
.equalTo(s1 + "_" + s2 + "_" + s3 + "_" + s4)
.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot userSnapshot : dataSnapshot.getChildren()) {
User user = userSnapshot.getValue(User.class);
}
}
public void onCancelled(DatabaseError databaseError) {
// 在Logcat中没有错误或异常信息
}
});
}
});
}
}
当我点击按钮3时,当前活动结束,控制权返回到父活动。如果我将以下行注释掉:
User user = userSnapshot.getValue(User.class);
问题就消失了。
在build.gradle
中,我已经添加了正确的依赖项。
可能出了什么问题?
英文:
This is my user class :
public class User {
public String name;
public int number;
public String title;
public String company;
public String location;
public Bitmap image;
public String name_title_company_location;
public static int uid = 0;
public User() {
// Default constructor required for calls to DataSnapshot.getValue(User.class)
}
public User(String name, int number, String title, String company, String location, Bitmap image) {
this.name = name;
this.number = number;
this.title = title;
this.company = company;
this.location = location;
this.image = image;
this.name_title_company_location = name + "_" + title + "_" +company + "_" + location;
uid++;
}
}
This is my Firebase databse :
users
1
company:"Indifair"
image
location:"Bangalore"
name:"Anupam Singh"
name_title_company_location:"Anupam Singh_Engineer_Indifair_Bangalore"
number:956156494
title:"Engineer"
I am using this code to read from database :
public class QueryActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_query);
Button bt = (Button) findViewById(R.id.button3);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();
EditText t1 = (EditText) findViewById(R.id.editText10);
EditText t2 = (EditText) findViewById(R.id.editText11);
EditText t3 = (EditText) findViewById(R.id.editText13);
EditText t4 = (EditText) findViewById(R.id.editText14);
final String s1 = t1.getText().toString();
final String s2 = t2.getText().toString();
final String s3 = t3.getText().toString();
final String s4 = t4.getText().toString();
mDatabase.child("users").orderByChild("name_title_company_location")
.equalTo(s1 + "_" + s2 + "_" + s3 + "_" + s4)
.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot userSnapshot : dataSnapshot.getChildren()) {
User user = userSnapshot.getValue(User.class);
}
}
public void onCancelled(DatabaseError databaseError) {
// Getting Post failed, log a message
//Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
}
});
}
});
}
}
When I click button3 current activity ends and control goes to parent activity. If I comment out
User user = userSnapshot.getValue(User.class);
the problem is gone.
There is no error or exception in Logcat. I have added correct dependencies in build.gradle.
What could be going wrong?
答案1
得分: 1
我认为用户类应该扩展到某个类。例如,`public User extends.....() {
// DataSnapshot.getValue(User.class)所需的默认构造函数
}
public User(String name, int number, String title, String company, String location, Bitmap image) {
this.name = name;
this.number = number;.....`
英文:
I thought the user class should extend to something. e.g `public User extends.....() {
// Default constructor required for calls to DataSnapshot.getValue(User.class)
}
public User(String name, int number, String title, String company, String location, Bitmap image) {
this.name = name;
this.number = number;.....`
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论