1

I created a pop up to confirm a package and when the user selects the "confirm" button it should take the user to another activity but it is not working properly.

Here is my code:

switch (v.getId()) {
        case R.id.button_solicitar:
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setCancelable(true);
            builder.setTitle("Confirmación");
            builder.setMessage("¿Deseas solicitar " + sum + " toallas con un total de $" + tot1 + " pesos?");
            builder.setPositiveButton("Confirmar",
                    new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Toast.makeText(Paquetes.this, "¡Tu pedido ha sido solicitado!", Toast.LENGTH_LONG).show();
                            startActivity(new Intent(this, Casa.class));
                        }
                    });
            builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                }
            });
            AlertDialog dialog = builder.create();
            dialog.show();
            break;
Konrad Morawski
  • 8,307
  • 7
  • 53
  • 91
AldoT
  • 89
  • 1
  • 1
  • 4

4 Answers4

0

Try change

startActivity(new Intent(this, Casa.class));

to

startActivity(new Intent(YourActivity.this, Casa.class)); //YourActivity is the class name of your current Activity
Linh
  • 57,942
  • 23
  • 262
  • 279
  • @AldoT can you explain more about `didn't work`. your app crash or your can not run the app – Linh Apr 22 '16 at 02:10
  • When I do that it does not allow me to run the app, it shows an error that says "Cannot resolve constructor 'Intent(java.lang.Class,java.lang.Class)'" – AldoT Apr 22 '16 at 02:13
  • does the toast pops up? aldot? – incr3dible noob Apr 22 '16 at 02:22
  • incr3dible, the toast appears after the user clicks on confirm – AldoT Apr 22 '16 at 02:24
  • @AldoT you say `it does not allow me to run the app. "Cannot resolve constructor 'Intent(java.lang.Class,java.lang.Class)'"`, why the Toast can show :( – Linh Apr 22 '16 at 02:26
  • @AldoT both `YourActivity.this` or `getApplicationContext` will fix your problem. However, you should remember that `YourActivity.this refers to activity context and getApplicationContext () refers to the application context.` – Linh Apr 22 '16 at 06:59
  • @PhanVănLinh ok, I'll remember that. Thanks! – AldoT Apr 22 '16 at 17:53
0

do like this

startActivity(new Intent(getApplicationContext(), Casa.class));
Rashid
  • 1,700
  • 1
  • 23
  • 56
  • 1
    why we need `getActivity()` in this case? – Linh Apr 22 '16 at 02:15
  • you should pass your activity context as a first parameter in your intent as stated here http://stackoverflow.com/a/20241921/5870896 – Rashid Apr 22 '16 at 02:19
  • in the link you share to me, it is start new activity from `Fragment` not from `Activity` – Linh Apr 22 '16 at 02:20
  • OMG! Now it works!! Thanks a lot to Phan, Rashid and incr3dible. I used the "getApplicationContext()". – AldoT Apr 22 '16 at 02:27
0

Try ,

startActivity(new Intent(Paquetes.this, Casa.class));    // assuming your current activity is Paquetes

if it doesn't work can you post your error log once?

incr3dible noob
  • 441
  • 2
  • 14
0

Please run your application in debug mode.Check if it is stopping at startActivity(new Intent(this, Casa.class)).If not please post your logcat.

One more thing Please check your manifest:

Have you declared your Activity Casa.class in manifest file:

<activity android:name=".Casa" >

If nothing is working out I request you to post your logcat.

Lips_coder
  • 686
  • 1
  • 5
  • 17