0

I've read how to create a simple app that launches a website in the phone's external browser in this post. I followed those steps, yet my simple App keeps crashing.

I'm trying to create the absolute simplest possible app with the highest compatibility which simply opens a URL in the external browser of the phone.

These were my exact steps:

  1. Installed Eclipse ADT with the Android SDK from Android's official download site with all the SDK packages listed here.
  2. Created a new "Android Application Project".
  3. Set the Minimum Required SDK to API 7: Android 2.1 (Eclair) (that's the lowest API version that allowed the creation of an Activity)
  4. Left the Target SDK and Compile With to the default API 21: Android $.X (L Preview)
  5. Set the Theme to None.
  6. Left everything as default in the "Configure Project" step.
  7. Left everything as default in the "Configure the attributes of the icon set" step.
  8. Under "Create Activity", I selected "Empty Activity".
  9. Left everything as default in the "Creates a new empty activity" step.
  10. Clicked "Finish"
  11. In MainActivity.java,

I replaced:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

With:

protected void onCreate(Bundle savedInstanceState) {
        String url = "http://www.YOUR-URL.com";
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setData(Uri.parse(url));
        startActivity(i);
    }
  1. I added the two lines import android.content.Intent; and import android.net.Uri; above public class MainActivity extends Activity {

.

  1. Saved the file, ran it, and then installed the MyFirstApp.apk (found in /workspace/MyFirstApp/bin/) on my phone

  2. Launched the App on my phone, it asked which browser to use, and I selected "Chrome" and "Always".

Now, every time I open the App on my phone, it will launch the URL in Chrome, but as it's launching Chrome, I get an error saying "Unfortunately, My First App has stopped."

I've tried to create the simplest App possible, and yet it crashes. The above are the exact steps I took. Where did I go wrong?

Community
  • 1
  • 1
ProgrammerGirl
  • 3,157
  • 7
  • 45
  • 82

1 Answers1

1

Please add this line in your androidManifest.xml

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

For more information you can see the following link

Also u forget to call the super onCreate() method

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    String url = "http://www.YOUR-URL.com";
    Intent i = new Intent(Intent.ACTION_VIEW);
    i.setData(Uri.parse(url));
    startActivity(i);
}
JpCrow
  • 4,881
  • 4
  • 32
  • 46
  • I just did that, but got the same error. I added that line to AndroidManifest.xml, saved the file, Ran it in Eclipse so it would update the .apk file, I then uninstalled the original App on my phone, installed the new version of the App on my phone, this time it mentioned during the installation that the app would have "full network access", I then completed the installation, opened the App, but got the same error. Any other ideas? – ProgrammerGirl Nov 29 '14 at 17:34
  • can u post your logcat so we can figure out what it is? – JpCrow Nov 29 '14 at 17:46
  • I'm trying to run logcat in Eclipse, but when I try to run the App inside the Emulator, it gives me an error saying "Installation error: Unknown failure". This is the logcat output: http://i.imgur.com/zSLh7dF.png Any ideas? – ProgrammerGirl Nov 29 '14 at 17:52
  • Please see my edit, i recreate your app and now is not crashing – JpCrow Nov 29 '14 at 17:57
  • OK, this is strange. When I added the `super.onCreate()` method, I stopped getting the error and it correctly opened the URL in Chrome. However, I then went back to the Home screen, launched the App again, and now get a blank white screen. The screen will stay fully white until I either click the "Home" button or rotate my phone to landscape mode, at which point it opens the URL in Chrome. Very strange. Any ideas? – ProgrammerGirl Nov 29 '14 at 18:07
  • You should add the onResume() method and there put your code for what the app should do on back – JpCrow Nov 29 '14 at 18:11
  • But the App shouldn't do anything other than simply launch the URL in the external browser. It doesn't need to resume or anything. Is there a way for the App to close after it launches the URL? What do you suggest? – ProgrammerGirl Nov 29 '14 at 18:14
  • Yes you can finalize the activity after the intent is launched. – JpCrow Nov 29 '14 at 18:16
  • Great, how can I finalize it after it launches the URL in the external browser? – ProgrammerGirl Nov 29 '14 at 18:18
  • I found it! This worked: http://stackoverflow.com/a/2093901/869849 I just added that line after `startActivity(i);` and it no longer opens a blank screen when opening the App a 2nd time. I just opened the App several times in a row and it opened the URL in Chrome correctly every time. Thanks for all your help! – ProgrammerGirl Nov 29 '14 at 18:24