Android EntryAdapter在super()上崩溃

时间:2013-05-01 18:51:11

标签: android multithreading

我正试图在我的应用中从网上提取一些信息。网络交互无法在MainActivity中完成,因此我有一个Runnable实例来处理这个问题。但是,自从我开始,我一直得到一个空指针异常。 LogCat指向我这些代码:

public CelebrityEntryAdapter(Activity context, List<CelebrityEntry> list) {
    super(context, R.layout.activity_celebrity, list);
    this.context = context;
    this.list = list;
}

和这里(在“adapter = new CelebrityEntryAdapter ...”行):

public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    context = this;
    filterText = (EditText) getView().findViewById(R.id.search_box);
    filterText.addTextChangedListener(filterTextWatcher);
    new Thread(new Runnable(){
        public void run()
        {
            try {
                adapter = new CelebrityEntryAdapter(context.getActivity(), populate());
            } catch (IOException e) {
                e.printStackTrace();
            }
            setListAdapter(adapter);

            filterTextWatcher = new TextWatcher() {

                public void afterTextChanged(Editable s) {
                }

                public void beforeTextChanged(CharSequence s, int start, int count,
                        int after) {
                }

                public void onTextChanged(CharSequence s, int start, int before,
                        int count) {
                    adapter.getFilter().filter(s);
                }
            };
        }
    }).start();
}

您怎么看?

编辑:

这是logcat:

05-01 18:49:36.391:E / AndroidRuntime(1468):致命异常:Thread-109
05-01 18:49:36.391:E / AndroidRuntime(1468):java.lang.NullPointerException
05-01 18:49:36.391:E / AndroidRuntime(1468):在android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
05-01 18:49:36.391:E / AndroidRuntime(1468):在android.widget.ArrayAdapter。(ArrayAdapter.java:153)
05-01 18:49:36.391:E / AndroidRuntime(1468):在edu.Drake.doppelganger.CelebrityEntryAdapter。(CelebrityEntryAdapter.java:31)
05-01 18:49:36.391:E / AndroidRuntime(1468):在edu.Drake.doppelganger.CelebritiesFragment $ 1.run(CelebritiesFragment.java:60)
05-01 18:49:36.391:E / AndroidRuntime(1468):在java.lang.Thread.run(Thread.java:856)

这是populate():

public List<CelebrityEntry> populate() throws IOException {
    List<CelebrityEntry> list = new ArrayList<CelebrityEntry>();
    HttpGet httppost = new HttpGet("http://www.ethanclevenger.com/MirrorMe/celebrity-master.txt");
    HttpResponse response = httpclient.execute(httppost);
    HttpEntity ht = response.getEntity();
    BufferedHttpEntity buf = new BufferedHttpEntity(ht);
    InputStream is = buf.getContent();
    BufferedReader r = new BufferedReader(new InputStreamReader(is));
    String name;
    String url;
    while ((name = r.readLine()) != null) {
        url = r.readLine();
        Log.v(url, url);
        list.add(new CelebrityEntry(name, url));
    }
    return list;
}

抱歉糟糕的logcat格式 - 试图解决这个问题。我已经能够从“populate()”中记录“url”和“name” - 它肯定会检索它。

为了更好的衡量,这里是构造函数:

public CelebrityEntry(String name, String pic) {
    this.name = name;
    this.pic = pic;
}

1 个答案:

答案 0 :(得分:0)

您的想法context.getActivity()似乎正在返回null,因为这是NullPointerException init()方法中ArrayAdapter的唯一来源。

除此之外:

  • 请不要尝试从后台线程修改UI,就像在这里一样(专业提示:使用AsyncTask

  • 请不要盲目地忽略异常,例如您正在使用IOException