У меня есть база данных Firebase, которая будет использоваться двумя Android проектами. Я пытаюсь добавить данные из одного проекта / приложения и получить данные в другом, следуя различным учебным пособиям на YouTube. Тем не менее, мне трудно получить данные. Вот снимок моей базы данных.
![enter image description here](https://i.stack.imgur.com/jWIvv.png)
Мое приложение загружает пользовательский интерфейс, но вылетает, когда я запускаю следующий код и нажимаю кнопку Get Data
.
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import org.w3c.dom.Attr;
import java.util.jar.Attributes;
import java.util.*;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
public class Main2Activity extends AppCompatActivity {
DatabaseReference reff;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
final TextView text1=(TextView)findViewById(R.id.textView);
final TextView text2=(TextView)findViewById(R.id.textView2);
final TextView text3=(TextView)findViewById(R.id.textView3);
final TextView text4=(TextView)findViewById(R.id.textView4);
final TextView text5=(TextView)findViewById(R.id.textView5);
final TextView text6=(TextView)findViewById(R.id.textView6);
final TextView text7=(TextView)findViewById(R.id.textView7);
final TextView text8=(TextView)findViewById(R.id.textView8);
final TextView text9=(TextView)findViewById(R.id.textView9);
final TextView text10=(TextView)findViewById(R.id.textView10);
final TextView text11=(TextView)findViewById(R.id.textView11);
final TextView text12=(TextView)findViewById(R.id.textView12);
final TextView text13=(TextView)findViewById(R.id.textView13);
final TextView text14=(TextView)findViewById(R.id.textView14);
Button btn=(Button)findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
reff= FirebaseDatabase.getInstance().getReference().child("Attributes").child("M0pSXmPNPbXa3vgQeuc");
reff.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
int attribute1=Integer.parseInt(dataSnapshot.child("attribute1").getValue().toString());
int attribute2=Integer.parseInt(dataSnapshot.child("attribute2").getValue().toString());
int attribute3=Integer.parseInt(dataSnapshot.child("attribute3").getValue().toString());
int attribute4=Integer.parseInt(dataSnapshot.child("attribute4").getValue().toString());
int attribute5=Integer.parseInt(dataSnapshot.child("attribute5").getValue().toString());
int attribute6=Integer.parseInt(dataSnapshot.child("attribute6").getValue().toString());
int attribute7=Integer.parseInt(dataSnapshot.child("attribute7").getValue().toString());
int attribute8=Integer.parseInt(dataSnapshot.child("attribute8").getValue().toString());
int attribute9=Integer.parseInt(dataSnapshot.child("attribute9").getValue().toString());
int attribute10=Integer.parseInt(dataSnapshot.child("attribute10").getValue().toString());
int attribute11=Integer.parseInt(dataSnapshot.child("attribute11").getValue().toString());
int attribute12=Integer.parseInt(dataSnapshot.child("attribute12").getValue().toString());
int attribute13=Integer.parseInt(dataSnapshot.child("attribute13").getValue().toString());
int attribute14=Integer.parseInt(dataSnapshot.child("attribute14").getValue().toString());
text1.setText(attribute1);
text2.setText(attribute2);
text3.setText(attribute3);
text4.setText(attribute4);
text5.setText(attribute5);
text6.setText(attribute6);
text7.setText(attribute7);
text8.setText(attribute8);
text9.setText(attribute9);
text10.setText(attribute10);
text11.setText(attribute11);
text12.setText(attribute12);
text13.setText(attribute13);
text14.setText(attribute14);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
});
}
}
Я получаю следующую ошибку: -
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.appdoctor, PID: 4920
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
at com.example.appdoctor.Main2Activity$1$1.onDataChange(Main2Activity.java:56)
at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@16.0.4:75)
at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@16.0.4:63)
at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@16.0.4:55)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Process 4920 terminated.
То, что я пробовал: -
- Добавлены google-сервисы. json file
- Добавлено
classpath 'com.google.gms:google-services:4.3.3'
в Project Gradle - Добавлено
apply plugin: 'com.google.gms.google-services'
в Build Gradle - Изменены переменные
attribute1
- attribute10
с String
на Integer
Любая помощь очень ценится. Спасибо!