在arraylist就绪之前启动自定义适配器

hc2pp10m  于 2021-07-13  发布在  Java
关注(0)|答案(2)|浏览(308)

我正在制作一个链接列表,为此我制作了一个自定义适配器,但该列表在适配器启动时尚未准备好,因此出现以下错误:
java.lang.runtimeexception:无法启动活动组件信息{}:java.lang.nullpointerexception:尝试对空对象引用调用虚拟方法“java.lang.object android.content.context.getsystemservice(java.lang.string)”
这是因为当适配器启动时,列表是空的,并且就在列表被填满的瞬间,但是已经太晚了,下面是我的代码:
更新:代码已更改,因此现在我没有收到错误,但它没有在适配器中运行getview:

public class Controller extends Activity {

private String TAG = Controller.class.getSimpleName();
private String http;
CustomAdapter adapter;
public Controller con = null;
private ListView lv;
private static String url;
ArrayList<Selfservice> linkList = new ArrayList<Selfservice>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_view);
    con = this;
    http = this.getString(R.string.http);
    url = this.getString(R.string.path1);

    new GetLinks().execute();

    lv = (ListView)findViewById(R.id.list);

    //Resources res = getResources();
    //adapter = new CustomAdapter(con, linkList, res);
    //lv.setAdapter(adapter);
}

private class GetLinks extends AsyncTask<Void, Void, List<Selfservice>> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

    }

    @Override
    protected List<Selfservice> doInBackground(Void... arg0) {
                Document doc;
                Elements links;
                List<Selfservice> returnList = null;
                try {
                    doc = Jsoup.connect(url).timeout(0).get();
                    links = doc.getElementsByClass("processlink");
                    returnList = ParseHTML(links);
                } catch (IOException e) {
                    e.printStackTrace();
                }

        return returnList;
    }

    @Override
    protected void onPostExecute(final List<Selfservice> result) {
        super.onPostExecute(result);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
                //setSupportActionBar(toolbar);
                //getSupportActionBar().setDisplayShowTitleEnabled(false);
                toolbar.setTitle("");
                toolbar.setSubtitle("");
                Resources res = getResources();
                Log.e(TAG, linkList.toString());
                linkList = (ArrayList<Selfservice>) result;
                adapter = new CustomAdapter(con, result, res);
                adapter.notifyDataSetChanged();
                lv.setAdapter(adapter);
            }
        });

    }

}

和我的适配器:

public class CustomAdapter extends BaseAdapter implements OnClickListener {
private String TAG = CustomAdapter.class.getSimpleName();
Context context;
List<Selfservice> data;
private Activity activity;
public Resources res;
Selfservice self = null;
private static LayoutInflater inflater;
int layoutResourceId = 0;

public CustomAdapter(Activity act, List<Selfservice> dataList, Resources resources) {

    res = resources;
    activity = act;
    data = dataList;

}

private class Holder {
    TextView title;
    TextView link;
}

@Override
public int getCount() {

    return data.size();
}

@Override
public Object getItem(int pos) {
    return pos;
}

@Override
public long getItemId(int pos) {
    return pos;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = convertView;
    Holder holder;

    if(rowView == null){

        rowView = inflater.inflate(R.layout.list_item, null);
        holder = new Holder();
        holder.title = (TextView) rowView.findViewById(R.id.title);
        holder.link = (TextView) rowView.findViewById(R.id.link);

        rowView.setTag(holder);

    }else{
        holder = (Holder)rowView.getTag();
    }
    if(data.size()<=0){
        holder.title.setText("did not work");
    }else{
        self = null;
        self = (Selfservice) data.get(position);
        holder.title.setText(self.getTitle());
        holder.link.setText(self.getLink());
        Log.i(TAG, "adapter");
        rowView.setOnClickListener(new OnItemClickListener(position));
    }

    return rowView;
    }
@Override
public void onClick(View v){
    Log.v("CustomAdapter", "row clicked");
}

private class OnItemClickListener implements OnClickListener{
    private int mPos;

    OnItemClickListener(int position){
        mPos = position;
    }
    @Override
    public void onClick(View arg0){
        Controller con = (Controller)activity;
        con.onItemClick(mPos);
    }
    }
}

那么如何让适配器等待列表已满?

ljsrvy3e

ljsrvy3e1#

您可以在onpostexecute中创建适配器;
将异步任务更改为:

private class GetLinks extends AsyncTask<Void, Void, List<Selfservice>> {

        @Override 
        protected void onPreExecute() { 
            super.onPreExecute(); 

        } 

        @Override 
        protected Void doInBackground(Void... arg0) {
                    Document doc;
                    Elements links;
                    List<Selfservice> returnList

                    try { 
                        doc = Jsoup.connect(url).timeout(10000).get();
                        links = doc.getElementsByClass("processlink");
                        returnList = ParseHTML(links);
                    } catch (IOException e) {
                        e.printStackTrace();
                    } 

            return returnList; 
        } 

        @Override 
        protected void onPostExecute(List<Selfservice> result) {
            super.onPostExecute(result);
            runOnUiThread(new Runnable() {
                @Override 
                public void run() { 
                    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
                    //setSupportActionBar(toolbar); 
                    //getSupportActionBar().setDisplayShowTitleEnabled(false); 
                    toolbar.setTitle("");
                    toolbar.setSubtitle("");

                    linkList = result
                    adapter = new CustomAdapter(con, result, res);
                    lv.setAdapter(adapter);
                } 
            }); 

        }

这样,您将只在列表就绪时创建适配器。
编辑:您没有在适配器内创建变量上下文。将构造函数更改为:

public CustomAdapter(Context context, Activity act, List<Selfservice> dataList, Resources resources) {

    res = resources;
    activity = act;
    data = dataList;
    this.context = context;

}

你就不会再看到nullpointerexecption了

hujrc8aj

hujrc8aj2#

第一次使用 ArrayAdapter<Selfservice> 而不是 BaseAdapter 使用构造函数

public CustomAdapter(Context context, int resource, List<Selfservice> objects) {
    super(context, resource, objects);
    data = objects;
}

然后只重写两个方法
public int getCount() public View getView(int position, View convertView, ViewGroup parent) 那么 return list.size()getCount() 在getview()方法中

if (convertView == null) {
    LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    v = inflater.inflate(R.layout.list_item, null);
}

在try块的doinbackground()方法中,而不是 linkList = ParseHTML(links);linkList.addAll(ParseHTML(links)); 在onpostexcute()方法中 adapter.notifyDatasetChanged(); 在ui线程中

相关问题