-1

I tried to open a webpage on clicking a button. But, Its not working. This is my code:

ImageButton fbButton = (ImageButton) findViewById(R.id.fb);
    fbButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

                Intent viewIntent = new Intent("Intent.ACTION_VIEW", Uri.parse("http://www.facebook.com"));
                startActivity(viewIntent);
        }
    });

Its always going to catch statement.

And Here it is Manifest file permission code:

   <uses-permission android:name="android.permission.INTERNET"/>
   <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Error Log:

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.krish.me, PID: 4532
              android.content.ActivityNotFoundException: No Activity found to handle Intent { act=Intent.ACTION_VIEW dat=http://www.facebook.com }

Thank you in Advance :)

Member_9
  • 64
  • 1
  • 11

3 Answers3

3
Intent viewIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.facebook.com"));

Use Intent.ACTION_VIEW instead "android.intent.action.view" .

goodev
  • 624
  • 1
  • 5
  • 10
1

If you want to open an URL with Intent just use Intent.ACTION_VIEW in place of android.intent.action.view

Intent viewIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com"));

Your code will become like this -

ImageButton fbButton = (ImageButton) findViewById(R.id.fb);
    fbButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            try {
                Intent viewIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/"));
                startActivity(viewIntent);
            }
            catch (Exception e) {
                Toast.makeText(getApplicationContext(), "Can't connect to Internet",
                        Toast.LENGTH_SHORT).show();
            }
        }
    });

Make sure you has Internet Permission added in the AndroidManifest file.

<uses-permission android:name="android.permission.INTERNET" /> 

The problem is i think you dont have any app that can open URLs (i.e. browsers) installed in your phone.

Sachin Rajput
  • 4,326
  • 2
  • 18
  • 29
1

Try this,

 try {
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.facebook.com"));
        startActivity(Intent.createChooser(intent, "Choose browser"));
    } catch (Exception e) {
        Toast.makeText(getApplicationContext(), "Can't connect to Internet",
                Toast.LENGTH_SHORT).show();
    }
Bhavya Gandhi
  • 507
  • 3
  • 10