What is a NullPointerException, and how do I fix it?
(12个答案)
7天前关闭。
我在YouTube上关注了一些教程,其中显示了Firebase的数据列表,然后我尝试使列表中的每个项目都可单击,但是应用程序崩溃并发出了
NullReferenceException
错误。
每当我尝试点击时,它都会崩溃,并显示以下异常:
java.lang.NullPointerException: Attempt to invoke interface method 'void com.example.logindemo.AdapterClass$OnCardListener.onCardClick(android.view.View, int)' on a null object reference
at com.example.logindemo.AdapterClass$MyViewHolder.onClick(AdapterClass.java:67)
at android.view.View.performClick(View.java:7339)
我不知道从哪里开始对问题进行故障排除,我尝试的每个更改都会给出相同的null错误。
AdapterClass.java:
public class AdapterClass extends RecyclerView.Adapter<AdapterClass.MyViewHolder>{
static ArrayList<LocationDetail> list;
private OnCardListener OnCardListener;
public AdapterClass(ArrayList<LocationDetail> list)
{
this.list = list;
}
public AdapterClass(ArrayList<LocationDetail> list, ValueEventListener valueEventListener, OnCardListener onCardListener) {
this.OnCardListener = onCardListener;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_holder, parent, false);
return new MyViewHolder(view, OnCardListener);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
holder.nameloc.setText(list.get(position).getLocationName());
holder.descriptionloc.setText(list.get(position).getLocationDescription());
holder.categoryloc.setText(list.get(position).getLocationCategory());
}
@Override
public int getItemCount() {
return list.size();
}
class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView nameloc, descriptionloc, categoryloc;
OnCardListener onCardListener;
public MyViewHolder(@NonNull View itemView, OnCardListener OnCardListener) {
super(itemView);
nameloc = itemView.findViewById(R.id.nameloc);
descriptionloc = itemView.findViewById(R.id.descriptionloc);
categoryloc = itemView.findViewById(R.id.categoryloc);
itemView.setOnClickListener(this);
this.onCardListener = OnCardListener;
}
@Override
public void onClick(View v) {
onCardListener.onCardClick(v, getAdapterPosition());
}
}
public interface OnCardListener {
void onCardClick (View view, int position);
}
}
SecondActivityTest.java:
public class SecondActivityTest extends AppCompatActivity implements AdapterClass.OnCardListener {
private static final String TAG = "SecondActivityTest";
private FirebaseAuth firebaseAuth;
DatabaseReference ref;
ArrayList<LocationDetail> list;
RecyclerView recyclerView;
SearchView searchView;
Integer count;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second_test);
firebaseAuth = FirebaseAuth.getInstance();
ref = FirebaseDatabase.getInstance().getReference().child("Location ");
recyclerView = findViewById(R.id.rv);
searchView = findViewById(R.id.searchView);
}
@Override
protected void onStart() {
super.onStart();
if(ref != null){
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.exists())
{
list = new ArrayList<>();
for(DataSnapshot ds : dataSnapshot.getChildren())
{
list.add(ds.getValue(LocationDetail.class));
}
AdapterClass adapterClass = new AdapterClass(list);
recyclerView.setLayoutManager(new LinearLayoutManager(SecondActivityTest.this));
recyclerView.setAdapter(adapterClass);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(SecondActivityTest.this, databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
if(searchView != null)
{
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
search(newText);
return true;
}
});
}
}
private void search(String str) {
ArrayList<LocationDetail> myList = new ArrayList<>();
for(LocationDetail object : list)
{
if (object.getLocationName().toLowerCase().contains(str.toLowerCase()) || object.getLocationCategory().toLowerCase().contains(str.toLowerCase()) )
{
myList.add(object);
}
}
AdapterClass adapterClass = new AdapterClass(myList);
recyclerView.setAdapter(adapterClass);
}
private void Logout(){
firebaseAuth.signOut();
finish();
startActivity(new Intent(SecondActivityTest.this, MainActivity.class));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.logoutMenu: {
Logout();
break;
}
case R.id.profileMenu: {
startActivity(new Intent(SecondActivityTest.this, ProfileActivity.class));
break;
}
case R.id.WeatherMenu: {
startActivity(new Intent(SecondActivityTest.this, WeatherActivity.class));
break;
}
case R.id.CurrentLocationMenu: {
startActivity(new Intent(SecondActivityTest.this, GoogleMapsActivity.class));
break;
}
}
return super.onOptionsItemSelected(item);
}
@Override
public void onCardClick(View view, int position) {
Log.d(TAG, "Clicked");
Toast.makeText(SecondActivityTest.this, "Click", Toast.LENGTH_SHORT).show();
/*
list.get(position);
Intent intent = new Intent(this, LocationActivity.java);
intent.putExtra("Som obj", "Some Ob");
startActivity(intent);*/
}
}
您没有将OnCardListener传递给适配器。它为null,因此引发异常。
修改SecondActivityTest.java中的以下行
AdapterClass adapterClass = new AdapterClass(list);
至
AdapterClass adapterClass = new AdapterClass(list,null,this);
而且您还必须修改AdapterClass构造函数:
public AdapterClass(ArrayList<LocationDetail> list, ValueEventListener valueEventListener, OnCardListener onCardListener) {
this.list = list;
this.OnCardListener = onCardListener;
}
我是一名优秀的程序员,十分优秀!