静态类无法解析类型

时间:2014-02-06 08:35:39

标签: android static android-asynctask android-listfragment

我有一个静态类,我想通过AsyncTask onPostExecute设置一个ListAdapter 但它给出了错误:无法将nestedObject解析为类型

静态类(嵌套类):

    public static class DummySectionFragment extends ListFragment
    {
    public PostData[] listData;
    public PostItemAdapter adapter;
    //DownloadWebPageTask task = new DownloadWebPageTask();
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        this.generateDummyData();
        adapter = new PostItemAdapter(inflater.getContext(), android.R.layout.simple_list_item_1, listData);
        setListAdapter(adapter);
        //task.execute();
        View rootView = inflater.inflate(R.layout.fragment_section_news, container, false);
        return rootView;
    }

    private void generateDummyData()
    {
        PostData data = null;
        listData = new PostData[10];
        // sets a default value to Date,Title and URL
        for (int i = 0; i < 10; i++)
        {
            data = new PostData();
            data.postDate = "May 20, 2013";
            data.postTitle = "Post " + (i + 1) + " Title: This is the Post Title from RSS Feed";
            data.postThumbUrl = null;
            listData[i] = data;
        }
    }
}

AsyncTask:

    public static class DownloadWebPageTask extends AsyncTask<List<String>, Void, List<String>>
{
    @Override
    protected List<String> doInBackground(List<String>... urls)
    {
        FeedParser parser = FeedParserFactory.getParser();
        messages = parser.parse();
        List<String> titles = new ArrayList<String>(messages.size());
        for (Message msg : messages)
        {
            titles.add(msg.getTitle());
        }
        return titles;
    }

    @Override
    protected void onPostExecute(List<String> result)
    {
        MainActivity.DummySectionFragment nestedObject =  new MainActivity.DummySectionFragment();
        ArrayAdapter<String> adapter = new ArrayAdapter<String> (nestedObject.this,R.layout.fragment_section_nieuws, result);
        nestedObject.this.setListAdapter(adapter);
    }
}

我正在尝试在ListFragment而不是ListActivity中执行this

1 个答案:

答案 0 :(得分:1)

对于setListAdapter()来电,请nestedObject.this放置nestedObject

第一个表单尝试使用类名限定this指针,但nestedObject是对象引用,而不是类名。您使用对象引用而不是this来引用其他对象。

对于ArrayAdapter构造函数,传入有效的Context。由于该类是static内部类,因此它无权访问包含的类。您需要将Context(例如您的活动参考)作为参数传递给DownloadWebPageTask。例如:

public static class DownloadWebPageTask extends AsyncTask<List<String>, Void, List<String>> {
    private Context mContext;

    DownloadWebPageTask(Context context) {
        mContext = context;
    }

    // now use mContext where a context is needed

(请注意,保留这样的Activity引用可能会导致严重的内存泄漏。但是我们首先解决您的构建问题。您的代码中还存在许多其他问题 - 交互式修复它们都超出了范围SO的问题是什么。)