我正在使用某个应用程序,但遇到了问题。当我按下后退按钮时,我的应用程序崩溃了,并给了我Null Pointer Exception错误。 我有一个列表视图,其中包含一些来自api的数据,我在其中使用了retrofit2来调用它们。除了“后退”按钮,其他所有功能均正常运行。 这是我所做的: 我的适配器
private Context context;
private Industry industries;
public IndustryAdapter(Context context, Industry industries) {
this.industries = industries;
this.context = context;
}
@Override
public int getCount() {
return 1;
}
@Override
public Object getItem(int position) {
return 1;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
convertView = inflater.inflate(R.layout.row_item, parent, false);
}
TextView tvName = (TextView)convertView.findViewById(R.id.tvName);
TextView tvDescription = (TextView)convertView.findViewById(R.id.tvDescription);
String name = industries.getName();
//industries.setName(name);
String descr = industries.getDescription();
//industries.setDescription(descr);
tvName.setText(name);
tvDescription.setText(descr);
return convertView;
}
在我的活动中,我有这个
CompaniesRetriver companiesRetriver = new CompaniesRetriver();
this.recyclerView = (RecyclerView) findViewById(R.id.companies_list);
SharedPreferences editor = getSharedPreferences(MY_PREFERENCE, MODE_PRIVATE);
String token = editor.getString("token", "");
final Context sfdsf = this;
Callback<Companies> callback = new Callback<Companies>() {
@Override
public void onResponse(Call<Companies> call, Response<Companies> response) {
progressBar.dismiss();
if(response.isSuccessful()) {
progressBar.dismiss();
Companies companies = response.body();
CompanyAdapter adapter = new CompanyAdapter(CompaniesListActivity.this, companies);
recyclerView.setLayoutManager(new LinearLayoutManager(CompaniesListActivity.this));
recyclerView.setAdapter(adapter);
System.out.println(companies);
} else {
System.out.println(response.body());
}
}
@Override
public void onFailure(Call<Companies> call, Throwable t) {
progressBar.dismiss();
System.out.println(t.getLocalizedMessage());
}
};
companiesRetriver.getCompanies(callback, token);
我的日志:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String al.sqmo.model.Industry.getName()' on a null object reference
我的POJO班
@SerializedName("name")
private String name;
@SerializedName("id")
private int id;
@SerializedName("description")
private String description;
public Industry(String description, String name, int id) {
this.description = description;
this.name = name;
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
感谢您的帮助!
答案 0 :(得分:0)
尝试检查以下行
String name = industries.getName();
此处行业对象可能为null,这就是您收到此错误的原因。 如果只显示一项,则不需要使用adapter,但是如果有更多项,则可以如下所示使用它:-
private Context context;
private ArrayList<Industry> industryList;
public IndustryAdapter(Context context, Industry industry) {
industryList = new ArrayList();
industryList.add(industry);
this.context = context;
}
public IndustryAdapter(Context context, ArrayList<Industry> industryList) {
this.industryList=this.industryList;
this.context = context;
}
@Override
public int getCount() {
return industryList.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
convertView = inflater.inflate(R.layout.row_item, parent, false);
}
TextView tvName = (TextView)convertView.findViewById(R.id.tvName);
TextView tvDescription = (TextView)convertView.findViewById(R.id.tvDescription);
String name = industryList.get(position).getName();
String descr = industryList.get(position).getDescription();
tvName.setText(name);
tvDescription.setText(descr);
return convertView;
}