尝试在空对象引用上调用虚拟方法“java.lang.String”
我正在使用一个应用程序,但我有一个问题。当我按下后退按钮时,我的应用程序崩溃,并给出空指针异常错误。 我有一个列表视图,其中包含来自api的一些数据,我在api中使用Reformation2调用了这些数据。除了“后退”按钮外,一切正常。 以下是我所做的: 我的适配器尝试在空对象引用上调用虚拟方法“java.lang.String”,java,android,Java,Android,我正在使用一个应用程序,但我有一个问题。当我按下后退按钮时,我的应用程序崩溃,并给出空指针异常错误。 我有一个列表视图,其中包含来自api的一些数据,我在api中使用Reformation2调用了这些数据。除了“后退”按钮外,一切正常。 以下是我所做的: 我的适配器 private Context context; private Industry industries; public IndustryAdapter(Context context, Industry industries)
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);
我的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;
}
感谢您的帮助 试着检查下一行
String name = industries.getName();
这里的industries对象可能为null,这就是为什么会出现此错误。
如果您只需要显示一项,则不需要使用适配器,但如果有更多项,则可以按如下方式使用它:-
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;
}
发布日志。在此字符串上,name=industries.getName;您面临的问题请发布您的pojo,以便我们了解您传递数据的问题和活动您提供了两个不同的问题。表示您提供了保存公司和行业适配器数据的活动。在该活动中,您使用的是CompanyAdapter,而不是IndustryAdapter。但是列表可以正常工作。我尝试更改为行业适配器,但不起作用。是的,行业为空。我有两个项目要显示,这很好,但当我按下后退按钮,我的应用程序crashes@EngineerEraldo您是否重写了onBackPressed方法?是的,我这样做了,但没有在onBackPressed方法中使用debug检查错误是否发生在转到onBackPressed方法之前或我们在其中时。
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;
}