Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Well, I'm trying to create an intent on a "login.java" the code is :

 Button btEntrar = (Button) findViewById(R.id.btnSingIn);
    btEntrar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i;
            i = new Intent(this, MainActivity.class);
            startActivity(i);


        }
    });

But it says that I can't go to the other activity saying this :

Error:(24, 21) error: no suitable constructor found for Intent(,Class) constructor Intent.Intent(String,Uri) is not applicable (argument mismatch; cannot be converted to String) constructor Intent.Intent(Context,Class) is not applicable (argument mismatch; cannot be converted to Context)

and...

Error:Execution failed for task ':app:compileDebugJava'. Compilation failed; see the compiler error output for details.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
172 views
Welcome To Ask or Share your Answers For Others

1 Answer

Just a few lines to explain the reason why this does not work in:

i = new Intent(this, MainActivity.class)

The intent is created inside another class, here an anonymous inner class OnClickListener. Thus this does not refer the instance of your Activity (or Context) as intended but the instance of your anonymous inner class OnClickListener.

So you should provide the correct context of your class.

i = new Intent(YourClassName.this, MainActivity.class)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...