Hello World App won't run in emulator

Started by
6 comments, last by Navyman 9 years, 8 months ago

Hi, I'm trying to learn how to make my first "Hello World" application for Android, in Java with Eclipse. But when I try to run it in the Emulator, all I get is the "Unfortunately, Hello World has stopped" message.

Here is my code:


package com.example.testgame;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;


public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView tv = new TextView(this);
        tv.setText("Yo Android World!");
        setContentView(tv);
    }


    @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;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

All I did was adding "import android.widget.TextView;", "TextView tv = new TextView(this);", "tv.setText("Yo Android World!");" and "setContentView(tv);" to the default New Project code, following THIS Tutorial.

Also, if you could point me to a more up-to-date Tutorial series for beginners, it would really help. :-)

Thank you in advance. :-)

Advertisement

The code works without those lines? It's working for me with and without them.

If the app crashed there should be some exception displayed in LogCat, what does it say? If you can't find the LogCat window go to window -> show view -> other and search for LogCat.

Okay, here is what the LogCat gives me:


07-30 20:56:19.420: E/AndroidRuntime(871): FATAL EXCEPTION: main
07-30 20:56:19.420: E/AndroidRuntime(871): Process: com.example.testgame, PID: 871
07-30 20:56:19.420: E/AndroidRuntime(871): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testgame/com.example.testgame.MainActivity}: android.util.AndroidRuntimeException: You cannot combine swipe dismissal and the action bar.
07-30 20:56:19.420: E/AndroidRuntime(871): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2197)
07-30 20:56:19.420: E/AndroidRuntime(871): 	at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2258)
07-30 20:56:19.420: E/AndroidRuntime(871): 	at android.app.ActivityThread.access$800(ActivityThread.java:138)
07-30 20:56:19.420: E/AndroidRuntime(871): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1209)
07-30 20:56:19.420: E/AndroidRuntime(871): 	at android.os.Handler.dispatchMessage(Handler.java:102)
07-30 20:56:19.420: E/AndroidRuntime(871): 	at android.os.Looper.loop(Looper.java:136)
07-30 20:56:19.420: E/AndroidRuntime(871): 	at android.app.ActivityThread.main(ActivityThread.java:5026)
07-30 20:56:19.420: E/AndroidRuntime(871): 	at java.lang.reflect.Method.invokeNative(Native Method)
07-30 20:56:19.420: E/AndroidRuntime(871): 	at java.lang.reflect.Method.invoke(Method.java:515)
07-30 20:56:19.420: E/AndroidRuntime(871): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
07-30 20:56:19.420: E/AndroidRuntime(871): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
07-30 20:56:19.420: E/AndroidRuntime(871): 	at dalvik.system.NativeStart.main(Native Method)
07-30 20:56:19.420: E/AndroidRuntime(871): Caused by: android.util.AndroidRuntimeException: You cannot combine swipe dismissal and the action bar.
07-30 20:56:19.420: E/AndroidRuntime(871): 	at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:275)
07-30 20:56:19.420: E/AndroidRuntime(871): 	at com.android.internal.policy.impl.PhoneWindow.generateLayout(PhoneWindow.java:2872)
07-30 20:56:19.420: E/AndroidRuntime(871): 	at com.android.internal.policy.impl.PhoneWindow.installDecor(PhoneWindow.java:3129)
07-30 20:56:19.420: E/AndroidRuntime(871): 	at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:303)
07-30 20:56:19.420: E/AndroidRuntime(871): 	at android.app.Activity.setContentView(Activity.java:1930)
07-30 20:56:19.420: E/AndroidRuntime(871): 	at android.support.v7.app.ActionBarActivity.superSetContentView(ActionBarActivity.java:217)
07-30 20:56:19.420: E/AndroidRuntime(871): 	at android.support.v7.app.ActionBarActivityDelegateICS.setContentView(ActionBarActivityDelegateICS.java:110)
07-30 20:56:19.420: E/AndroidRuntime(871): 	at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:77)
07-30 20:56:19.420: E/AndroidRuntime(871): 	at com.example.testgame.MainActivity.onCreate(MainActivity.java:15)
07-30 20:56:19.420: E/AndroidRuntime(871): 	at android.app.Activity.performCreate(Activity.java:5242)
07-30 20:56:19.420: E/AndroidRuntime(871): 	at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-30 20:56:19.420: E/AndroidRuntime(871): 	at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2161)
07-30 20:56:19.420: E/AndroidRuntime(871): 	... 11 more

I'm not an Android user, but I'd recommend Googling the message in the root exception: You cannot combine swipe dismissal and the action bar

Have you tested on an actual device? The android emulator sometimes has problems.

Stay gold, Pony Boy.

Making sure that your emulator is setup correctly can be an issue.

Developer with a bit of Kickstarter and business experience.

YouTube Channel: Hostile Viking Studio
Twitter: @Precursors_Dawn

OK, I googled it (didn't know exactly what to google at the time) and found the solution.

I downgraded the API Level to 17 just to be sure (Android version 4.2.2) and it works like a charm, both in the emulator and on my physical phone. :-)

Thanks for the help. :-)

A question though: Do I even need the API Level 20 stuff? Or is it safe to just uninstall them and just work with Level 17? Because the whole thing takes a billion trillion (rough estimate) Gigabytes on my HDD and it's a bit annoying since it's almost full... :-/

I use level 17, because it is a fairly tested base. Level 20 is more on the bleeding edge and I would say if you do not know the new features than it is better to use a base you are most familiar with.

Developer with a bit of Kickstarter and business experience.

YouTube Channel: Hostile Viking Studio
Twitter: @Precursors_Dawn

This topic is closed to new replies.

Advertisement