• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Calling an Activity from a customDialog.

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:

Code:
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);
                [COLOR=Red]startActivity(myIntent);[/COLOR]
                
                
            }
        });      
...
}
Then the red squiggly line appears on startActivity(myIntent). :confused:
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.
 
Try: context.startActivity(myIntent);
You probably need to define the Context variable final also since its beeing referenced in an inner class(View.OnClickListener)
 
The problem is that, as the error says, startActivity() always refers to "this", which in normal use-cases is an Activity (which again is a sub-class of Context). But in a case like this "this" is the View.onClickListener, which is neither an Activity or sub of Context, and therefore has no startActivity method.

To fix it you can do the same thing you do when creating the Intent - view.getContext.
This returns the Context of the clicked View, in this case the Context in which the button is placed!

So, replace your line with:

Code:
view.getContext().startActivity(myIntent);

And you should be good to go.
 
Back
Top Bottom