在 Android 中使用 EndlessAdapter 时出错
在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当然。没有这样的构造函数。该文档没有引用这样的构造函数。
demo/
应用程序不使用此类构造函数。EndlessAdapter
的代码不包含这样的构造函数。请使用定义的构造函数之一。引用文档:不,你没有。
您不需要在清单中的任何位置包含
com.commonsware.cwac.adapter
。请参阅 GitHub 存储库中的demo/
项目。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 toEndlessAdapter
does not contain such a constructor. Use one of the defined constructors, please. Quoting the documentation:No, you did not.
You do not need to have
com.commonsware.cwac.adapter
anywhere in your manifest. Please see thedemo/
project in the GitHub repository.发布实际的 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 yourActivity
class, likenew EndlessAdapter(MyActivity.this);