我找不到任何可能的原因导致我面临空指针异常。当我尝试在中调用shimmerLayout.startShimmer();
方法时,我正面临着它OnCreateView
。我已经检查了几次 shimmerLayout 连接是否正常。但似乎我已经完美地将微光容器与 java 代码连接起来。我仍然面临空指针异常。我认为关于微光生命周期或其他任何事情,我不知道一些事情。我也调用了shimmerLayout.stopShimmer();
ingetDataToArray()
方法。
片段博客.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".BlogFragment">
<com.facebook.shimmer.ShimmerFrameLayout
android:id="@+id/shimmerFrameLayout_blog"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="@layout/facebook_shimmer_item" />
<include layout="@layout/facebook_shimmer_item" />
<include layout="@layout/facebook_shimmer_item" />
<include layout="@layout/facebook_shimmer_item" />
</LinearLayout>
</com.facebook.shimmer.ShimmerFrameLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView_blog"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/floatingActionButton_blog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="38dp"
android:layout_marginBottom="68dp"
android:clickable="true"
android:src="@drawable/ic_add_black_24dp"
app:backgroundTint="#0979D3"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.954"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.931" />
</RelativeLayout>
facebook_shimmer_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView12"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_marginTop="5dp"
android:background="@color/content_text_color" />
<TextView
android:id="@+id/textView14"
android:layout_width="94dp"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_marginTop="40dp"
android:background="@color/content_text_color" />
<TextView
android:id="@+id/textView13"
android:layout_width="80dp"
android:layout_height="77dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:background="@color/content_text_color" />
</androidx.cardview.widget.CardView>
<TextView
android:id="@+id/textView15"
android:layout_width="380dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:background="@color/content_text_color" />
<TextView
android:id="@+id/textView16"
android:layout_width="308dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:background="@color/content_text_color" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="220dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/imageView3"
app:srcCompat="@drawable/ic_watchers"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp" />
<TextView
android:id="@+id/textView_views"
android:layout_width="20dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="0" />
<ImageView
android:id="@+id/imageView4"
app:srcCompat="@drawable/ic_like"
android:layout_width="18dp"
android:layout_height="12dp"
android:layout_marginTop="5dp" />
<TextView
android:id="@+id/textView_likes"
android:layout_width="20dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="0" />
<ImageView
android:id="@+id/imageView2"
app:srcCompat="@drawable/ic_comments"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp" />
<TextView
android:id="@+id/textView_comments"
android:layout_width="20dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="0" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="34dp"
android:orientation="horizontal">
<TextView
android:id="@+id/textView18"
android:layout_width="123dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/content_text_color" />
<TextView
android:id="@+id/textView19"
android:layout_width="128dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/content_text_color" />
</LinearLayout>
</LinearLayout>
博客片段.java
package com.example.farmersapp;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.farmersapp.adapter.ListBlogItem_Adapter;
import com.example.farmersapp.model.BlogItem;
import com.facebook.shimmer.ShimmerFrameLayout;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.QueryDocumentSnapshot;
import com.google.firebase.firestore.QuerySnapshot;
import com.google.firebase.storage.StorageReference;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
public class BlogFragment extends Fragment {
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;
public static final String TAG = "checked";
//widgets
private RecyclerView recyclerViewBlogItem;
private FloatingActionButton floatingActionButton;
private ShimmerFrameLayout shimmerFrameLayout;
List<BlogItem> mBlogData;
//Firebase
private FirebaseAuth mAuth;
private StorageReference storageReference;
private CollectionReference collectionReferenceBlog,collectionReferenceBlogId,collectionReferenceusers;
private FirebaseFirestore firebaseFirestore;
private DocumentReference documentReferenceBlogId,documentReferenceBlog;
//adapter
ListBlogItem_Adapter adapter;
public BlogFragment() {
// Required empty public constructor
}
public static BlogFragment newInstance(String param1, String param2) {
BlogFragment fragment = new BlogFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View contentView = inflater.inflate(R.layout.fragment_blog, container, false);
floatingActionButton = contentView.findViewById(R.id.floatingActionButton_blog);
recyclerViewBlogItem = contentView.findViewById(R.id.recyclerView_blog);
shimmerFrameLayout = container.findViewById(R.id.shimmerFrameLayout_blog);
mBlogData = new ArrayList<>();
shimmerFrameLayout.startShimmer();
mAuth = FirebaseAuth.getInstance();
collectionReferenceBlog = FirebaseFirestore.getInstance().collection("Blog");
floatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Fragment fragment = new AddNewBlogFragment();
FragmentManager fragmentManager = requireActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_right, R.anim.enter_from_right, R.anim.exit_to_right);
fragmentTransaction.replace(R.id.container, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
setUpRecyclerViewManual();
getDataToArray();
return contentView;
}
private void getDataToArray() {
collectionReferenceBlog.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
if(queryDocumentSnapshots.isEmpty())
{
Log.d(TAG,"there is nothing in blog database");
}
else
{
List<BlogItem> data = queryDocumentSnapshots.toObjects(BlogItem.class);
adapter = new ListBlogItem_Adapter(mBlogData, getContext());
recyclerViewBlogItem.setAdapter(adapter);
mBlogData.addAll(data);
Log.d("checked success:", "ok " + mBlogData);
shimmerFrameLayout.stopShimmer();
// shimmerFrameLayout.setVisibility(View.GONE);
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d(TAG, "blog data load failed");
}
});
}
private void setUpRecyclerViewManual() {
recyclerViewBlogItem.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this.getContext());
// adapter = new AlternateListAdapter_Market(mData,this.getContext());
// marketRecyleView.setAdapter(adapter);
Log.d("checked", "adapter called");
recyclerViewBlogItem.setLayoutManager(layoutManager);
}
}
例外
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.farmersapp, PID: 27402
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.facebook.shimmer.ShimmerFrameLayout.startShimmer()' on a null object reference
at com.example.farmersapp.BlogFragment.onCreateView(BlogFragment.java:106)
at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2698)
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:320)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1187)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1356)
at androidx.fragment.app.FragmentManager.moveFragmentToExpectedState(FragmentManager.java:1434)
at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1497)
at androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:447)
at androidx.fragment.app.FragmentManager.executeOps(FragmentManager.java:2169)
at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1992)
at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:1947)
at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1849)
at androidx.fragment.app.FragmentManager$4.run(FragmentManager.java:413)
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)