[英]How to fix null pointer from firebase when getting a key?
I got an error, for not getting data from firebase, I don't know why but I try to set the key to my modeldata.我收到一个错误,因为没有从 firebase 获取数据,我不知道为什么,但我尝试将密钥设置为我的模型数据。 And when I try to put it on Recycleview.当我尝试将它放在 Recycleview 上时。 I got an error when I tried to get the data.当我尝试获取数据时出现错误。 Take a look at my code.看看我的代码。
ListActivity列表活动
public class ListActivity extends BaseActivity {
private DatabaseReference databaseReference;
private RecyclerView labelRecycleView;
private ArrayList<DataFirebase> listLabel;
private listAdapter listAdapter;
private Button bInput;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
findView();
setDataToList();
initListener();
putLayoutManager();
}
private void setDataToList() {
simpleway.progressDialog(ListActivity.this);
databaseReference.child("Username").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
listLabel = new ArrayList<>();
for (DataSnapshot dataSnap : dataSnapshot.getChildren()) {
DataFirebase modelClass = dataSnap.getValue(DataFirebase.class);
modelClass.setLabel(dataSnap.getKey());
listLabel.add(modelClass);
}
listAdapter = new listAdapter(listLabel, ListActivity.this);
labelRecycleView.setAdapter(listAdapter);
simpleway.stopProgressDialog();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
System.out.println(databaseError.getDetails() + " " + databaseError.getMessage());
simpleway.stopProgressDialog();
}
});
}
private void putLayoutManager() {
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
labelRecycleView.setLayoutManager(layoutManager);
labelRecycleView.setItemAnimator(new DefaultItemAnimator());
}
@Override
public void findView() {
databaseReference = FirebaseDatabase.getInstance().getReference();
labelRecycleView = findViewById(R.id.listRecyclerVIew);
bInput = findViewById(R.id.inputData);
}
@Override
public void initListener() {
bInput.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
simpleway.startnextActivity(ListActivity.this, InputActivity.class);
}
});
}
}
and this is my adapter it show that i cant get data from my label.这是我的适配器,它显示我无法从我的标签中获取数据。
ListAdapter列表适配器
public class listAdapter extends RecyclerView.Adapter<listAdapter.ViewHolder> {
private List<DataFirebase> dataList;
private AppCompatActivity someActivity;
public listAdapter(List<DataFirebase> dataFirebases, AppCompatActivity someActivity) {
this.dataList = dataFirebases;
this.someActivity = someActivity;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.item_label, viewGroup, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
final DataFirebase labelList = dataList.get(i);
viewHolder.textLabel.setText(labelList.getLabel());
viewHolder.linearLabel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
someActivity.getSupportFragmentManager()
.beginTransaction()
.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_right, R.anim.enter_from_right, R.anim.exit_to_right)
.replace(R.id.container_layout, new TitleFragment())
.commit();
}
});
}
@Override
public int getItemCount() {
return dataList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public LinearLayout linearLabel;
public TextView textLabel;
public ViewHolder(@NonNull View itemView) {
super(itemView);
textLabel = itemView.findViewById(R.id.text_label);
linearLabel = itemView.findViewById(R.id.linearLabel);
}
}
}
its perfectly fine before I try to install for the second time.在我尝试第二次安装之前它完全没问题。 the data are showed up just fine but the second time I try to install I got this error.数据显示得很好,但我第二次尝试安装时出现此错误。
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.example.parzival.flashcard.adapter.listAdapter.onBindViewHolder(listAdapter.java:45)
at com.example.parzival.flashcard.adapter.listAdapter.onBindViewHolder(listAdapter.java:23)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6781)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6823)
How can I fix this?我怎样才能解决这个问题?
Going by the stack trace, your textview is null.通过堆栈跟踪,您的 textview 为空。
textLabel = itemView.findViewById(R.id.text_label); linearLabel = itemView.findViewById(R.id.linearLabel);
Make sure that your text_label / linearLabel id belongs to R.layout.item_label.确保您的 text_label / linearLabel id 属于 R.layout.item_label。
Please show your data structure请显示您的数据结构
add this in your code:在你的代码中添加这个:
if (modelClass != null){
modelClass.setLabel(dataSnap.getKey());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.