"GooglePlayServices not available due to error 9"运行集成了 Firestore 的应用程序时



我刚刚创建了一个从Firestore数据库检索数据的应用程序。当我尝试运行这个应用程序时,它总是崩溃。

Logcat(错误(:

GooglePlayServices not available due to error 9

这是我的Java代码:

TextView ret;
FirebaseFirestore db = FirebaseFirestore.getInstance();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Objects.requireNonNull(getSupportActionBar()).hide();
ret = findViewById(R.id.ret);
DocumentReference documentReference = db.collection("users").document("user");
documentReference.addSnapshotListener((documentSnapshot, e) -> {
if (e != null) {
Toast.makeText(MainActivity.this, "An Error Occurred while connecting to the database", Toast.LENGTH_SHORT).show();
}
if (documentSnapshot != null && documentSnapshot.exists()) {
ret.setText(Objects.requireNonNull(Objects.requireNonNull(documentSnapshot.getData()).get("updatedValue")).toString());
}
});

XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/ret"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textSize="50sp"
android:textAlignment="center"
android:layout_marginTop="60dp"/>
</LinearLayout>

清单:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

问题出在模拟器中。使用您的手机进行尝试,并将应用程序的Android版本更改为与您的手机兼容。

最新更新