编辑: 我试图从firebase填充firebase recyclerview,看起来一切都很好,但是在填充textview时它会抛出
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)
at com.example.cyrenians.cyreniansprototypenew.DummyFragment$1.onBindViewHolder(DummyFragment.java:64)
at com.example.cyrenians.cyreniansprototypenew.DummyFragment$1.onBindViewHolder(DummyFragment.java:55)
at com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:118)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6482)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6515)
at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5458)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5724)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5563)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5559)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2229)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1556)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1516)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:608)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3693)
at android.support.v7.widget.RecyclerView.onMeasure(RecyclerView.java:3109)
at android.view.View.measure(View.java:22002)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
at android.view.View.measure(View.java:22002)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:22002)
at android.support.constraint.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:934)
at android.support.constraint.ConstraintLayout.onMeasure(ConstraintLayout.java:973)
at android.view.View.measure(View.java:22002)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139)
at android.view.View.measure(View.java:22002)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
at android.view.View.measure(View.java:22002)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:22002)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
at android.view.View.measure(View.java:22002)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:721)
at android.view.View.measure(View.java:22002)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2410)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1498)
E/AndroidRuntime: at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1751)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1386)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6733)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
at android.view.Choreographer.doCallbacks(Choreographer.java:723)
at android.view.Choreographer.doFrame(Choreographer.java:658)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
这是我的片段类
public class DummyFragment extends android.support.v4.app.Fragment {
private FirebaseRecyclerAdapter<Product, ProductViewHolder> mFirebaseAdapter;
private RecyclerView mProduceList = null;
public Context c;
private LinearLayoutManager manager;
public DummyFragment() {
}
public static DummyFragment newInstance() {
DummyFragment fragment = new DummyFragment();
return fragment;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View itemView = inflater.inflate(R.layout.dummy_fragment, container, false);
FirebaseApp.initializeApp(c);
mProduceList = itemView.findViewById(R.id.recyclerView);
mProduceList.setLayoutManager(new LinearLayoutManager(getActivity()));
DatabaseReference productRef = FirebaseDatabase.getInstance().getReference().child("Products");
Query productQuery = productRef.orderByKey();
FirebaseRecyclerOptions productOptions = new FirebaseRecyclerOptions.Builder<Product>().setQuery(productQuery, Product.class).build();
mFirebaseAdapter = new FirebaseRecyclerAdapter<Product, ProductViewHolder>(productOptions) {
@Override
public ProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
return new ProductViewHolder(inflater.inflate(R.layout.dummy_fragment, parent, false));
}
@Override
protected void onBindViewHolder(ProductViewHolder viewHolder, int position, Product product) {
viewHolder.post_name.setText(product.getTitle());
viewHolder.post_type.setText(product.getDesc());
}
};
mProduceList.setAdapter(mFirebaseAdapter);
return itemView;
}
@Override
public void onStart() {
super.onStart();
mFirebaseAdapter.startListening();
}
@Override
public void onStop() {
super.onStop();
mFirebaseAdapter.stopListening();
}
public static class ProductViewHolder extends RecyclerView.ViewHolder {
View mView;
TextView post_name;
TextView post_type;
public ProductViewHolder(View itemView) {
super(itemView);
mView = itemView;
post_name = (TextView) mView.findViewById(R.id.productTitle);
post_type = (TextView) mView.findViewById(R.id.productDesc);
}
public void setTitle(String title) {
post_name.setText(title);
}
public void setDesc(String desc) {
post_type.setText(desc);
}
}
}
这是我的模型类
public class Product {
@Exclude
private String Title;
@Exclude private String Desc;
int image;
public Product()
{
}
@Keep
public String getTitle() {
return Title;
}
@Keep
public void setTitle(String title){
Title = title;
}
public String getDesc(){
return Desc;
}
public void setDesc(String desc){
Desc = desc;
}
public int getImage(){
return image;
}
public void setImage(int image){
this.image=image;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Product model = (Product) o;
return (Title == null ? model.Title == null : Title.equals(model.Title))
&& (Desc == null ? model.Desc == null : Desc.equals(model.Desc));
}
@Override
public int hashCode() {
int result = Title == null ? 0 : Title.hashCode();
result = 31 * result + (Desc == null ? 0 : Desc.hashCode());
return result;
}
@Override
public String toString() {
return "Model{" +
"mTitle='" + Title + '\'' +
", mImage='" + Desc + '\'' +
'}';
}
}
我不确定我的模型类是否设置正确以及我的查看器是否正确。我对Firebase来说比较新,我真的很挣这个回收者的观点,所以如果有人能提供解决方案,那么我将非常感激。 这是布局
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/cardView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/product"
android:paddingLeft="10dp"
android:layout_width="70sp"
android:layout_height="70sp" />
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/productTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Card Title"
android:textColor="#000000"
android:textSize="15sp"
android:paddingTop="13dp"
android:paddingLeft="20dp"
android:textStyle="bold"/>
<TextView
android:id="@+id/productDesc"
android:layout_width="fill_parent"
android:paddingLeft="20dp"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
和
<?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="fill_parent"
android:background="#f7f7f8"
android:orientation="vertical"
android:scrollbars="horizontal">
<ImageView
android:layout_marginTop="-120dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/productsdarken"/>
<!-- Recycler View -->
<android.support.v7.widget.RecyclerView
android:layout_marginTop="-100dp"
android:id="@+id/recyclerView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="none" />
</LinearLayout>
答案 0 :(得分:1)
改变这个:
viewHolder.post_name.setText(product.getTitle());
viewHolder.post_type.setText(product.getDesc());
到此:
viewHolder.setTitle(product.getTitle());
viewHolder.setDesc(product.getDesc());
答案 1 :(得分:0)
在dimens.xml
中使用不含dp
的dimen值时出现此错误。例子
<dimen name="dp_50">50</dimen>
这看起来很奇怪,因为应用程序仍然可以成功构建,并且错误消息看起来不相关。
要解决问题,只需更改为
<dimen name="dp_50">50dp</dimen>
希望它可以帮助任何人