当我点击一个项目打开详细活动时,我得到了一个nullpointer错误,并设法找出哪个变量给出了null,mContactItemDetail。我从onCreate中的Firebase数据快照中检索数据,并尝试将其传递给全局变量mContactItemDetail,它似乎是否通过。当我尝试调用mContactItemDetail时,我得到一个空值。有人能帮助我理解为什么,我错过了什么?
public class DetailActivityFragment extends Fragment {
private static final String LOG_TAG = DetailActivityFragment.class.getSimpleName();
private DatabaseReference databaseRef;
private String mContactID;
private ContactItemDetail mContactItemDetail;
private TextView textViewName;
private TextView textViewNumber;
private TextView textViewLocation;
private TextView textViewDescription;
public DetailActivityFragment() {
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
databaseRef = FirebaseDatabase.getInstance().getReference();
Bundle arg = getArguments();
mContactID = arg.getString(Constants.DETAIL_CONTACT_ID);
Log.v(LOG_TAG, "ContactID " + mContactID);
databaseRef.child(Constants.FIREBASE_LOCATION_DETAIL_LIST).child(mContactID). addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.v(LOG_TAG, "Datasnapshot: " + dataSnapshot.getValue());
ContactItemDetail contactItem = dataSnapshot.getValue(ContactItemDetail.class);
if (contactItem != null) {
mContactItemDetail = contactItem;
}
Log.v(LOG_TAG, "contactdetail 1: " + mContactItemDetail);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_detail, container, false);
initializeDetailScreen(rootView);
Log.v(LOG_TAG, "contactdetail 2: " + mContactItemDetail);
// textViewName.setText(mContactItemDetail.getContactName());
// textViewNumber.setText(Long.toString(mContactItemDetail.getTel()));
// textViewLocation.setText(mContactItemDetail.getLocation());
// textViewDescription.setText(mContactItemDetail.getDescription());
return rootView;
}
public void initializeDetailScreen(View view) {
textViewName = (TextView) view.findViewById(R.id.detail_textview_name);
textViewNumber = (TextView) view.findViewById(R.id.detail_textview_contact_number);
textViewLocation = (TextView) view.findViewById(R.id.detail_textview_location);
textViewDescription = (TextView) view.findViewById(R.id.detail_textview_description);
}
}
onCreate中的mContactItemDetail的Logcat返回值
08-09 22:08:11.929 5234-5234 / co.house.myapp V / DetailActivityFragment:contactdetail 1:co.house.myapp.model.ContactItemDetail@41ff1c18
但onCreateView中的mContactItemDetail的Logcat返回null
08-09 22:08:11.859 5234-5234 / co.house.myapp V / DetailActivityFragment:contactdetail 2:null
和mContactItemDetail类如下
public class ContactItemDetail {
private String contactName;
private String description;
private long fax;
private String location;
private String postalAddress;
private long rating;
private long tel;
public ContactItemDetail() {
}
public ContactItemDetail(String contactName, String description, long fax, String location,
String postalAddress, long rating, long tel) {
this.contactName = contactName;
this.description = description;
this.fax = fax;
this.location = location;
this.postalAddress = postalAddress;
this.rating = rating;
this.tel = tel;
}
public String getContactName() {
return contactName;
}
public String getDescription() {
return description;
}
public long getFax() {
return fax;
}
public String getLocation() {
return location;
}
public String getPostalAddress() {
return postalAddress;
}
public long getRating() {
return rating;
}
public long getTel() {
return tel;
}
}
这是我在onCreateView中注释掉textViewNumber时得到的错误日志,因为我相信空值
08-09 22:01:13.359 6462-6462/? E/SamsungIME: mOCRHelper is null
08-09 22:01:13.649 6462-6462/? E/SamsungIME: mOCRHelper is null
08-09 22:01:13.869 612-612/? E/asset: Creating SharedZip 0x5a0e8d08 /mnt/asec/com.truecaller-1/pkg.apk
08-09 22:01:13.869 612-612/? E/asset: !@+++ opening zip '/mnt/asec/com.truecaller-1/pkg.apk' by system_server(system_server)
08-09 22:01:14.669 612-620/? E/asset: !@+++ closed '/mnt/asec/com.truecaller-1/pkg.apk' by system_server(FinalizerDaemon)
08-09 22:01:15.879 612-612/? E/asset: Creating SharedZip 0x5f8c4640 /mnt/asec/com.truecaller-1/pkg.apk
08-09 22:01:15.889 612-612/? E/asset: !@+++ opening zip '/mnt/asec/com.truecaller-1/pkg.apk' by system_server(system_server)
08-09 22:01:22.189 4744-4744/? E/NetworkScheduler.SR: Invalid parameter app
08-09 22:01:22.189 4744-4744/? E/NetworkScheduler.SR: Invalid package name : Perhaps you didn't include a PendingIntent in the extras?
08-09 22:01:22.209 28564-8748/? E/Drive.UninstallOperation: Package still installed co.house.myapp
08-09 22:01:22.549 612-796/? E/Watchdog: !@Sync 4195
08-09 22:01:22.929 2863-2875/? E/SPPClientService: ============PushLog. commonIsShipBuild. stop!
08-09 22:01:22.929 2863-2875/? E/SPPClientService: [PushClientApplication] Push log off : This is Ship build version
08-09 22:01:23.679 2843-2843/? E/memtrack: Couldn't load memtrack module (No such file or directory)
08-09 22:01:23.679 2843-2843/? E/android.os.Debug: failed to load memtrack module: -2
08-09 22:01:23.679 2811-2811/? E/memtrack: Couldn't load memtrack module (No such file or directory)
08-09 22:01:23.679 2811-2811/? E/android.os.Debug: failed to load memtrack module: -2
08-09 22:01:25.649 2917-2917/? E/memtrack: Couldn't load memtrack module (No such file or directory)
08-09 22:01:25.649 2917-2917/? E/android.os.Debug: failed to load memtrack module: -2
答案 0 :(得分:2)
只需等待Firebase方法完成即可。不要试图立即使用变量结果。
private void showContactItemDetail(ContactItemDetail item) {
if (item == null) return; // Might want to Log here
textViewName.setText(item.getContactName());
textViewNumber.setText(Long.toString(item.getTel()));
textViewLocation.setText(item.getLocation());
textViewDescription.setText(item.getDescription());
}
然后,您可以在onCreateView
中使用该方法。否则,当您设置视图的详细信息时,您的视图也会出现空错误。
话虽如此,除了设置onCreate
之外,其他所有人都不需要databaseRef
。
View rootView = inflater.inflate(R.layout.fragment_detail, container, false);
initializeDetailScreen(rootView);
databaseRef.child(Constants.FIREBASE_LOCATION_DETAIL_LIST).child(mContactID). addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
mContactItemDetail = dataSnapshot.getValue(ContactItemDetail.class);
showContactItemDetail(mContactItemDetail);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
return rootView;