在 Android 中使用 EndlessAdapter 时出错

发布于 2024-12-26 15:03:58 字数 2352 浏览 1 评论 0原文

在使用 EndlessAdapter 时,我遇到了这个错误:

无法在以下语句中实例化类型 EndlessAdapter:

EndlessAdapter adapter = new EndlessAdapter(this); 

I已经在 EndlessAdapter.java 中定义了一个适配器类并导入了它。

这是我的整个代码:

package com.example.litest;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.example.utilities.TestListAdapter;
import com.example.utilities.EndlessAdapter;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;



public class ListActivity extends Activity{
    public final static String ITEM_TITLE = "title";  
    public final static String ITEM_CAPTION = "caption";  

    public Map<String,?> createItem(String title, String caption) {  
        Map<String,String> item = new HashMap<String,String>();  
        item.put(ITEM_TITLE, title);  
        item.put(ITEM_CAPTION, caption);  
        return item;  
    }  

    @Override  
    public void onCreate(Bundle icicle) {  
        super.onCreate(icicle);  

        List<Map<String,?>> security = new LinkedList<Map<String,?>>();  
        security.add(createItem("Remember passwords", "Save usernames and passwords for Web sites"));  
        security.add(createItem("Clear passwords", "Save usernames aznd passwords for Web sites"));  
        security.add(createItem("Show security warnings", "Show warning if there is a problem with a site's security"));  
        for(int n=10; n>0; n--)
            security.add(createItem("Clear passwords", "Save usernames and passwords for Web sites"));  

        // create our list and custom adapter  
        EndlessAdapter adapter = new EndlessAdapter(this) 


        //adapter.addSection("Security", new EndlessAdapter(this, security, R.layout.list_complex,new String[] { ITEM_TITLE, ITEM_CAPTION }, new int[] { R.id.list_complex_title, R.id.list_complex_caption }));  

        ListView list = new ListView(this);  
        list.setAdapter(adapter);  
        this.setContentView(list);  

    }  

}  

Logcat:http://pastebin.com/2WnnJ9KA

While using EndlessAdapter, I came across this error:

Cannot instantiate the type EndlessAdapter at this following statement:

EndlessAdapter adapter = new EndlessAdapter(this); 

I've defined an adapter class in EndlessAdapter.java and have imported it.

This is my entire code:

package com.example.litest;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.example.utilities.TestListAdapter;
import com.example.utilities.EndlessAdapter;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;



public class ListActivity extends Activity{
    public final static String ITEM_TITLE = "title";  
    public final static String ITEM_CAPTION = "caption";  

    public Map<String,?> createItem(String title, String caption) {  
        Map<String,String> item = new HashMap<String,String>();  
        item.put(ITEM_TITLE, title);  
        item.put(ITEM_CAPTION, caption);  
        return item;  
    }  

    @Override  
    public void onCreate(Bundle icicle) {  
        super.onCreate(icicle);  

        List<Map<String,?>> security = new LinkedList<Map<String,?>>();  
        security.add(createItem("Remember passwords", "Save usernames and passwords for Web sites"));  
        security.add(createItem("Clear passwords", "Save usernames aznd passwords for Web sites"));  
        security.add(createItem("Show security warnings", "Show warning if there is a problem with a site's security"));  
        for(int n=10; n>0; n--)
            security.add(createItem("Clear passwords", "Save usernames and passwords for Web sites"));  

        // create our list and custom adapter  
        EndlessAdapter adapter = new EndlessAdapter(this) 


        //adapter.addSection("Security", new EndlessAdapter(this, security, R.layout.list_complex,new String[] { ITEM_TITLE, ITEM_CAPTION }, new int[] { R.id.list_complex_title, R.id.list_complex_caption }));  

        ListView list = new ListView(this);  
        list.setAdapter(adapter);  
        this.setContentView(list);  

    }  

}  

Logcat: http://pastebin.com/2WnnJ9KA

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

可是我不能没有你 2025-01-02 15:03:58

无法在以下语句中实例化 EndlessAdapter 类型:

EndlessAdapter adapter = new EndlessAdapter(this); 

当然。没有这样的构造函数。该文档没有引用这样的构造函数。 demo/ 应用程序不使用此类构造函数。 EndlessAdapter 的代码不包含这样的构造函数。请使用定义的构造函数之一。引用文档:

EndlessAdapter 有两个构造函数。最初的一个以ListAdapter作为参数,代表现有的适配器将被无限化。您的 EndlessAdapter 子类需要重写此构造函数并向上链接。例如,演示项目中的 DemoAdapterArrayList 作为构造函数参数,并将其包装在 ListAdapter 中以提供给 <代码>EndlessAdapter。

第二个构造函数采用 Context 和资源 ID 以及 ListAdapter。这些将用于创建占位符(见下文)。


发布日志猫。

不,你没有。

我可以告诉你问题所在,那就是在manifest.xml中我必须添加com.commonsware.cwac.adapter包。这里的问题是,我的主包是 com.litest.main,这是我放在 manifest.xml 中的内容

您不需要在清单中的任何位置包含 com.commonsware.cwac.adapter 。请参阅 GitHub 存储库中的 demo/ 项目。

Cannot instantiate the type EndlessAdapter at this following statement:

EndlessAdapter adapter = new EndlessAdapter(this); 

Of course. There is no such constructor. The documentation does not cite such a constructor. The demo/ app does not use such a constructor. The code to EndlessAdapter does not contain such a constructor. Use one of the defined constructors, please. Quoting the documentation:

EndlessAdapter has two constructors. The original one takes a ListAdapter as a parameter, representing the existing adapter to be made endless. Your EndlessAdapter subclass will need to override this constructor and chain upwards. For example, the DemoAdapter inside the demo project takes an ArrayList<String> as a constructor parameter and wraps it in a ListAdapter to supply to EndlessAdapter.

The second constructor takes a Context and resource ID along with the ListAdapter. These will be used to create the placeholder (see below).


Posted the log cat.

No, you did not.

I can tell you the problem, that is in manifest.xml I've to add the com.commonsware.cwac.adapter package. The problem here is, my main package is com.litest.main, which is what I've put in manifest.xml

You do not need to have com.commonsware.cwac.adapter anywhere in your manifest. Please see the demo/ project in the GitHub repository.

羞稚 2025-01-02 15:03:58

发布实际的 logcat 错误。您很可能尝试在“this”未引用正确的Context 的位置实例化它。您可能需要添加 Activity 类的名称,例如 new EndlessAdapter(MyActivity.this);

Post the actual logcat error. In all likelihood, you are trying to instantiate it at a point where "this" does not refer to the proper Context. You'll likely need to add the name of your Activity class, like new EndlessAdapter(MyActivity.this);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文