我在从alias()
行开始的DessertAdapter类中不断收到空指针异常错误
我已经尝试了所有我知道的方法,但我仍然遇到同样的错误。
这是holder.mName.setText(dessert.getName());
类
Adapter
这是将数据保存到Dessert对象的类
public class DessertAdapter extends RecyclerView.Adapter<DessertAdapter.DessertVh> {
private List<Dessert> desserts = new ArrayList<>();
private static final int VIEW_TYPE_EMPTY_LIST_PLACEHOLDER = 0;
private static final int VIEW_TYPE_OBJECT_VIEW = 1;
private Context context;
@Override
public int getItemViewType(int position) {
if (desserts.isEmpty()) {
return VIEW_TYPE_EMPTY_LIST_PLACEHOLDER;
} else {
return VIEW_TYPE_OBJECT_VIEW;
}
}
public DessertAdapter(Context context,List<Dessert> desserts) {
this.context = context;
this.desserts = desserts;
}
// TODO: another placeholder stuff here
@Override
public DessertVh onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.item_dessert, parent, false);
return new DessertAdapter.DessertVh(view);
}
@Override
public void onBindViewHolder(DessertVh holder, int position) {
Dessert dessert = desserts.get(position);
System.out.println(position);
holder.mName.setText(dessert.getName());
holder.mDescription.setText(dessert.getDescription());
holder.mFirstLetter.setText(String.valueOf(dessert.getFirstLetter()));
holder.mPrice.setText(String.valueOf(dessert.getAmount()));
}
@Override
public int getItemCount() {
return desserts == null ? 0 : desserts.size();
}
public static class DessertVh extends RecyclerView.ViewHolder {
private TextView mName;
private TextView mPrice;
private TextView mDescription;
private TextView mFirstLetter;
public DessertVh(View itemView) {
super(itemView);
mName = (TextView) itemView.findViewById(R.id.txt_name);
mPrice = (TextView) itemView.findViewById(R.id.txt_price);
mDescription = (TextView) itemView.findViewById(R.id.txt_desc);
mFirstLetter = (TextView) itemView.findViewById(R.id.txt_firstletter);
}
}
这是主要的活动代码片段,它在回收站视图中显示来自firebase的数据:
public class AddGigActivity extends AppCompatActivity {
private static String TAG = "AddGigActivity";
private ImageButton saveBtn;
private EditText gigName, gigDescrip, gigAmount;
private String userID;
private FirebaseDatabase mFirebaseDatabase;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthStateListener;
private DatabaseReference myRef;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_gig);
gigName = (EditText)findViewById(R.id.gig_name);
gigDescrip = (EditText)findViewById(R.id.gig_description);
gigAmount = (EditText) findViewById(R.id.gig_amnt);
saveBtn = (ImageButton) findViewById(R.id.mybtn_add);
mAuth = FirebaseAuth.getInstance();
mFirebaseDatabase = FirebaseDatabase.getInstance();
myRef = FirebaseDatabase.getInstance().getReference();
FirebaseUser user = mAuth.getCurrentUser();
userID = user.getUid();
saveBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AddGig();
}
});
}
// real-time adding to the firebase database
private void AddGig(){
String name = gigName.getText().toString();
String descrip = gigDescrip.getText().toString();
String amount = gigAmount.getText().toString();
if((!TextUtils.isEmpty(name))&&(!TextUtils.isEmpty(descrip) && (!TextUtils.isEmpty(amount))) ){
FirebaseUser user = mAuth.getCurrentUser();
userID = user.getUid();
String id = myRef.push().getKey();
Dessert dessert = new Dessert( name, descrip, amount);
// myRef.child(id).setValue(dessert);
myRef.child("users").child(userID).child("Gig posts").child(id).setValue(dessert);
Toast.makeText(this, "Posted! ",Toast.LENGTH_LONG).show();
finish();
// you can still sprlit these to check for each text field
}else{
Toast.makeText(this, "One or more field(s) missing!",Toast.LENGTH_LONG).show();
}
}
请您介意看看代码并帮助我理顺它吗?谢谢。
答案 0 :(得分:0)
尝试以下适配器类
public class DessertAdapter extends RecyclerView.Adapter<DessertAdapter.DessertVh> {
private List<Dessert> desserts;
private static final int VIEW_TYPE_EMPTY_LIST_PLACEHOLDER = 0;
private static final int VIEW_TYPE_OBJECT_VIEW = 1;
private Context context;
// TODO: placeholder stuff here
@Override
public int getItemViewType(int position) {
if (desserts.isEmpty()) {
return VIEW_TYPE_EMPTY_LIST_PLACEHOLDER;
} else {
return VIEW_TYPE_OBJECT_VIEW;
}
}
public DessertAdapter(Context context,List<Dessert> desserts) {
this.context = context;
this.desserts = desserts;
// desserts = Dessert.prepareDesserts(
// context.getResources().getStringArray(R.array.dessert_names),
// context.getResources().getStringArray(R.array.dessert_descriptions),
// context.getResources().getStringArray(R.array.dessert_amounts));
}
// TODO: another placeholder stuff here
@Override
public DessertVh onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
View view = inflater.inflate(R.layout.item_dessert, parent, false);
return new DessertAdapter.DessertVh(view);
}
@Override
public void onBindViewHolder(DessertVh holder, int position) {
holder.mName.setText(desserts.get(position).getName());
holder.mDescription.setText(desserts.get(position).getDescription());
holder.mFirstLetter.setText(String.valueOf(desserts.get(position).getFirstLetter()));
holder.mPrice.setText(String.valueOf(desserts.get(position).getAmount()));
}
@Override
public int getItemCount() {
// if nothing, return null,
// else return the number of items in the list
return desserts == null ? 0 : desserts.size();
}
public static class DessertVh extends RecyclerView.ViewHolder {
private TextView mName;
private TextView mPrice;
private TextView mDescription;
private TextView mFirstLetter;
public DessertVh(View itemView) {
super(itemView);
mName = (TextView) itemView.findViewById(R.id.txt_name);
mPrice = (TextView) itemView.findViewById(R.id.txt_price);
mDescription = (TextView) itemView.findViewById(R.id.txt_desc);
mFirstLetter = (TextView) itemView.findViewById(R.id.txt_firstletter);
}
}
答案 1 :(得分:0)
好的,我修好了! 这是代码
public static class UserFeedFragment extends Fragment {
int color;
public UserFeedFragment() {
}
@SuppressLint("ValidFragment")
public UserFeedFragment(int color) {
this.color = color;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dummy_fragment, container, false);
final RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.dummyfrag_scrollableview);
final FrameLayout frameLayout = (FrameLayout) view.findViewById(R.id.dummyfrag_bg);
frameLayout.setBackgroundColor(color);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity().getBaseContext());
recyclerView.setLayoutManager(linearLayoutManager);
recyclerView.setHasFixedSize(true);
String curtUserId = FirebaseAuth.getInstance().getCurrentUser().getUid();
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference()
.child("users")
.child(curtUserId)
.child("Gig posts");
final List<Dessert> dessertList = new ArrayList<Dessert>();
final DessertAdapter adapter = new DessertAdapter(getContext(), dessertList);
recyclerView.setAdapter(adapter);
rootRef.addValueEventListener(new com.google.firebase.database.ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
dessertList.clear();
for(DataSnapshot gigSnapshot: dataSnapshot.getChildren()){
Dessert dessert = gigSnapshot
.getValue(Dessert.class);
dessertList.add(dessert);
adapter.notifyDataSetChanged();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
// possible to put progress dialogue
return view;
}
}
所有我不得不改变的是
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference()
.child("users")
.child(curtUserId)
.child("Gig posts");
然后在这里添加一个事件监听器
rootRef.addValueEventListener(new com.google.firebase.database.ValueEventListener() {
它有效! 我确实有一个小问题。我有两个feed片段:UserFeed和FeedsFragments。 Userfeed获取当前用户发布的供稿,而FeedsFragment旨在获取所有用户发布的供稿。我试过这个
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference()
.child("users")
// .child(curtUserId) I commented out the id of the user but nothing happened
.child("Gig posts");
我的问题是:有没有办法可以“引导”所有用户检索帖子的引用? 再一次,非常感谢您的帮助!