我有一个问题-应用中的答案系统。我可以看到我的问题,如果我愿意,我可以回答我的问题。一切正常,但是当我“将我的问题标记为已回答”时,我得到了空值异常。当我再次运行该应用程序时,firestore和应用程序中的所有内容都可以,就像我没有空点异常一样。我不知道这是否与Firestore或其他有关。
Logcat指向此:
private void getQuestions() {
Objects.requireNonNull( getView() ).findViewById(R.id.default_item).setVisibility(View.GONE);
refreshLayout.setRefreshing(true);
Query firstQuery = mFirestore.collection("Questions")
.orderBy("timestamp", Query.Direction.DESCENDING);
firstQuery.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
if (!documentSnapshots.isEmpty()) {
for (DocumentChange doc : documentSnapshots.getDocumentChanges()) {
if (doc.getType() == DocumentChange.Type.ADDED) {
if (!Objects.equals( doc.getDocument().getString( "id" ), mCurrentUser.getUid() )) {
AllQuestionsModel question = doc.getDocument().toObject(AllQuestionsModel.class).withId(doc.getDocument().getId());
allQuestionsModelList.add(question);
adapter.notifyDataSetChanged();
refreshLayout.setRefreshing(false);
}
}
}
if(allQuestionsModelList.isEmpty()){
Objects.requireNonNull( getView() ).findViewById(R.id.default_item).setVisibility(View.VISIBLE);
refreshLayout.setRefreshing(false);
}
} else {
Objects.requireNonNull( getView() ).findViewById(R.id.default_item).setVisibility(View.VISIBLE);
refreshLayout.setRefreshing(false);
}
}
});
}
错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
at eu.healthydev.quizarena.social.AllQuestionsFragment$2.onEvent(AllQuestionsFragment.java:146)
at eu.healthydev.quizarena.social.AllQuestionsFragment$2.onEvent(AllQuestionsFragment.java:124)
at com.google.firebase.firestore.Query.lambda$addSnapshotListenerInternal$2(com.google.firebase:firebase-firestore@@21.3.0:1025)
at com.google.firebase.firestore.Query$$Lambda$3.onEvent(Unknown Source:6)
at com.google.firebase.firestore.core.AsyncEventListener.lambda$onEvent$0(com.google.firebase:firebase-firestore@@21.3.0:42)
at com.google.firebase.firestore.core.AsyncEventListener$$Lambda$1.run(Unknown Source:6)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
我很困惑,因为再次运行应用程序后,一切正常。有帮助吗?
更新
完整的片段代码,因为问题与视图有关:
public class AllQuestionsFragment extends Fragment {
private RecyclerView recyclerView;
private Context context;
private FirebaseFirestore mFirestore;
private FirebaseUser mCurrentUser;
private QuestionAdapter adapter;
private static String TAG= AllQuestionsFragment.class.getSimpleName();
private List<AllQuestionsModel> allQuestionsModelList =new ArrayList<>();
private TextView et0,et1,et2,et3,et4,et5,et6,et7,et8,et9,et10,et11,et12,et13;
private SwipeRefreshLayout refreshLayout;
public AllQuestionsFragment() {
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if(getActivity().getSharedPreferences("theme",MODE_PRIVATE).getBoolean("dark",false))
return inflater.inflate( R.layout.social_frag_answered_dark, container, false);
else
return inflater.inflate(R.layout.social_frag_answered_dark, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
context = view.getContext();
mCurrentUser = FirebaseAuth.getInstance().getCurrentUser();
if (mCurrentUser != null) {
et0=view.findViewById(R.id.all);
et1=view.findViewById(R.id.accountancy);
et2=view.findViewById(R.id.astronomy);
et3=view.findViewById(R.id.biology);
et4=view.findViewById(R.id.business_maths);
et5=view.findViewById(R.id.computer_science);
et6=view.findViewById(R.id.commerce);
et7=view.findViewById(R.id.chemistry);
et8=view.findViewById(R.id.economics);
et9=view.findViewById(R.id.geography);
et10=view.findViewById(R.id.history);
et11=view.findViewById(R.id.physics);
et12=view.findViewById(R.id.p_science);
et13=view.findViewById(R.id.maths);
mFirestore = FirebaseFirestore.getInstance();
adapter = new QuestionAdapter(allQuestionsModelList);
refreshLayout=view.findViewById(R.id.refreshLayout);
recyclerView = view.findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(context));
recyclerView.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.VERTICAL));
recyclerView.setItemAnimator(new DefaultItemAnimator());
allQuestionsModelList.clear();
recyclerView.setAdapter(adapter);
refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
allQuestionsModelList.clear();
adapter.notifyDataSetChanged();
getQuestions();
}
});
setUpOnClick();
getQuestions();
}
}
private void getQuestions() {
Objects.requireNonNull( getView() ).findViewById(R.id.default_item).setVisibility(View.GONE);
refreshLayout.setRefreshing(true);
Query firstQuery = mFirestore.collection("Questions")
.orderBy("timestamp", Query.Direction.DESCENDING);
firstQuery.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
if (!documentSnapshots.isEmpty()) {
for (DocumentChange doc : documentSnapshots.getDocumentChanges()) {
if (doc.getType() == DocumentChange.Type.ADDED) {
if (!Objects.equals( doc.getDocument().getString( "id" ), mCurrentUser.getUid() )) {
AllQuestionsModel question = doc.getDocument().toObject(AllQuestionsModel.class).withId(doc.getDocument().getId());
allQuestionsModelList.add(question);
adapter.notifyDataSetChanged();
refreshLayout.setRefreshing(false);
}
}
}
if(allQuestionsModelList.isEmpty()){
Objects.requireNonNull( getView() ).findViewById(R.id.default_item).setVisibility(View.VISIBLE);
refreshLayout.setRefreshing(false);
}
} else {
Objects.requireNonNull( getView() ).findViewById(R.id.default_item).setVisibility(View.VISIBLE);
refreshLayout.setRefreshing(false);
}
}
});
}
@Override
public void onResume() {
super.onResume();
adapter.notifyDataSetChanged();
}
public void filterResult(String subject){
if(subject.equals("All")){
getQuestions();
}else{
refreshLayout.setRefreshing(true);
Objects.requireNonNull( getView() ).findViewById(R.id.default_item).setVisibility(View.GONE);
Query firstQuery = mFirestore.collection("Questions")
.whereEqualTo("subject",subject)
.orderBy("timestamp", Query.Direction.DESCENDING);
firstQuery.addSnapshotListener(getActivity(), new EventListener<QuerySnapshot>() {
@Override
public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {
try {
if (!documentSnapshots.isEmpty()) {
for (DocumentChange doc : documentSnapshots.getDocumentChanges()) {
if (doc.getType() == DocumentChange.Type.ADDED) {
if (!doc.getDocument().getString("id").equals(mCurrentUser.getUid())) {
AllQuestionsModel question = doc.getDocument().toObject(AllQuestionsModel.class).withId(doc.getDocument().getId());
allQuestionsModelList.add(question);
adapter.notifyDataSetChanged();
refreshLayout.setRefreshing(false);
}
}
}
if(allQuestionsModelList.isEmpty()){
Objects.requireNonNull( getView() ).findViewById(R.id.default_item).setVisibility(View.VISIBLE);
refreshLayout.setRefreshing(false);
}
} else {
getView().findViewById(R.id.default_item).setVisibility(View.VISIBLE);
refreshLayout.setRefreshing(false);
}
}catch (NullPointerException eee){
Toasty.error(context, "Some technical error occurred", Toasty.LENGTH_SHORT,true).show();
adapter.notifyDataSetChanged();
if(allQuestionsModelList.isEmpty()){
Objects.requireNonNull( getView() ).findViewById(R.id.default_item).setVisibility(View.VISIBLE);
refreshLayout.setRefreshing(false);
}
} catch (Exception ee){
ee.printStackTrace();
Toasty.error(context, "Some technical error occurred", Toasty.LENGTH_SHORT, true).show();
if(allQuestionsModelList.isEmpty()){
refreshLayout.setRefreshing(false);
getView().findViewById(R.id.default_item).setVisibility(View.VISIBLE);
}
}
}
});
}
}
public void setUpOnClick(){
et0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
allQuestionsModelList.clear();
adapter.notifyDataSetChanged();
getQuestions();
refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
allQuestionsModelList.clear();
adapter.notifyDataSetChanged();
getQuestions();
}
});
}
});
et1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
allQuestionsModelList.clear();
adapter.notifyDataSetChanged();
filterResult("Accountancy");
refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
allQuestionsModelList.clear();
adapter.notifyDataSetChanged();
filterResult("Accountancy");
}
});
}
});
et2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
allQuestionsModelList.clear();
adapter.notifyDataSetChanged();
filterResult("Astronomy");
refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
allQuestionsModelList.clear();
adapter.notifyDataSetChanged();
filterResult("Astronomy");
}
});
}
});
et3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
allQuestionsModelList.clear();
adapter.notifyDataSetChanged();
filterResult("Biology");
refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
allQuestionsModelList.clear();
adapter.notifyDataSetChanged();
filterResult("Biology");
}
});
}
});
et4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
allQuestionsModelList.clear();
adapter.notifyDataSetChanged();
filterResult("Business Maths");
refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
allQuestionsModelList.clear();
adapter.notifyDataSetChanged();
filterResult("Business Maths");
}
});
}
});
et5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
allQuestionsModelList.clear();
adapter.notifyDataSetChanged();
filterResult("Computer Science");
refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
allQuestionsModelList.clear();
adapter.notifyDataSetChanged();
filterResult("Computer Science");
}
});
}
});
et6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
allQuestionsModelList.clear();
adapter.notifyDataSetChanged();
filterResult("Commerce");
refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
allQuestionsModelList.clear();
adapter.notifyDataSetChanged();
filterResult("Commerce");
}
});
}
});
et7.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
allQuestionsModelList.clear();
adapter.notifyDataSetChanged();
filterResult("Chemistry");
refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
allQuestionsModelList.clear();
adapter.notifyDataSetChanged();
filterResult("Chemistry");
}
});
}
});
et8.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
allQuestionsModelList.clear();
adapter.notifyDataSetChanged();
filterResult("Economics");
refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
allQuestionsModelList.clear();
adapter.notifyDataSetChanged();
filterResult("Economics");
}
});
}
});
et9.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
allQuestionsModelList.clear();
adapter.notifyDataSetChanged();
filterResult("Geography");
refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
allQuestionsModelList.clear();
adapter.notifyDataSetChanged();
filterResult("Geography");
}
});
}
});
et10.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
allQuestionsModelList.clear();
adapter.notifyDataSetChanged();
filterResult("History");
refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
allQuestionsModelList.clear();
adapter.notifyDataSetChanged();
filterResult("History");
}
});
}
});
et11.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
allQuestionsModelList.clear();
adapter.notifyDataSetChanged();
filterResult("Physics");
refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
allQuestionsModelList.clear();
adapter.notifyDataSetChanged();
filterResult("Physics");
}
});
}
});
et12.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
allQuestionsModelList.clear();
adapter.notifyDataSetChanged();
filterResult("Political Science");
refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
allQuestionsModelList.clear();
adapter.notifyDataSetChanged();
filterResult("Political Science");
}
});
}
});
et13.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
allQuestionsModelList.clear();
adapter.notifyDataSetChanged();
filterResult("Maths");
refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
allQuestionsModelList.clear();
adapter.notifyDataSetChanged();
filterResult("Maths");
}
});
}
});
}
答案 0 :(得分:2)
在onViewCreated
内部初始化视图,如下所示。
public class AllQuestionsFragment extends Fragment {
LinearLayout viewLayout;
....
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
viewLayout = view.findViewById(R.id.default_item);
....
}
}
然后使用viewLayout
代替下面的Objects.requireNonNull( getView() ).findViewById(R.id.default_item).setVisibility(View.GONE);
private void getQuestions() {
viewLayout.setVisibility(View.GONE);
....
}