0

I'm writing an android apps in Java of Eclipse. I'm not very familiar with the java syntax. I encounter this error.

 The constructor Intent(new AdapterView.OnItemClickListener(){},
 Class<NoteEditor> ) is undefined

Below is the code

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 is extends Activity of Android. The above code is correct because I write it in another place it's no error.

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);
    }
}
4
  • No, the code isn't correct, which is why you're getting a compiler error. this doesn't refer to what you think it does there; it's referring to the anonymous class you're instatiating. Commented Nov 15, 2011 at 5:55
  • change like this Intent intent = new Intent(YourActivity.this, NoteEditor.class); Commented Nov 15, 2011 at 5:57
  • post your whole activity code. Commented Nov 15, 2011 at 6:20
  • write this, Intent intent = new Intent(Category.this, NoteEditor.class); Commented Nov 15, 2011 at 6:27

3 Answers 3

1

The context used in your code is wrong, as you're using the anonymous inner class's this. What you should be using is the Activity's context, like so:

 Intent intent = new Intent(Category.this, NoteEditor.class);

The first parameter indicates the calling class's context. So you can use the Activity's this or getBaseContext()

public Intent (Context packageContext, Class<?> cls)
Sign up to request clarification or add additional context in comments.

2 Comments

But it show "No enclosing instance of the type NoteEditor is accessible in scope". What's the problem?
"public class Category extends ListActivity"
1

Here in your code this refers to your new AdapterView class not a activity,

and for Intent constructor you have to pass a reference of your current activity or application's base context,

replace your code,

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);
    }

  });

EDIT: also you can write

Intent intent = new Intent(<your current activity name>.this, NoteEditor.class);

Comments

0

Your problem is that this applies to the anonymous inner class rather than your Context subclass instance. In general, you'd write YourEnclosingClassName.this to get to that. In your case, you need NodeEditor.this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.