Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/196.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 安卓,Can';t在查询数据后初始化对象';他来自火场_Android_Database_Firebase_Object_Google Cloud Firestore - Fatal编程技术网

Android 安卓,Can';t在查询数据后初始化对象';他来自火场

Android 安卓,Can';t在查询数据后初始化对象';他来自火场,android,database,firebase,object,google-cloud-firestore,Android,Database,Firebase,Object,Google Cloud Firestore,我正在尝试在android上制作一款简单的老式浏览器游戏,比如Tanoth/OGame。我使用CloudFireStore数据库,但我有一个问题。 在登录活动之后,我传入包含用户字符名的字符串。因此,我希望通过一个简单的查询从数据库中获取my PGCharacter对象。在类的开头,我声明了一个PGCharacter变量,但没有初始化它 在onCreate方法中,我初始化存储所有字符的数据库和集合。我创建了一个带有extrasting的文档引用,因为我的所有文档ID都与播放器用户名相同。之后,我

我正在尝试在android上制作一款简单的老式浏览器游戏,比如Tanoth/OGame。我使用CloudFireStore数据库,但我有一个问题。 在登录活动之后,我传入包含用户字符名的字符串。因此,我希望通过一个简单的查询从数据库中获取my PGCharacter对象。在类的开头,我声明了一个PGCharacter变量,但没有初始化它

在onCreate方法中,我初始化存储所有字符的数据库和集合。我创建了一个带有extrasting的文档引用,因为我的所有文档ID都与播放器用户名相同。之后,我进行查询,如果找到快照,我想用快照结果初始化PGCharacter对象。问题是,每次我想打印我的对象时,它都会崩溃。我希望我能很好地解释这个问题。 我怎样才能解决它?我甚至尝试添加一个外部函数,但没有成功。 我的测试用的是一个已经存在的播放器

这是我的班级代码:

package com.github.albertocoder97.archadia;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;

public class CharacterActivity extends AppCompatActivity {

private FirebaseFirestore db;
private CollectionReference characterCollection;

private PGCharacter pgCharacter;

private TextView usernameTextView;
private TextView goldTextView;
private TextView gemsTextView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_character);

    //Initialize the database
    db = FirebaseFirestore.getInstance();
    //Get characters database
    characterCollection = db.collection("characters");

    Intent intent = getIntent();
    String characterName = intent.getStringExtra("CHARACTER_NAME");

    Log.i("myFilter", "Intent input: " + characterName);

    //Initializing PGCharacter Object
    DocumentReference charDocReference = characterCollection.document(characterName);
    Log.i("myFilter", "ID: " + charDocReference.getId());
    charDocReference.get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
        @Override
        public void onSuccess(DocumentSnapshot documentSnapshot) {
            Log.i("myFilter", "Inner Gold DocumentSnapshot: " + documentSnapshot.get("gold").toString());
            setPGCharacter(documentSnapshot);
        }
    });

}




public void setPGCharacter(DocumentSnapshot snapshot){
    PGCharacter temp = snapshot.toObject(PGCharacter.class);
    pgCharacter = temp;
}
package com.github.albertocoder97.archadia;
导入android.content.Intent;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.util.Log;
导入android.widget.TextView;
导入com.google.android.gms.tasks.OnSuccessListener;
导入com.google.firebase.firestore.CollectionReference;
导入com.google.firebase.firestore.DocumentReference;
导入com.google.firebase.firestore.DocumentSnapshot;
导入com.google.firebase.firestore.FirebaseFirestore;
公共类CharacterActivity扩展了AppCompatActivity{
私有FirebaseFirestore数据库;
私人收藏参考字符收藏;
私人PGCharacter PGCharacter;
私有文本视图用户名文本视图;
私有文本视图goldTextView;
私有文本视图;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_字符);
//初始化数据库
db=FirebaseFirestore.getInstance();
//获取字符数据库
characterCollection=db.collection(“字符”);
Intent=getIntent();
String characterName=intent.getStringExtra(“字符名称”);
Log.i(“myFilter”,“意图输入:”+字符名);
//初始化PGCharacter对象
DocumentReference charDocReference=characterCollection.document(characterName);
Log.i(“myFilter”,“ID:+charDocReference.getId());
charDocReference.get().addOnSuccessListener(新OnSuccessListener()){
@凌驾
成功时公共无效(文档快照文档快照){
Log.i(“myFilter”,“内部Gold DocumentSnapshot:”+DocumentSnapshot.get(“Gold”).toString());
setPGCharacter(documentSnapshot);
}
});
}
公共void setPGCharacter(文档快照快照){
PGCharacter temp=snapshot.toObject(PGCharacter.class);
pgCharacter=temp;
}
}

p.p.s我尝试登录snapshot.getData(),如果有人问我,我会得到我所有的PGCharacter数据,在我的PGCharacter类中,我有一个公共空构造函数和每个属性的所有get。

删除这一行

pgCharacter = temp;

问题::PGCharacter类我有一个公共空构造函数和每个属性的所有get。重要提示:每个自定义类必须有一个不带参数的公共构造函数。此外,该类必须为每个属性包含一个公共getter。我的类有一个不带参数的公共构造函数,并且还具有所有属性的所有公共getter。我在帖子中写道:)问题不在于它没有创建对象。如果我把它放在一个temp变量中,它就工作了。问题是,如果我把它放在“private PGCharacter PGCharacter”中,它就不起作用了;那是因为你已经声明了它twiceWhere?我在onCreation之前声明过一次,就这样。我在哪里再次声明了它?如果我删除了它,如何将对象传递给pgCharacter?我无法从外部方法访问temp。请尝试并告诉我它是否修复了它。它没有崩溃,但pgCharacter未初始化,我无法访问temp。我想从数据库检索的对象获取数据,以设置显示文本。关键是,如果我试图从查询内部初始化pgCharacter,它会崩溃。如果我尝试使用另一个函数,则无法访问其中的变量temp。我已将其修复为将pgCharacter从private更改为public,无论如何感谢您的帮助:)