Error using onClickListener (Intent)

后端 未结 5 1335
無奈伤痛
無奈伤痛 2020-12-06 10:33

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

 Button btEntrar = (Button) findViewById(R.id.btnSingIn);
    btEntrar.setOnClickLis         


        
相关标签:
5条回答
  • 2020-12-06 10:44

    try using

    Intent i = new Intent(login.this, mainActivity.class);
    

    hope this helps

    0 讨论(0)
  • 2020-12-06 10:53

    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)
    
    0 讨论(0)
  • 2020-12-06 10:55

    updated code in to your activity

    Button btEntrar = (Button) findViewById(R.id.btnSingIn);
     btEntrar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i;
            i = new Intent(login.this, MainActivity.class);
            startActivity(i);
    
    
        }
    });
    
    0 讨论(0)
  • 2020-12-06 11:00

    use if you want to send it from login.java to mainactivity.class use

     Intent intent=new Intent(login.this,Mainactivity.class);
    startActivity(intent);
    
    0 讨论(0)
  • 2020-12-06 11:07

    Did you add manifest.xml or try this code ?
    Intent i = new Intent(login.this,mainActivity.class);

    0 讨论(0)
提交回复
热议问题