I have a problem with trying to open another activity

0

Good evening friends puej the truth I do not know what is wrong I'm drowning in a small problem because I tell you that an attempt to open another activity does not work for me not that it is wrong but the other intents if: here the code:

@Override
public void onClick(View v) {


    if(v.getId() == R.id.btnAgregarSoporte){
        Intent abrirAgregarSoporteActivity = new Intent(this,AgregarSoporteActivity.class);
        startActivity(abrirAgregarSoporteActivity);
    }
    if(v.getId() == R.id.btnPublicarAnuncio){
        Intent abrirAgregarUbicacionYProdInm = new Intent(getApplicationContext(),AgregarUbicacionYproductoInmueble.class);
        startActivity(abrirAgregarUbicacionYProdInm);
    }

    if(v.getId() == R.id.btnVerAnuncios){
            Intent AbrirAnuncios = new Intent(this,AnunciosActivity.class);
        Toast.makeText(getApplicationContext(),"Pero llega",Toast.LENGTH_LONG).show();  // inclusive llega hasta aqui cuando quito la linea de abajo
        startActivity(AbrirAnuncios); // cuando quito esta linea funciona pero obviamente no abre la otra activity si no que muestra el mensaje de la anterior linea
    }




}

As you see all the intents here work except the frame with comment the truth is not what happens I have set everything and nothing can help me, because the application is closed by pressing that button see ads. thanks.

In case you need something else, I can modify the question and add more code or whatever you need.

    
asked by David Garcia 26.10.2018 в 05:18
source

2 answers

0

As the previous comment says it is incorrect to use this within the onclick method, so the solution would be:

@Override
public void onClick(View v) {
    if(v.getId() == R.id.btnAgregarSoporte){
startActivity(new Intent(getApplicationContext(),AgregarSoporteActivity.class));
    }
    if(v.getId() == R.id.btnPublicarAnuncio){
startActivity(new Intent(getApplicationContext(),AgregarUbicacionYproductoInmueble.class));
    }

    if(v.getId() == R.id.btnVerAnuncios){
    startActivity(new Intent(getApplicationContext(),AnunciosActivity.class));
            Toast.makeText(getApplicationContext(),"Pero llega",Toast.LENGTH_LONG).show();  // inclusive llega hasta aqui cuando quito la linea de abajo
// cuando quito esta linea funciona pero obviamente no abre la otra activity si no que muestra el mensaje de la anterior linea
        }
    }

Remove several lines that you had and simplify it to one, I hope it works for you. Good luck and greetings.

    
answered by 26.10.2018 в 07:10
0

It is incorrect to use this as context within the method:

@Override
public void onClick(View v) {
 ...
 ...
 ...
}

You must use as a context the call to the getApplicationContext() method:

Intent AbrirAnuncios = new Intent(getApplicationContext(),AnunciosActivity.class);

Or also refer to the Activity, assuming that your Activity is called MainActivity :

Intent AbrirAnuncios = new Intent(MainActivity.this,AnunciosActivity.class);

Sometimes we add Activity to our projects but they are not automatically registered in AndroidManifest.xml , add the activity within your AndroidManifest.xml to be registered in your application.

 <application
     ...
      ...
      <activity android:name=".AnunciosActivity"/>
  </application>
    
answered by 26.10.2018 в 06:06