Java 使用MVVM获取空对象引用&;火库
我尝试在我的项目中使用MVVM已经很多天了。但却无法实现 我的代码: FeedRepository.javaJava 使用MVVM获取空对象引用&;火库,java,android,firebase,mvvm,google-cloud-firestore,Java,Android,Firebase,Mvvm,Google Cloud Firestore,我尝试在我的项目中使用MVVM已经很多天了。但却无法实现 我的代码: FeedRepository.java public class FeedsRepository { private static FeedsRepository instance; private List<Feed> feedsToAppend = new ArrayList<>(); public static FeedsRepository getInstance() { if
public class FeedsRepository {
private static FeedsRepository instance;
private List<Feed> feedsToAppend = new ArrayList<>();
public static FeedsRepository getInstance() {
if (instance == null) {
instance = new FeedsRepository();
}
return instance;
}
public MutableLiveData<List<Feed>> getFeeds() {
MutableLiveData<List<Feed>> mFeeds = new MutableLiveData<>();
Query query;
query = feedsRef.whereEqualTo("userID", currentUID).whereEqualTo("not_interested", false)
.orderBy("created_at", Query.Direction.DESCENDING).limit(10);
query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()) {
QuerySnapshot snapshot = task.getResult();
if (snapshot.isEmpty()) {
} else {
List<DocumentSnapshot> documents = snapshot.getDocuments();
for (DocumentSnapshot doc: documents) {
final Feed feed = doc.toObject(Feed.class);
feed.setFeedID(doc.getId());
feed.setFeedAvailable(true);
feedsToAppend.add(feed);
System.out.println("Feed: " + feed.getFeedID());
}
System.out.println("Total feeds to append: " + feedsToAppend.size());
mFeeds.setValue(feedsToAppend);
}
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
}
});
return mFeeds;
}
public class HomeViewModel extends ViewModel {
private MutableLiveData<List<Feed>> mFeeds;
private FeedsRepository repo;
public void init() {
if (mFeeds != null) {
return;
}
repo = FeedsRepository.getInstance();
mFeeds = repo.getFeeds();
}
public LiveData<List<Feed>> getFeeds() {
return mFeeds;
}
}
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.home_fragment, container, false);
feedsRecycleView = view.findViewById(R.id.feeds_recycler_view);
homeViewModel = ViewModelProviders.of(this).get(HomeViewModel.class);
homeViewModel.init();
homeViewModel.getFeeds().observe(getViewLifecycleOwner(), new Observer<List<Feed>>() {
@Override
public void onChanged(List<Feed> feeds) {
System.out.println("Feed adapter feeds: "+feeds.size());
mFeeds = feeds;
feedAdapter.notifyDataSetChanged();
}
});
List<Feed> testFeeds = homeViewModel.getFeeds().getValue();
System.out.println("Test feeds are : " + testFeeds.size());
feedAdapter = new FeedAdapter(getContext(), homeViewModel.getFeeds().getValue());
feedsRecycleView.setAdapter(feedAdapter);
feedsRecycleView.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
linearLayoutManager.setStackFromEnd(true); //newer posts will be shown on top
linearLayoutManager.setReverseLayout(true);
feedsRecycleView.setLayoutManager(linearLayoutManager);
return view;
}
请任何人告诉我哪里出错。您得到以下错误: “尝试对空对象引用调用接口方法'int java.util.List.size()'” 最有可能出现在这一特定代码行:
System.out.println("Test feeds are : " + testFeeds.size());
private MutableLiveData<List<Feed>> mFeeds
这是因为您的testFeeds
对象是null
,这意味着:
homeViewModel.getFeeds().getValue();
返回null
。这也意味着getValue()
方法返回一个null
的LiveData对象,这与HomeViewModel
类中的情况一样,getFeeds()
方法返回一个从未初始化过的LiveData对象。要解决此问题,请更改以下代码行:
System.out.println("Test feeds are : " + testFeeds.size());
private MutableLiveData<List<Feed>> mFeeds
private MutableLiveData mFeeds
到
private MutableLiveData mFeeds=new MutableLiveData();
我尝试了这个,但仍然是相同的错误
public MutableLiveData<List<Feed>> getFeeds() {
MutableLiveData<List<Feed>> mFeeds = new MutableLiveData<>();
return mFeeds;
}
public MutableLiveData getFeeds(){
MutableLiveData mFeeds=新的MutableLiveData();
返回mFeeds;
}
好的。我已经弄明白了
我必须在FeedsRepository中添加这一行
public MutableLiveData<ArrayList<Feed>> getFeeds() {
MutableLiveData<ArrayList<Feed>> mFeeds = new MutableLiveData<>();
mFeeds.setValue(feedsToAppend);
......
因为后端进程是异步的,并且需要时间来获取
它将返回空对象。顺便说一句,我已经在存储库中初始化了MutableLiveData,因此通过调用repo.getFeeds()应该返回空源。
testFeeds
仍然为空吗?是的,它仍然为空。您是否尝试在FeedRepository
类中移动这一行返回mFeeds代码>就在if-else语句结束之后?是onComplete
方法中的最后一行。是这样吗?
public MutableLiveData<ArrayList<Feed>> getFeeds() {
MutableLiveData<ArrayList<Feed>> mFeeds = new MutableLiveData<>();
mFeeds.setValue(feedsToAppend);
......
mFeeds.setValue(feedsToAppend);