12

I'm trying to do as simple thing as opening web page from my app. I think code should look something looke below, but I keep getting error "no activity found" while executing code:

    public class MainActivity extends Activity {
    public static final String MY_STRING =  "com..example.MY_STRING";
    Button button1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(clickListener);

    }    
    public OnClickListener clickListener = new  OnClickListener(){          
        @Override
        public void onClick(View arg0) {

            Intent launchBrowser = new Intent(Intent.ACTION_VIEW, Uri.parse("www.example.com"));                
            startActivity(launchBrowser);               
        }           
    };      
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

here is log:

D/libEGL(11574): loaded /system/lib/egl/libGLES_android.so
D/libEGL(11574): loaded /vendor/lib/egl/libEGL_mtk.so
D/libEGL(11574): loaded /vendor/lib/egl/libGLESv1_CM_mtk.so
D/libEGL(11574): loaded /vendor/lib/egl/libGLESv2_mtk.so
D/OpenGLRenderer(11574): Enabling debug mode 0
V/Provider/Setting(11574): invalidate [system]: current 109 != cached 0
D/AndroidRuntime(11574): Shutting down VM
W/dalvikvm(11574): threadid=1: thread exiting with uncaught exception (group=0x40ca8258)
E/AndroidRuntime(11574): FATAL EXCEPTION: main
E/AndroidRuntime(11574): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=www.example.com }
E/AndroidRuntime(11574):    at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1518)
E/AndroidRuntime(11574):    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1390)
E/AndroidRuntime(11574):    at android.app.Activity.startActivityForResult(Activity.java:3204)
E/AndroidRuntime(11574):    at android.app.Activity.startActivity(Activity.java:3311)
E/AndroidRuntime(11574):    at com.example.openwebpage.MainActivity$1.onClick(MainActivity.java:40)
E/AndroidRuntime(11574):    at android.view.View.performClick(View.java:3517)
E/AndroidRuntime(11574):    at android.view.View$PerformClick.run(View.java:14155)
E/AndroidRuntime(11574):    at android.os.Handler.handleCallback(Handler.java:605)
E/AndroidRuntime(11574):    at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(11574):    at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(11574):    at android.app.ActivityThread.main(ActivityThread.java:4503)
E/AndroidRuntime(11574):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(11574):    at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(11574):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
E/AndroidRuntime(11574):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
E/AndroidRuntime(11574):    at dalvik.system.NativeStart.main(Native Method)

and here is my manifest:

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.openwebpage.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

user696847
  • 469
  • 2
  • 7
  • 16

4 Answers4

25

Just guessing, but you might be missing the http:// prefix. Try

Intent launchBrowser = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.example.com"));
ssantos
  • 16,001
  • 7
  • 50
  • 70
  • 1
    Mmh weird. Looking for similar posts, this one suggest that url scheme is actually the problem. http://stackoverflow.com/questions/2201917/how-can-i-open-a-url-in-androids-web-browser-from-my-application Any chance that the last change wasn't compiled? Also, have you tried something like http://www.google.com? – ssantos Sep 26 '13 at 21:47
  • I did try with google.com, but my app doesn't even starts the browser. It crashes on click event (launching a browser). So it is not case that it cannot resolve url. – user696847 Sep 26 '13 at 21:57
  • Also, make sure that "http://" or "https://" is all lower case and not "Http://" or "Https://", etc – Patrick Apr 27 '16 at 01:25
2

The correct method to launch a website through the browser is

Intent viewIntent = new Intent("android.intent.action.VIEW", Uri.parse("http://www.google.com"));
startActivity(viewIntent);
rcbevans
  • 7,101
  • 4
  • 30
  • 46
1

For some reason now it is working with those added "http://"

After I reboot device, and ran it again with added "http://" it worked out.

gunr2171
  • 16,104
  • 25
  • 61
  • 88
user696847
  • 469
  • 2
  • 7
  • 16
1

sometimes Upper Case cause that problem. try to make URL lower case.

aidinism
  • 178
  • 1
  • 5