Open url from Dialog class


Ask by : user1390816 November 04, 2012 12:33

i try to open an url form a dialog class. I tried it with following code, but I get a NullPointerException. How can I do this?

I start the Dialog with following from my Activity:

AboutDialog about = new AboutDialog(this);
about.setTitle(R.string.Text_about);
about.show();

This is the Dialog class:

public class AboutDialog extends Dialog implements OnClickListener{
private Button button_ok;
.
.
.

}

public void onClick(View arg0) {
            myActivity activity = new myActivity();
            String url = "http://www.test.com";
            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setData(Uri.parse(url));
            activity.startActivity(i); 
}
View original question

Answer by : JanuszNovember 04, 2012 12:33

You are creating a new Activity in the onCick method. Activities should not be created manually. They are set up from the system. I would guess that the Activity is not set up properly and this results in the NullPointerException.

You should get a valid context through getContext. Use

getContext().startActivity(i); 

instead of

activity.startActivity(i); 
View original answer