我正在用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的Activity的扩展。上面的代码是正确的,因为我在另一个地方编写了它,没有错误。
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);
}
}
最佳答案
代码中使用的上下文是错误的,因为您正在使用匿名内部类的this
。您应该使用的是活动的上下文,如下所示:
Intent intent = new Intent(Category.this, NoteEditor.class);
第一个参数指示调用类的上下文。因此,您可以使用活动的
this
或getBaseContext()
public Intent (Context packageContext, Class<?> cls)