我试图从我的应用程序中的网络拉一些信息。网络交互不能在MainActivity中完成,所以我有一个Runnable的实例来处理这个问题。然而,自从我开始,我得到一个空指针异常。 logcat的是在这些代码块指着我:Android的EntryAdapter超级崩溃()
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):致命异常:线程109
05-01 18:49:36.391: E/AndroidRuntime(1468):java.lang.NullPointerException
05-01 18:49:36.391:E/AndroidRuntime(1468):at android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
05-01 18:49:36.391:E/AndroidRuntime(1468):at android.widget.ArrayAdapter。(ArrayAdapter.java:153)
05-01 18:49:36.391: E/AndroidRuntime(1468):at edu.Drake.doppelganger.CelebrityEntryAdapter。(CelebrityEntryAdapter.java:31)
05-01 18:49:36.391:E/AndroidRuntime(1468):at 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;
}
你的logcat在哪里? – 2013-05-01 18:58:51
populate()的定义在哪里?也许它返回空值 – eltabo 2013-05-01 19:07:26