Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build a Simple Android App (retired 2014) Pretty Little Things Customizing Buttons and Labels

Brett Klugman
Brett Klugman
2,147 Points

Prblem running the app in the emulator

I have followed all of the instructions in the videos correctly, but when I try to run the crystal ball app it crashes and won't run. This started happening after I added the crystal ball image and I'm not sure what the problem is. Any advice on how to fix it?

12 Answers

You have an issue in one of your xml files. You should first try expanding all of the folders underneath the /res directory within your application. Try to keep an eye out for any red X to the left of a file. If you see where it tell you in the log "You need to use a Theme.AppCompat.theme" with this activity.

When reading stack exceptions what you want to try to do is focus at the start of it. That will be where your first known clue it gives you is. Because the log fail has a head at the top and a tail at the end.

So what you're going to want to do is first look at the line at the top where your stack trace strarted. FATAL EXCEPTION. This tells you what package it happeend under. Great but that doesn't really tell us anything does it. Especially since this application only has one module and it gave you the top level parent package name.

So let's look at line #2. Its not formatted with proper line breaks so find where the date and time stamp is next in the paragraph. Now we are getting deeper. The fatal exception was caused by a Runtime exception. Okay and this one tells us a little bit more. It looks like when we tried to run the MainActivity.java file in the crystalball package it caused a 'Runtime Exception'. Then notice how included on this line is an IllegalStateException and it has a very very very helpful message for you. Once we get passed this line everything is an effect caused by what we already know. The mainActivity failed to run.

Notice how you can connect the dots and find out information about why something that happened on line x can be answered usually by on line x+1 (usually) and what caused line x + 1 can be answered by looking at line x + 2. It is very important not to just skip to the end. You have to walk through the stack trace methodically in order to debug an error in the logs.

I've got to go eat dinner or I would look at it some more for you. I urge you to spend time with that. Regardless of how many lessons you complete tonight if you spend the time to debug that on your own and go through and fix it through trial and error you will be gain more knowledge than watching a video.

P.S. - Highlight and copy that last line with the message and go over to google. Look that up. Whenever you have an error message that gives you so much detail like that. it means that it was logged by developers. Meaning that they most likely know what causes it. Hence the abundant amount of documentation. I would walk you through but meatloaf does sound good right now. Good luck to you! Plus I can't see your source.

KUNAL HAJRA
KUNAL HAJRA
5,288 Points

You can look into the Logcat for the exact error which is causing the App to crash.Also please paste the error code from the logcat which will help us resolve the issue

Brett Klugman
Brett Klugman
2,147 Points

05-22 00:49:27.014: E/AndroidRuntime(1106): FATAL EXCEPTION: main 05-22 00:49:27.014: E/AndroidRuntime(1106): Process: com.example.crystalball, PID: 1106 05-22 00:49:27.014: E/AndroidRuntime(1106): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.crystalball/com.example.crystalball.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

05-22 00:49:27.014: E/AndroidRuntime(1106): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)

05-22 00:49:27.014: E/AndroidRuntime(1106): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)

05-22 00:49:27.014: E/AndroidRuntime(1106): at android.app.ActivityThread.access$800(ActivityThread.java:135) 05-22 00:49:27.014: E/AndroidRuntime(1106): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 05-22 00:49:27.014: E/AndroidRuntime(1106): at android.os.Handler.dispatchMessage(Handler.java:102) 05-22 00:49:27.014: E/AndroidRuntime(1106): at android.os.Looper.loop(Looper.java:136)

here are the first error messages that it gives me, I can give you the rest if that would be helpful

KUNAL HAJRA
KUNAL HAJRA
5,288 Points

Great insight by Chris on how to debug the issue. Basically the problem is with the Theme that the MainActivity expects. Please check in your AndroidManifest.xml for the following line android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" >

And also look for any xml related errors under the res directory and let us know how did it go.

Brett Klugman
Brett Klugman
2,147 Points

I was having trouble fixing the problem by just following the error messages, so I tried googling the error lines like christopher suggested and found this thread: https://teamtreehouse.com/forum/you-need-to-use-a-themeappcompat-theme-or-descendent-crystal-ball-crashes I followed all of the advice there, and my app still crashes., and then I found a suggestion that I needed to use Theme.AppCompat, but I'm not sure what this means. Any thoughts about what could still be wrong? I can post my code if that will help

KUNAL HAJRA
KUNAL HAJRA
5,288 Points

Yes!! Please paste the code and let me see what caused the error

Brett Klugman
Brett Klugman
2,147 Points

Here is my activity_main.xml:

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:gravity="top"
     android:background="@android:color/black"
     tools:context=".MainActivity" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitCenter"
        android:src="@drawable/ball01" />

    <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:layout_centerVertical="true"
            android:shadowColor="@android:color/white"
            android:shadowRadius="10"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#3f0f7f"
            android:textSize="32sp" />

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="Enlighten me"
        android:textColor="#3f0f7f"
        android:textSize="24sp"
        android:textStyle="bold|italic"
        android:typeface="serif" />

</RelativeLayout>

Here is my MainActivity.java:

package com.example.crystalball;

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

public class MainActivity extends ActionBarActivity {


    private crystalBall mCrystalBall = new crystalBall(); 
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Declare our view variable:
        final TextView answerLabel = (TextView) findViewById(R.id.textView1);
        Button getAnswerButton = (Button) findViewById(R.id.button1);
        getAnswerButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                String answer = mCrystalBall.getAnAnswer();
                //update the label with your dynamic variable:

                answerLabel.setText(answer);

            }
        });
    }


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


}

Here is my crystalBall.java:

package com.example.crystalball;

import java.util.Random;

public class crystalBall {
    public String[] manswers= {
            "It is certain!",
            "It is decidedly so",
            "All signs point to YES",
            "The stars are not aligned",
            "My reply is no",
            "It is doubtful",
            "Better not tell you now",
            "Concentrate and ask again",
            "unable to answer now"};

    public String getAnAnswer(){

        String answer = "";

        //randomly select one answer:
        Random randomGenerator = new Random();
        int randomNumber = randomGenerator.nextInt(manswers.length);

        answer = manswers[randomNumber];

        return answer;

    }

}

Here is my AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.crystalball"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" >
        <activity
            android:name="com.example.crystalball.MainActivity"
            android:label="@string/app_name" android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

Here is my styles.xml:

<resources>

    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="AppBaseTheme" parent="Theme.AppCompat.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

    <!-- Application theme. -->
    <style name="AppTheme" parent="AppBaseTheme">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
         <item name="android:windowNoTitle">true</item> <!-- Hides the Action Bar -->
         <item name="android:windowFullscreen">true</item> <!-- Hides the status bar -->
    </style>

</resources>

thank you for all of your help

KUNAL HAJRA
KUNAL HAJRA
5,288 Points

I noticed that you are trying to load the AppCompat style. Please change the following in your code

  1. In the styles.xml replace the line <style name="AppBaseTheme" parent="Theme.AppCompat.Light">

with <style name="AppBaseTheme" parent="android:Theme.Light">

Also check if your app has any more styles.xml and replace the line in all of them

  1. In the activity_main.xml delete the following line

android:textAppearance="?android:attr/textAppearanceLarge"

Save the changes and build and run the app. It should work now.

Brett Klugman
Brett Klugman
2,147 Points

My app still isn't working, but I couldn't see the line that you wanted me to replace in the style.xml

Brett Klugman
Brett Klugman
2,147 Points

in my MainActivity.java, I changed public class MainActivity extends ActionBarActivity to public class MainActivity extends Activity and this worked. Thank you for all of your help.

KUNAL HAJRA
KUNAL HAJRA
5,288 Points

Good to know that .Thanks

Can we close this question? I think it has been answered because the original poster has acknowledged that the solution provided was able to allow him to read a stack trace and use it figure out what line it occurred on at the start of the problem and the end of the problem when the application crashed.