2016-08-09 38 views
-3

当我点击一个项目以打开详细信息活动时,我收到一个空指针错误,并设法弄清楚哪个变量正在给出null,mContactItemDetail。我从onCreate中的Firebase datasnapshot中检索数据并尝试将其传递给全局变量mContactItemDetail,该变量似乎已通过或未通过。当我尝试调用mContactItemDetail时,我得到一个空值。有人能帮助我理解为什么,我错过了什么?在的onCreatejava.lang.NullPointerException但分配了值,Firebase datasnapshot

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); 
    } 
} 

logcat的用于mContactItemDetail返回一个值

08-09 22:08:11.929 5234-5234/co.house.myapp V/DetailActivityFragment:contactdetail 1:co.house.myapp .model.ContactItemDetail @ 41ff1c18

但对于logcat中在mContactItemDetail返回onCreateView空

08-09 22:08:11.859 5234-5234/co.house.myapp V/DetailActivityFragment:contactdetail 2:空

和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: [email protected]+++ opening zip '/mnt/asec/com.truecaller-1/pkg.apk' by system_server(system_server) 
08-09 22:01:14.669 612-620/? E/asset: [email protected]+++ 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: [email protected]+++ 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: [email protected] 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 
+1

您的变量为空,直到firebase方法**完成**。这是异步代码,适合你。 –

回答

2

只需等待火力地堡的方法来完成。不要试图立即使用变量结果。

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; 
+0

你的解决方案工作,但我不知道我理解异步在这里,我的错误是什么。纠正我,如果我错了,你会说我的代码onDataChange()方法与onCreateView()方法同时运行(这将是我认为的异步)。我想在onDataChange()可以为mContactItemDetail(未完成)指定值之前访问mContactItemDetail。这是我的错误。我对编程仍然很陌生,所以试图深入了解原理 – ndheti

+0

是的,基本上这就是发生了什么。在其他代码运行时,您正在等待网络请求(异步) –

相关问题