OnPostExecute没有给出类的上下文

时间:2015-10-29 12:18:44

标签: java android listview

我的公共类中有一个私有子类。这个私有类扩展了AsyncTask。 在onPostExecute()中,我必须通过自定义适配器发送我的对象列表。 但问题是当我得到"背景"主公共类(私有子类的父),它在@Override(OnPostExecute())上给出错误。

public class MainListActivity extends ListActivity {

protected String[] mBlogPost;
public static final int NUMBER_OF_POST = 20;
public static final String TAG= MainListActivity.class.getSimpleName();

    public void loadPage() {
    Log.d(TAG,"loadpage me a gae nh");
   // if((sPref.equals(ANY)) && (wifiConnected || mobileConnected)) {
        new DownloadXmlTask().execute(URL);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_list);
    loadPage();
}
private class DownloadXmlTask extends AsyncTask<String, Void, Object> {
    @Override
    protected Object doInBackground(String... urls) {
        try {

            return loadXmlFromNetwork(urls[0]);

        } catch (IOException e) {
            return "I/O exception ae hy";
        } catch (XmlPullParserException e) {
            return "XML pull parser ke exception ae hy";
        }
    }

    @Override
    protected void onPostExecute(List<StackOverflowXmlParser.Entry> results) {
        String title,link,summary;
        ArrayList<HashMap<String,String>> blogPosts = new ArrayList<HashMap<String,String>>();
        for(StackOverflowXmlParser.Entry result : results){
            title = result.title;
            link = result.link;
            summary = result.summary;
            HashMap<String,String> blogPost = new HashMap<String,String>();
            blogPost.put("link",link);
            blogPost.put("title",title);
            //blogPost.put("summary",summary);

            blogPosts.add(blogPost);
        }
        String[] keys = {"title","link"};
        int[] ids = {android.R.id.text1,android.R.id.text2};
        SimpleAdapter adapter =
                new SimpleAdapter(MainListActivity.this,blogPosts,android.R.layout.simple_list_item_2,keys,ids);
        setListAdapter(adapter);
    }

@Override发出错误。我必须得到私有子类的父类的上下文(这扩展了asyncTask并具有onPostExecute())

我也使用了getApplicationContext()。此外,我还为上下文创建了一个新的公共类,并从那里获得了上下文。

private Object loadXmlFromNetwork(String urlString) throws XmlPullParserException, IOException {
        InputStream stream = null;
        // Instantiate the parser
        StackOverflowXmlParser stackOverflowXmlParser = new StackOverflowXmlParser();
        List<StackOverflowXmlParser.Entry> entries = null;
        String title = null;
        String url = null;
        String summary = null;
        Calendar rightNow = Calendar.getInstance();
        DateFormat formatter = new SimpleDateFormat("MMM dd h:mmaa");


        StringBuilder htmlString = new StringBuilder();

        try {
            stream = downloadUrl(urlString);
            entries = stackOverflowXmlParser.parse(stream);
            // Makes sure that the InputStream is closed after the app is
            // finished using it.
        } finally {
            if (stream != null) {
                stream.close();
            }
        }
        for (StackOverflowXmlParser.Entry entry : entries) {
            htmlString.append("<p><a href='");
            htmlString.append(entry.link);
            htmlString.append("'>" + entry.title + "</a></p>");
            htmlString.append(entry.summary);

        }
        for (StackOverflowXmlParser.Entry entry : entries)
        {
            Log.d(TAG, entry.link + " /" + entry.title);
        }

            //return htmlString.toString();
            return entries;
    }

2 个答案:

答案 0 :(得分:3)

private class DownloadXmlTask extends AsyncTask<String, Void, Object>

在这一行中你定义了3件事。

String传递给protected Object doInBackground(String... urls)的参数类型。这是正确的。

Void传递给protected void onProgressChanged(Integer... values)的参数类型。你没有使用这种方法所以应该没问题。

Object传递给protected void onPostExecute(List<StackOverflowXmlParser.Entry> results)的参数类型。这是错的。你需要匹配这些。要么改变

protected void onPostExecute(List<StackOverflowXmlParser.Entry> results)

protected void onPostExecute(Object results)

或将AsyncTask<String, Void, Object>更改为

AsyncTask<String, Void, List<StackOverflowXmlParser.Entry>>

然后,如果您这样做,则还必须更改protected Object doInBackground(String... urls)以使该返回类型与onPostExecute()期望的类型相匹配。您将在该方法中返回的值取决于您(虽然它必须与返回类型匹配),我不会详细说明如何实现它。

请参阅this answer中的图片: enter image description here

答案 1 :(得分:2)

object WeatherForwarder { def props : Props = Props[WeatherForwarder] } //see provided link for example definition class WeatherForwarder extends Actor {...} val actorRef = actorSystem actorOf WeatherForwarder.props //note the stream has not been instatiated yet actorRef ! Weather("02139", 32.0, true) //stream already has 1 Weather value to process which is sitting in the //ActorRef's internal buffer val stream = Source(ActorPublisher[Weather](actorRef)).runWith{...} 的实施不正确。您将通用类型指定为AsyncTask,但您的<String, Void, Object>方法采用onPostExecute类型的参数

将其更改为以下

List<StackOverflowXmlParser.entry>

当你执行后

时,这也会给你例外