Question

I guess this is just a simple question (I’m such a noob…) I have this custom dialog box that has 3 buttons in it.

Now I want to call an activity from one of the buttons so I tried this:

public class picturedialog extends Dialog implements OnClickListener {
    Button Camera;

    public picturedialog (Context context){
        super (context);
        setContentView(R.layout.picturedialog);

        Camera = (Button) this.findViewById(R.id.pdButton1);

        Camera.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                dismiss();

                Intent myIntent = new Intent(view.getContext(), CameraActivity.class);
                startActivity(myIntent);

            }
        });
...
}

Then the red squiggly line appears on startActivity(myIntent). Upon hovering on it, eclipse tells me this: “The method startActivity(Intent) is undefined for the type new View.OnClickListener(){}” Ehhh? Please orient me on how to do this properly. Any help would be appreciated.

Was it helpful?

Solution

I am sure you are overcomplicating with subclassing from Dialog. Try to follow to the dialog tutorial - https://developer.android.com/guide/topics/ui/dialogs.html#ShowingADialog

Note that dialogs are created on the fly (in Activity.onCreateDialog()) without the need to have your own custom dialog classes. Since you set a listener being inside of the wrapping activity (if you follow the tutorial), then you are able to call startActivity(myIntent), because all the fields/methods of a wrapping class instance are available for an instance of an inner class.

OTHER TIPS

Suppose the name of your Activity is A, then you just do:

   A.this.startActivity(myIntent);

The problem arises because "this" inside your inner class refers to the object of that inner class, when what you want is the object of the enclosing Activity. A.this will refer to that.

If you aren't enclosing this class in an Activity, then try calling the startActivity from method using the context that you passed into the method, e.g. context.startActivty(myIntent). The startActivity method belongs to the Context class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top