(Android) 按下后退时,尝试在空对象引用上调用虚拟方法“int java.lang.String.hashCode()”
Posted
技术标签:
【中文标题】(Android) 按下后退时,尝试在空对象引用上调用虚拟方法“int java.lang.String.hashCode()”【英文标题】:(Android) When Back pressed, Attempt to invoke virtual method 'int java.lang.String.hashCode()' on a null object reference 【发布时间】:2022-01-04 16:48:48 【问题描述】:我一直在做一个 android 项目(一个基本的大学信息应用程序),我有三个主要活动,用户进入就像,Goal/Course Select activity
-> CollegeList activity
-> College Details Activity
。现在,当我按回 College Details Activity
时,它会因以下错误而崩溃:
尝试在空对象引用上调用虚拟方法“int java.lang.String.hashCode()”
以下是我认为可能导致此问题的文件/代码.....
CollegeListActivity.java 文件
package com.anurag.college_information.activities;
import static com.anurag.college_information.activities.CareerGoalActivity.GOAL;
import static com.anurag.college_information.activities.CareerGoalActivity.SHARED_PREFS;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.anurag.college_information.R;
import com.anurag.college_information.adapters.RecyclerAdapter;
import com.anurag.college_information.models.ModelClass;
import java.util.ArrayList;
import java.util.List;
public class CollegeListActivity extends AppCompatActivity
private RecyclerAdapter.RecyclerViewClickListener listener;
//ListView collegeList;
TextView collegeListTitle;
Button courseChange;
RecyclerView recyclerView;
LinearLayoutManager LayoutManager;
RecyclerAdapter recyclerAdapter;
List<ModelClass> cList;
String courseName;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_college_list);
collegeListTitle = findViewById(R.id.college_list_title);
courseChange = findViewById(R.id.btn_change_course);
//collegeListTitle.setText(goal + "Colleges");
collegeListTitle.setText(getIntent().getStringExtra("Title") + " Colleges");
initData();
initRecyclerView();
//collegeList = findViewById(R.id.lv_college_list);
courseChange.setTransformationMethod(null);
courseChange.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.clear();
editor.apply();
Intent i = new Intent(CollegeListActivity.this, CareerGoalActivity.class);
startActivity(i);
//finish();
);
private void initData()
cList = new ArrayList<>();
courseName = getIntent().getStringExtra("Title");
switch (courseName)
case "BE/BTech":
cList.add(new ModelClass("https://images.static-collegedunia.com/public/college_data/images/campusimage/1479294300b-5.jpg", "A.P. Shah College of Engineering", "Thane", "8.0"));
break;
case "Pharmacy":
cList.add(new ModelClass("https://images.static-collegedunia.com/public/college_data/images/campusimage/14382400753.jpg", "Bombay College Of Pharmacy", "Mumbai", "9.0"));
break;
private void initRecyclerView()
setOnClickListener();
recyclerView = findViewById(R.id.recycler_view);
LayoutManager = new LinearLayoutManager(this);
LayoutManager.setOrientation(RecyclerView.VERTICAL);
recyclerView.setLayoutManager(LayoutManager);
recyclerAdapter = new RecyclerAdapter(cList, listener);
recyclerView.setAdapter(recyclerAdapter);
private void setOnClickListener()
listener = new RecyclerAdapter.RecyclerViewClickListener()
@Override
public void onClick(View v, int position)
Intent i = new Intent(CollegeListActivity.this, CollegeDetailsActivity.class);
startActivity(i);
;
@Override
public void onBackPressed()
new AlertDialog.Builder(this)
.setMessage("Are you sure you want to exit?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener()
public void onClick(DialogInterface dialog, int id)
finish();
)
.setNegativeButton("No", null)
.show();
RecyclerAdapter.java
package com.anurag.college_information.adapters;
import android.content.Context;
import android.content.Intent;
import android.telecom.Call;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.anurag.college_information.activities.CollegeDetailsActivity;
import com.anurag.college_information.models.ModelClass;
import com.anurag.college_information.R;
import com.squareup.picasso.Picasso;
import java.util.List;
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder>
private List<ModelClass> collegeList ;
private RecyclerViewClickListener listener;
List<String> imageUrl, collegeName, collegeLocation, collegeRating;
public RecyclerAdapter(List<ModelClass> collegeList, RecyclerViewClickListener listener)
this.collegeList=collegeList;
this.listener = listener;
@NonNull
@Override
public RecyclerAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.college_list_single_row, parent, false);
return new ViewHolder(view);
@Override
public void onBindViewHolder(@NonNull RecyclerAdapter.ViewHolder holder, int position)
String imageLink = collegeList.get(position).getImageLink();
//int img = collegeList.get(position).getCollegeImage();
String cName = collegeList.get(position).getCollegeName();
String cRating = collegeList.get(position).getCollegeRating();
String cLocation = collegeList.get(position).getLocation();
Picasso.get().load(imageLink).into(holder.imageView);
//holder.setData(img, cName, cRating);
holder.setData(imageLink, cName, cLocation ,cRating);
@Override
public int getItemCount()
return collegeList.size();
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
private ImageView imageView;
private TextView collegeName, collegeRating, collegeLocation;
public ViewHolder(@NonNull View itemView)
super(itemView);
imageView = itemView.findViewById(R.id.college_image);
collegeName = itemView.findViewById(R.id.college_name);
collegeRating = itemView.findViewById(R.id.college_rating);
collegeLocation = itemView.findViewById(R.id.college_location);
itemView.setOnClickListener(this);
public void setData(String imageLink, String cName, String cLocation, String cRating)
//imageView.setImageResource(img);
Picasso.get().load(imageLink).error(R.drawable.error).into(imageView);
collegeName.setText(cName);
collegeRating.setText(cRating);
collegeLocation.setText(cLocation);
@Override
public void onClick(View v)
listener.onClick(v, getAdapterPosition());
Intent i = new Intent(v.getContext(), CollegeDetailsActivity.class);
i.putExtra("collegeImage", collegeList.get(getAdapterPosition()).getImageLink());
i.putExtra("collegeName", collegeList.get(getAdapterPosition()).getCollegeName());
i.putExtra("collegeRating", collegeList.get(getAdapterPosition()).getCollegeRating());
i.putExtra("collegeLocation", collegeList.get(getAdapterPosition()).getLocation());
v.getContext().startActivity(i);
public interface RecyclerViewClickListener
void onClick(View v, int position);
CollegeDetailsActivity.java
package com.anurag.college_information.activities;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.anurag.college_information.R;
import com.squareup.picasso.Picasso;
public class CollegeDetailsActivity extends AppCompatActivity
Button btnApply;
ImageView dCollegeImage;
TextView dCollegeName, dCollegeRating, dCollegeLocation;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_college_details);
dCollegeImage = findViewById(R.id.details_college_image);
dCollegeName = findViewById(R.id.details_college_name);
dCollegeRating = findViewById(R.id.details_college_rating);
dCollegeLocation = findViewById(R.id.details_college_location);
btnApply = findViewById(R.id.btn_apply);
Intent i = getIntent();
String cn = i.getStringExtra("collegeName");
String cr = i.getStringExtra("collegeRating");
String ci = i.getStringExtra("collegeImage");
String cl = i.getStringExtra("collegeLocation");
Picasso.get().load(ci).error(R.drawable.error).into(dCollegeImage);
dCollegeName.setText(cn);
dCollegeRating.setText(cr);
dCollegeLocation.setText(cl);
btnApply.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
Toast.makeText(getApplicationContext(), "The institute will be notified, of your application", Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), "The college will contact you, Thank you", Toast.LENGTH_LONG).show();
);
@Override
public void onBackPressed()
Intent i = new Intent(CollegeDetailsActivity.this, CollegeListActivity.class);
startActivity(i);
这是错误截图:
我对 android 还很陌生,我只参与了 4 到 5 个项目, 任何帮助将不胜感激,谢谢
注释掉的代码,只是我实现的一个普通列表视图,以防万一,我必须删除回收器视图。
【问题讨论】:
【参考方案1】:如果您不在CollegeDetailsActivity
中覆盖onBackPressed
,这可能会消失。您发布的代码不会返回具有有效“标题”字符串的活动,而是转到未定义“标题”的新活动,然后得到 NullPointerException,因为courseName
在@987654325 中将为空@(错误消息告诉您在该方法的第 81 行导致错误)。在开关中使用空字符串会导致that type of error
只需在CollegeDetailsActivity
中完全删除您的onBackPressed
。
【讨论】:
所以,现在应用程序没有崩溃,但是,按 back 一次显示相同的CollegeDetailsActivty
,没有任何数据,只是一个空白布局,有所有视图,但是没有数据。有解决办法吗?编辑:当按两次返回时,它会进入上一个屏幕
您有两个地方可以启动详细信息活动 - 一个在 RecyclerAdapter 中,另一个在点击侦听器中。您将启动详细信息活动两次,因此按“返回”将返回其中的第一个。你会想要解决这个问题,所以它只启动一次。删除 CollegeListActivity 中的一组 - 它不会将任何参数传递给新活动,或者修复那个并且不在 RecyclerAdapter 中启动意图(无论哪种方式 - 只是不要做两次)以上是关于(Android) 按下后退时,尝试在空对象引用上调用虚拟方法“int java.lang.String.hashCode()”的主要内容,如果未能解决你的问题,请参考以下文章
尝试在空对象引用上调用虚拟方法 'android.view.SurfaceHolder android.view.SurfaceView.getHolder()'
尝试在空对象引用上调用虚拟方法 'android.graphics.Rect android.graphics.drawable.Drawable.getBounds()'
尝试在空对象引用上调用虚拟方法 'android.view.Window$Callback android.view.Window.getCallback()'
尝试在空对象引用上调用虚拟方法 'android.os.Handler android.support.v4.app.FragmentHostCallback.getHandler()'