早安,
我试图从Firebase中获取数据,但不幸的是,它加载了Intent但崩溃了。
这是我的代码
这是我生成的主要Java类。
Voter_SSC_President.java
DatabaseReference databaseReferenceSSCPres;
ListView listViewSSCPresident;
List<SSC_Presidents> sscPresList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_voter__ssc__president);
databaseReferenceSSCPres = FirebaseDatabase.getInstance().getReference("candidate");
listViewSSCPresident = findViewById(R.id.listViewArtists);
sscPresList = new ArrayList<>();
}
@Override
protected void onStart() {
super.onStart();
databaseReferenceSSCPres.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
sscPresList.clear();
for (DataSnapshot SSCPresSnapshot : dataSnapshot.getChildren())
{
SSC_Presidents SSCPres = SSCPresSnapshot.getValue(SSC_Presidents.class);
sscPresList.add(SSCPres);
}
SSCPresLists adapter = new SSCPresLists(Voter_SSC_President.this, sscPresList);
listViewSSCPresident.setAdapter(adapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
这是Java类,倾向于从Firebase获取数据
SSC_Presidents.java
String candidate_platform;
String college_id;
String council_id;
String first_name;
String last_name;
String middle_name;
String partylist_id;
String position_id;
String school_year;
String student_id;
public SSC_Presidents()
{
}
public String getCandidate_platform() {
return candidate_platform;
}
public String getCollege_id() {
return college_id;
}
public String getCouncil_id() {
return council_id;
}
public String getFirst_name() {
return first_name;
}
public String getLast_name() {
return last_name;
}
public String getMiddle_name() {
return middle_name;
}
public String getPartylist_id() {
return partylist_id;
}
public String getPosition_id() {
return position_id;
}
public String getSchool_year() {
return school_year;
}
public String getStudent_id() {
return student_id;
}
这是我的充气机Java类。
SSCPresList.java
private Activity context;
private List<SSC_Presidents> SSC_PresidentsList;
public SSCPresLists(Activity context, List<SSC_Presidents> SSC_PresidentsList )
{
super(context, R.layout.listview_voter_ssc_president, SSC_PresidentsList);
this.context = context;
this.SSC_PresidentsList = SSC_PresidentsList;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View listViewItem = inflater.inflate(R.layout.listview_voter_ssc_president, null, false);
TextView textViewLast_name = (TextView) listViewItem.findViewById(R.id.list_last_name);
TextView textViewFirst_middle_name = (TextView) listViewItem.findViewById(R.id.list_first_middle_name);
TextView textViewPartylist = (TextView) listViewItem.findViewById(R.id.list_partylist);
TextView textPlatform = (TextView) listViewItem.findViewById(R.id.list_platform);
SSC_Presidents sscPresidents = SSC_PresidentsList.get(position);
textViewLast_name.setText(sscPresidents.getLast_name());
textViewFirst_middle_name.setText(sscPresidents.getFirst_name()+", "+sscPresidents.getMiddle_name());
textViewPartylist.setText(sscPresidents.getPartylist_id());
textPlatform.setText(sscPresidents.getCandidate_platform());
return listViewItem;
}
这是我的Firebase结构。 Firebase Structure
我在youtube教程中模仿了此代码。当我运行他的代码版本时,我获得了成功。但是当我修改它时,我没有成功
预先感谢您的回答。
编辑。
这是我的Logcat
09-23 21:28:00.119 17620-17620 / com.example.vj.plmonlinevoting
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.vj.plmonlinevoting, PID: 17620
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at com.example.vj.plmonlinevoting.Voter_SSC_President$1.onDataChange(Voter_SSC_President.java:60)
at com.google.android.gms.internal.zzegf.zza(Unknown Source)
at com.google.android.gms.internal.zzeia.zzbyc(Unknown Source)
at com.google.android.gms.internal.zzeig.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5318)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:922)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:717)