Sarath
Sarath

Reputation: 21

exceptions in program

I tried to execute the following piece of code.If i enter the site names with http [for eg:http://www.google.com] am getting correct output.Otherwise I am getting force close. Even I am catching the activitynotfoundexception then also I am getting ActivityNotFoundException.

Help me.

try {
    Button browse=(Button)findViewById(R.id.Browse);
    browseURl=(EditText)findViewById(R.id.BrowseUrl);
    browse.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent invokeURI=new Intent(Intent.ACTION_VIEW,Uri.parse(browseURl.getText().toString()));
            startActivity(invokeURI);
        }
    });
} catch (ActivityNotFoundException ex) {
    // TODO: handle exception

    Log.e("BrowseURI","Failed Browsing the given URI",ex);
}

Upvotes: 0

Views: 113

Answers (1)

Scottie
Scottie

Reputation: 545

You need to catch the exception within your onClick event, because you're getting the exception after you've clicked, not when you're hooking the event handler.

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        Intent invokeURI=new Intent(Intent.ACTION_VIEW,Uri.parse(browseURl.getText().toString()));
        try {
            startActivity(invokeURI);
        }
        catch (ActivityNotFoundException ex) {
            Log.e("BrowseURI","Failed Browsing the given URI",ex);
        }
    }

Upvotes: 2

Related Questions