【问题标题】:Static class cannot be resolved a type静态类无法解析类型
【发布时间】:2014-02-06 08:35:38
【问题描述】:

我有一个静态类,我想通过 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;
        }
    }
}

异步任务:

    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

【问题讨论】:

    标签: android static android-asynctask android-listfragment


    【解决方案1】:

    对于setListAdapter() 呼叫,只需将nestedObject.thisnestedObject 放在一起。

    第一种形式尝试用类名限定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问题是为了。)

    【讨论】:

    • 当我删除它时,我收到两个新错误:构造函数 ArrayAdapter(MainActivity.DummySectionFragment,int,List) 未定义
    • DownloadWebPageTask 在 MainActivity 中,所以我这样做了:ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt; (MainActivity.this,R.layout.fragment_section_nieuws, result); 现在它抛出:没有 MainActivity 类型的封闭实例在范围内是可访问的
    • 当我这样做并添加 mContext 时,MainActivity.this 在上面的评论中,我无法再在 ListFragment 中执行我的任务
    • 如您所见,我更新了我的 DummySectionFragment 类,它现在抛出:构造函数 MainActivity.DownloadWebPageTask() 未定义在线 DownloadWebPageTask task = new DownloadWebPageTask();
    • 由于我们添加了带有Context arg 的显式构造函数,因此您需要传递例如MainActivity.this 作为 arg 存在:new DownloadWebPageTask(MainActivity.this)
    猜你喜欢
    • 1970-01-01
    • 2018-12-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-22
    • 2021-10-14
    • 2014-06-26
    • 1970-01-01
    • 2015-11-22
    相关资源
    最近更新 更多