我正在尝试在我的应用程序中从网络上提取一些信息。网络交互不能在 MainActivity 中完成,所以我有一个 Runnable 实例来处理这个。但是,自从我开始这样做以来,我一直在收到一个空指针异常。LogCat 将我指向这些代码块:
public CelebrityEntryAdapter(Activity context, List<CelebrityEntry> list) {
super(context, R.layout.activity_celebrity, list);
this.context = context;
this.list = list;
}
在这里(在“适配器=新的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 )
这是填充():
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;
}