我有一个主要活动类的实例变量(art)在onPostExecution方法中被赋值.Log语句在onPostExecute方法中打印art的值。
但在onPostExecute方法之后,art的值为null。
public class MainActivity extends ActionBarActivity implements OnItemClickListener {
ListView listView1;
String[] dummyData = {"sunday","monday","tuesday","wednesday","thursday","friday","saturday","sunday","monday","tuesday",
"wednesday","thursday","friday","saturday","sunday","monday","tuesday","wednesday","thursday","friday","saturday"};
ArrayAdapter<String> adapter ;
ArrayList<String> summery = new ArrayList<String>(4);
ArrayList<String> links = new ArrayList<String>(4);
Elements art;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String stringUrl = "https://techcards.wordpress.com";
Fetch fetch = new Fetch();
fetch.execute(stringUrl);
try{
for (int i =0;i<art.size() ;i++ ) {
Log.v("articles after post executive",art.get(i).toString());
links.add(i,art.get(i).getElementsByTag("a").toString());
Log.v("links",art.toString());
summery.add(i,art.get(i).getElementsByTag("p").text());
}
}catch(NullPointerException e){
e.printStackTrace();
System.out.println("art is null");
}
listView1 = (ListView) findViewById(R.id.listView1);
adapter = new ArrayAdapter<String>(this,R.layout.single_row,R.id.textView2, links);
listView1.setAdapter(adapter);
listView1.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
TextView t = (TextView) view.findViewById(R.id.textView2);
String text =(String) t.getText();
Toast.makeText(this, text, Toast.LENGTH_SHORT).show();//this makeText is a static method
}
// Fetch AsyncTask
private class Fetch extends AsyncTask<String, Void, Elements> {
@Override
protected Elements doInBackground(String... params) {
Elements articles = null;
try {
// Connect to the web site
Document doc = Jsoup.connect(params[0]).get();
Element main =doc.getElementById("content").getElementById("primary").
getElementById("main");
articles = main.getElementsByClass("entry-summary");
} catch (IOException e) {
e.printStackTrace();
System.out.println("IO exception");
}
return articles;
}
protected void onPostExecute(Elements result) {
art = result;
for (int i =0;i<art.size() ;i++ ) {
Log.v("links in post executive",art.get(i).getElementsByTag("a").toString());
Log.v("summery in post executive",art.get(i).getElementsByTag("p").text());
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
当我试图用它来更新链接和夏季时,艺术正在给出空指针异常。答案 0 :(得分:1)
AsyncTask异步工作。 之后的那些行:
fetch.execute(stringUrl);
在 onPostExecute
之后不起作用,但它与AsyncTask中的代码同时(或左右)起作用。这就是为什么AsyncTask被称为AsyncTask。
这就是你的art变量为null的原因,因为onPostExecute还没有被调用。
答案 1 :(得分:1)
onPostExecute
执行完成时运行 doInBackground
方法。所以在onPostExecute
中的适配器中添加数据,而不是在启动AsyncTask
后添加数据:
@Override
protected void onPostExecute(Elements result) {
super.onPostExecute(result);
// 1. Add data in summery from result
// 2. Pass summery to Adapter constructor
adapter = new ArrayAdapter<String>(MainActivity.this,R.layout.single_row,
R.id.textView2, links);
listView1.setAdapter(adapter);
}