我正在用Eclipse的Java编写一个android应用程序。我对java的语法不是很熟悉。我遇到了这个错误。
The constructor Intent(new AdapterView.OnItemClickListener(){},
Class<NoteEditor> ) is undefined下面是代码
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(this, NoteEditor.class);
startActivity(intent);
}
});NoteEditor是Android的扩展活动。上面的代码是正确的,因为我在另一个地方写了它,它没有错误。
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.new_game:
Intent intent = new Intent(this, NoteEditor.class);
startActivity(intent);
//newGame();
return true;
default:
return super.onOptionsItemSelected(item);
}
}发布于 2011-11-15 13:55:06
代码中使用的上下文是错误的,因为您使用的是匿名内部类的this。您应该使用活动的上下文,如下所示:
Intent intent = new Intent(Category.this, NoteEditor.class);第一个参数表示调用类的上下文。因此,您可以使用活动的this或getBaseContext()
public Intent (Context packageContext, Class<?> cls)发布于 2011-11-15 13:55:01
在您的代码中,this指的是new AdapterView class not a activity,
对于意图构造器,您必须传递当前活动或应用程序的基上下文的引用,
替换你的代码,
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(getBaseContext(), NoteEditor.class);
startActivity(intent);
}
});编辑:你也可以写
Intent intent = new Intent(<your current activity name>.this, NoteEditor.class);发布于 2011-11-15 13:53:56
您的问题是this应用于匿名内部类,而不是您的Context子类实例。通常,您需要编写YourEnclosingClassName.this来实现这一点。在您的示例中,您需要NodeEditor.this。
https://stackoverflow.com/questions/8132039
复制相似问题