Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java OnPostExecute不提供类的上下文_Java_Android_Listview - Fatal编程技术网

Java OnPostExecute不提供类的上下文

Java OnPostExecute不提供类的上下文,java,android,listview,Java,Android,Listview,我的公共类中有一个私有子类。这个私有类扩展了AsyncTask。 在onPostExecute()中,我必须通过自定义适配器发送对象列表。 但问题是,当我获取主公共类(私有子类的父类)的“上下文”时,它在@Override(OnPostExecute())上给出了一个错误 公共类MainListActivity扩展ListActivity{ 受保护字符串[]mBlogPost; 公共静态最终整数,邮政编码=20; 公共静态最终字符串标记=MainListActivity.class.getSim

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

公共类MainListActivity扩展ListActivity{
受保护字符串[]mBlogPost;
公共静态最终整数,邮政编码=20;
公共静态最终字符串标记=MainListActivity.class.getSimpleName();
公共void加载页(){
Log.d(标签“loadpage me a gae nh”);
//如果((sPref.equals(ANY))&&(wifiConnected | | mobileConnect)){
新建DownloadXmlTask().execute(URL);
}
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u主列表);
loadPage();
}
私有类DownloadXmlTask扩展了AsyncTask{
@凌驾
受保护对象doInBackground(字符串…URL){
试一试{
返回loadXmlFromNetwork(URL[0]);
}捕获(IOE异常){
返回“I/O异常ae hy”;
}catch(XMLPullParseRexE){
返回“XML pull解析器ke异常ae hy”;
}
}
@凌驾
受保护的void onPostExecute(列出结果){
字符串标题、链接、摘要;
ArrayList blogPosts=新的ArrayList();
for(StackOverflowXmlParser.Entry结果:结果){
title=result.title;
link=result.link;
summary=result.summary;
HashMap blogPost=新的HashMap();
blogPost.put(“link”,link);
blogPost.put(“title”,title);
//blogPost.put(“摘要”,summary);
添加(blogPost);
}
字符串[]键={“标题”,“链接”};
int[]ids={android.R.id.text1,android.R.id.text2};
SimpleAdapter适配器=
新的simpledapter(MainListActivity.this,blogPosts,android.R.layout.simple\u list\u item\u 2,key,id);
setListAdapter(适配器);
}
它在@Override上给出了一个错误。我必须获取私有子类的父类的上下文(这扩展了asyncTask并具有onPostExecute())

我还使用了getApplicationContext()。此外,我还为context创建了一个新的公共类,并从中获取context

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;
    }
私有对象loadXmlFromNetwork(字符串urlString)抛出XmlPullParserException,IOException{
InputStream=null;
//实例化解析器
StackOverflowXmlParser StackOverflowXmlParser=新的StackOverflowXmlParser();
列表条目=空;
字符串标题=null;
字符串url=null;
字符串摘要=null;
Calendar rightNow=Calendar.getInstance();
DateFormat格式化程序=新的SimpleDataFormat(“MMM dd h:mmaa”);
StringBuilder htmlString=新的StringBuilder();
试一试{
stream=下载URL(urlString);
entries=stackOverflowXmlParser.parse(流);
//确保在应用程序启动后关闭InputStream
//用完了。
}最后{
if(流!=null){
stream.close();
}
}
for(StackOverflowXmlParser.Entry:entries){
htmlString.append(“

”); htmlString.append(entry.summary); } for(StackOverflowXmlParser.Entry:entries) { Log.d(标签,entry.link+“/”+entry.title); } //返回htmlString.toString(); 返回条目; }

或者将
AsyncTask
更改为

AsyncTask<String, Void, List<StackOverflowXmlParser.Entry>>
异步任务
如果这样做,还必须更改
protectedobjectdoinbackground(String…url)
,以将该返回类型与
onPostExecute()
期望的类型相匹配。在该方法中返回什么值取决于您(但必须与返回类型相匹配),我不会详细介绍如何实现它

另请参考以下图片:

您的
异步任务的实现不正确。您将泛型类型指定为
,但您的
onPostExecute
方法使用的参数类型为
List

将其更改为以下内容

private class DownloadXmlTask extends AsyncTask<String, Void, List<StackOverflowXmlParser.Entry>> {
    private Exception mException = null;
    private Context mContext

    public DownloadXmlTask(Context context) {
        mContext = context;
    }

    @Override
    protected List<StackOverflowXmlParser.Entry> doInBackground(String... urls) {
        try {
            return loadXmlFromNetwork(urls[0]);
        } catch (IOException e) {
            mException = e;
        } catch (XmlPullParserException e) {
            mException = e;
        }

        return null;
    }

    @Override
    protected void onPostExecute(List<StackOverflowXmlParser.Entry> results) {
        if (results != null && mException == null) {
          // Do your stuff
          // Rather use mContext as the context for your SimpleAdapter. Injection is always better.
        } else {
            if (mException instanceof IOException){
              //Handle the IOException seperately
            } else if (mException instanceof XmlPullParserException) {
              //Handle the XmlPullParserException seperately
            }
        }
    }
}
私有类下载XmlTask扩展AsyncTask{
私有异常mException=null;
私有上下文mContext
公共下载XmlTask(上下文){
mContext=上下文;
}
@凌驾
受保护列表doInBackground(字符串…URL){
试一试{
返回loadXmlFromNetwork(URL[0]);
}捕获(IOE异常){
mException=e;
}catch(XMLPullParseRexE){
mException=e;
}
返回null;
}
@凌驾
受保护的void onPostExecute(列出结果){
if(结果!=null&&mException==null){
//做你的事
//而是使用mContext作为SimpleAdapter的上下文。注入总是更好。
}否则{
if(IOException的MEException实例){
//分别处理IOException
}else if(XmlPullParserException的MeException实例){
//分别处理XMLPullParseRexException
}
}
}
}

当您在post execute中时,这也会给您一个异常显示StackTrace错误是什么?错误:(128,9)错误:方法不重写或实现超类型中的方法注意:C:\Users\Talha\AndroidStudioProjects\AppforBlog\app\src\main\java\com\example\Talha\AppforBlog\MainListActivity.java使用或重写不推荐的API。错误表示方法不重写其超类中的方法
AsyncTask
尝试更改为
AsyncTask
匹配签名这可能是正确的,但可能不是,因为我们不知道加载XML的是什么
protected void onPostExecute(List<StackOverflowXmlParser.Entry> results)
protected void onPostExecute(Object results)
AsyncTask<String, Void, List<StackOverflowXmlParser.Entry>>
private class DownloadXmlTask extends AsyncTask<String, Void, List<StackOverflowXmlParser.Entry>> {
    private Exception mException = null;
    private Context mContext

    public DownloadXmlTask(Context context) {
        mContext = context;
    }

    @Override
    protected List<StackOverflowXmlParser.Entry> doInBackground(String... urls) {
        try {
            return loadXmlFromNetwork(urls[0]);
        } catch (IOException e) {
            mException = e;
        } catch (XmlPullParserException e) {
            mException = e;
        }

        return null;
    }

    @Override
    protected void onPostExecute(List<StackOverflowXmlParser.Entry> results) {
        if (results != null && mException == null) {
          // Do your stuff
          // Rather use mContext as the context for your SimpleAdapter. Injection is always better.
        } else {
            if (mException instanceof IOException){
              //Handle the IOException seperately
            } else if (mException instanceof XmlPullParserException) {
              //Handle the XmlPullParserException seperately
            }
        }
    }
}