Calling new Activity via an Imageview

Asked

Viewed 1,855 times

0

I need to call a new Activity through the click in a ImageView.

Follows my code:

final ImageView botaoAbrirMesas = (ImageView) findViewById(R.botoes_laucher.openMesas);

    botaoAbrirMesas.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent it = new Intent();
            it.setClass(null, PrincipalActivity.class);

            startActivity(it);
        }
    });

However, when running the application terminates due to an error. This is the right way to use or I cannot use click in a ImageView?

1 answer

2

I think the problem is that you pass null in the method setClass.

Replace your code with this:

Intent intent = new Intent(Main.this, PrincipalActivity.class);
startActivity(intent);  

Substitute Main.this for NomeDaSuaActividade.this

If you want to use the method setClass substitute null for NomeDaSuaActividade.this

  • In addition to doing what @ramaral posted, be sure to declare the new Activity on AndroidManifest.xml <Activity android:name="com.package.app. Nameserve" android:label="Nameserve" > </Activity>

  • Thank you for remembering that. But the activity to declare is PrincipalActivity and not NomeDaSuaActividade. At least in the context where NomeDaSuaActividadeis used in my example.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.