Java 为什么我的应用程序会因NullPointerException崩溃,为什么我的适配器(可能)为null?
当我的应用程序尝试获取适配器上的位置时,它会崩溃,因为显然我的适配器没有正确填充 数据通过改造得到了很好的恢复,我的自定义适配器(Java 为什么我的应用程序会因NullPointerException崩溃,为什么我的适配器(可能)为null?,java,android,nullpointerexception,android-arrayadapter,retrofit2,Java,Android,Nullpointerexception,Android Arrayadapter,Retrofit2,当我的应用程序尝试获取适配器上的位置时,它会崩溃,因为显然我的适配器没有正确填充 数据通过改造得到了很好的恢复,我的自定义适配器(NewsAdapter,基于ArrayAdapter)以前也被填充过,但我不得不对它进行一些调整,现在根本无法让它工作 我们的想法是让每个新闻源都可用,获得每个特定新闻源的文章,然后用这些文章填充我的GridView。这是我的密码: 新闻适配器: public class NewsAdapter extends ArrayAdapter<Articles_Map
NewsAdapter
,基于ArrayAdapter)以前也被填充过,但我不得不对它进行一些调整,现在根本无法让它工作
我们的想法是让每个新闻源都可用,获得每个特定新闻源的文章,然后用这些文章填充我的GridView。这是我的密码:
新闻适配器
:
public class NewsAdapter extends ArrayAdapter<Articles_Map> {
Context mContext;
ArrayList<Articles_Map> articles;
public NewsAdapter(Context c, int resource) {
super(c, resource);
this.mContext = c;
}
public NewsAdapter(Context c, int resource, ArrayList<Articles_Map> articles) {
super(c, resource, articles);
this.mContext = c;
this.articles = articles;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// get the property we are displaying
Articles_Map article = articles.get(position);
// get the inflater and inflate the XML layout for each item
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.article_layout, null);
ImageView thumbnail = (ImageView) view.findViewById(R.id.thumbnail);
TextView title = (TextView) view.findViewById(R.id.title);
TextView description = (TextView) view.findViewById(R.id.description);
Picasso.with(mContext).load(article.urlToImage).into(thumbnail);
title.setText(article.title);
description.setText(article.description);
return view;
}
}
文章\u地图
:
public class NewsAPI_Map {
String status;
String source;
ArrayList<Articles_Map> articles;
public NewsAPI_Map(String status, String source, ArrayList<Articles_Map> articles) {
this.status = status;
this.source = source;
this.articles = articles;
}
}
public class Articles_Map {
String title;
String description;
String url;
String urlToImage;
public Articles_Map(String title, String description, String url, String urlToImage) {
this.title = title;
this.description = description;
this.url = url;
this.urlToImage = urlToImage;
}
}
ListNewsActivity.java
:
@Override
protected void onCreate(Bundle savedInstanceState) {
/* ... */
// parameters for Sources endpoint
String category = "sport";
String language = "en";
String country = "us";
// Sources endpoint
Sources_Interface client_sources = NewsAPI_Adapter.createService(Sources_Interface.class);
Call<Sources_Map> call_sources = client_sources.getData(category, language, country);
call_sources.enqueue(new Callback<Sources_Map>() {
@Override
public void onResponse(Call<Sources_Map> call_sources, Response<Sources_Map> response) {
if (response.body() != null) {
final NewsAdapter nAdapter = new NewsAdapter(ListNewsActivity.this,
R.layout.article_layout);
for (Sources_Content source : response.body().sources) {
if (source.sortBysAvailable.contains("latest")) {
// Articles endpoint
NewsAPI_Interface client = NewsAPI_Adapter.createService(NewsAPI_Interface.class);
Call<NewsAPI_Map> call = client.getData(source.id, "apiKeyHere");
call.enqueue(new Callback<NewsAPI_Map>() {
@Override
public void onResponse(Call<NewsAPI_Map> call, Response<NewsAPI_Map> response) {
if (response.body() != null) {
ExpandableHeightGridView gv_content = (ExpandableHeightGridView) findViewById(R.id.gv_content);
nAdapter.addAll(response.body().articles);
System.out.println("Count: " + nAdapter.getCount() + "\n" +
"Body: " + response.body().articles + "\n");
gv_content.setAdapter(nAdapter);
gv_content.setExpanded(true);
}
}
@Override
public void onFailure(Call<NewsAPI_Map> call, Throwable t) {
System.out.println("An error ocurred!\n" +
"URL: " + call.request().url() + "\n" +
"Cause: " + t.getCause().toString());
}
});
}
}
}
}
@Override
public void onFailure(Call<Sources_Map> call_sources, Throwable t) {
System.out.println("An error ocurred!");
}
});
}
您基本上使用的是第一个构造函数,这意味着您需要传递
ArrayList
。使用第一个构造函数会使数组未初始化,这会导致出现Nullpointer异常
final NewsAdapter nAdapter = new NewsAdapter(ListNewsActivity.this,
R.layout.article_layout);
而你的要求是第二个
public NewsAdapter(Context c, int resource, ArrayList<Articles_Map> articles) {
super(c, resource, articles);
this.mContext = c;
this.articles = articles;
}
更新代码
public class NewsAdapter extends ArrayAdapter<Articles_Map> {
Context mContext;
ArrayList<Articles_Map> articles;
public NewsAdapter(Context c, int resource) {
super(c, resource);
this.mContext = c;
this.articles = new ArrayList<>();
}
public void add(Articles_Map m){
articles.add(m);
notifyDataSetChanged();
}
... more code ...
}
公共类新闻适配器扩展了ArrayAdapter{
语境;
ArrayList文章;
公共新闻适配器(上下文c,int资源){
超级(c,资源);
this.mContext=c;
this.articles=new ArrayList();
}
公共作废添加(文章地图m){
增加(m)条;
notifyDataSetChanged();
}
…更多代码。。。
}
我添加了代码add(Articles\u Map m)
,以便您可以在回调中使用它,在这种情况下,您不需要重新初始化适配器。问题是,您正在使用第一个构造函数创建适配器,它不会初始化数据集(数组列表),因此它为空。然后,您尝试将数据添加到数据集“articles”,因为数据集未初始化,所以无法工作
要解决这个问题,请进入您在此处定义的addAll()方法
nAdapter.addAll(response.body().articles)
并在方法的开头添加
if(articles==null){
articles=新ArrayList articles();}
在这之后,不要忘记调用.notifyDataHasChanged()
这是你应该知道的一条基本编程规则,因为集合不是安卓的东西
Articles_Map article = articles.get(position);
请记住在使用前检查空值:
if (articles != null)
{
Articles_Map article = articles.get(position);
//other code
}
在NewsAPI_映射类中,您忘记为对象放置getter和setter
NewsAPI_Map:
public class NewsAPI_Map {
String status;
String source;
ArrayList<Articles_Map> articles;
public NewsAPI_Map(String status, String source, ArrayList<Articles_Map> articles) {
this.status = status;
this.source = source;
this.articles = articles;
}
public void setStatus(String status){
this.status = status;
}
public String getStatus(){
return this.status;
}
....
}
NewsAPI\u地图:
公共类新闻API_图{
字符串状态;
字符串源;
ArrayList文章;
公共新闻API_映射(字符串状态、字符串源、ArrayList文章){
这个状态=状态;
this.source=源;
这个.文章=文章;
}
公共无效设置状态(字符串状态){
这个状态=状态;
}
公共字符串getStatus(){
返回此状态;
}
....
}
可能您的rest API由于您的getter&setter类而无法将数据转换为对象。请发布日志。@Enzokie我是Android开发新手,但我假设您所说的日志是指控制台输出,因此我已将其添加到我的原始帖子中。如果你的意思是别的,那就问问吧。是的,我知道,那个构造函数就是我在修改之前使用的那个,而且一切都正常工作。然而,据我所知,我目前不能使用同一个构造函数,因为如果我在onResponse()中声明变量并使用3参数构造函数,它将继续为同一个变量声明,因为它在循环中,这是一个问题。我不知道该如何解决这个问题。实际上,你应该将适配器声明为成员变量,这里需要的是一个方法,它接受Articles\u Map
参数,然后“使”视图无效。很抱歉,我不太明白你的意思。。。你能解释一下“一个接受Articles\u Map参数然后“使”视图无效的方法”是什么意思吗?@KaiZ看到我的更新,它应该会让你知道如何使用它。实际上我以前没有定义addAll()方法,但我刚才还是做了一个。我把它编码为,我不确定这是否正确。应用程序现在没有崩溃,但它似乎没有填满适配器,因为没有显示任何内容,并且我的getCount()始终为0。这应该能回答你的问题
if (articles != null)
{
Articles_Map article = articles.get(position);
//other code
}
NewsAPI_Map:
public class NewsAPI_Map {
String status;
String source;
ArrayList<Articles_Map> articles;
public NewsAPI_Map(String status, String source, ArrayList<Articles_Map> articles) {
this.status = status;
this.source = source;
this.articles = articles;
}
public void setStatus(String status){
this.status = status;
}
public String getStatus(){
return this.status;
}
....
}