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 Adding an Image

Ben Junya
Ben Junya
12,365 Points

"You need to use a Theme.AppCompat theme (or descendent) Crystal Ball Crashes

Hi all,

Here's a problem I'm currently encountering in this tutorial:

Right now, the layout will display without error in Eclipse, but when I'm trying to run the application itself, it crashes on the emulator. I've attached a copy of the error data from the console log.

Here's what I THINK is happening:

In the error log, there's this: You need to use a Theme.AppCompat theme (or descendant) with this activity.

I went back into the CrystalBall Manifest > Application tab

Then clicked on Theme > Browse - there's 2 radial buttons - Project Resources and System Resources

Any theme from "Project Resources" works fine and the app doesn't crash

The theme used in the video, "Theme.black.NoTitleBar.Fullscreen" is in the System resources. I think this is why the application is crashing upon running.

Is there a fix around this? The themes in Project Resources all make the Crystal Ball app look terrible!

Even if I use the AppCompat themes in the project resources, the app runs, but when I try to move textview1 into the center of the image, the app crashes as well. Again, this doesn't show as an error in Eclipse, but it does cause the application to crash.

Any workarounds found?

Crash log: 04-06 23:25:43.510: E/AndroidRuntime(975): FATAL EXCEPTION: main 04-06 23:25:43.510: E/AndroidRuntime(975): Process: com.example.crystalball, PID: 975 04-06 23:25:43.510: E/AndroidRuntime(975): 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. 04-06 23:25:43.510: E/AndroidRuntime(975): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 04-06 23:25:43.510: E/AndroidRuntime(975): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 04-06 23:25:43.510: E/AndroidRuntime(975): at android.app.ActivityThread.access$800(ActivityThread.java:135) 04-06 23:25:43.510: E/AndroidRuntime(975): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 04-06 23:25:43.510: E/AndroidRuntime(975): at android.os.Handler.dispatchMessage(Handler.java:102) 04-06 23:25:43.510: E/AndroidRuntime(975): at android.os.Looper.loop(Looper.java:136) 04-06 23:25:43.510: E/AndroidRuntime(975): at android.app.ActivityThread.main(ActivityThread.java:5017) 04-06 23:25:43.510: E/AndroidRuntime(975): at java.lang.reflect.Method.invokeNative(Native Method) 04-06 23:25:43.510: E/AndroidRuntime(975): at java.lang.reflect.Method.invoke(Method.java:515) 04-06 23:25:43.510: E/AndroidRuntime(975): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 04-06 23:25:43.510: E/AndroidRuntime(975): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 04-06 23:25:43.510: E/AndroidRuntime(975): at dalvik.system.NativeStart.main(Native Method) 04-06 23:25:43.510: E/AndroidRuntime(975): Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. 04-06 23:25:43.510: E/AndroidRuntime(975): at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:111) 04-06 23:25:43.510: E/AndroidRuntime(975): at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:58) 04-06 23:25:43.510: E/AndroidRuntime(975): at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:98) 04-06 23:25:43.510: E/AndroidRuntime(975): at com.example.crystalball.MainActivity.onCreate(MainActivity.java:16) 04-06 23:25:43.510: E/AndroidRuntime(975): at android.app.Activity.performCreate(Activity.java:5231) 04-06 23:25:43.510: E/AndroidRuntime(975): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 04-06 23:25:43.510: E/AndroidRuntime(975): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 04-06 23:25:43.510: E/AndroidRuntime(975): ... 11 more

Robert Shaw
Robert Shaw
1,567 Points

Ben Jakuben Ben, I am having the same issue that many here have indicated. After Run is initiated returns "Unfortunately, Crystal Ball has stopped." Any help would be greatly appreciated.

Here is my activity_main

<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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.crystalball.MainActivity$PlaceholderFragment" >

<ImageView
    android:id="@+id/imageView2"
    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:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:gravity="center_horizontal"
    android:shadowColor="@android:color/white"
    android:shadowRadius="10"
    android:textColor="#3f0f7f"
    android:textSize="32sp" />

<Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/imageView2"
    android:layout_alignLeft="@+id/imageView2"
    android:text="Enlighten Me!"
    android:textColorLink="#3f0f7f"
    android:textSize="24sp"
    android:textStyle="bold|italic"
    android:typeface="serif" />

</RelativeLayout>

Here is MainActivity:

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();

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

    //Declare our View variables and assign them the Views from the layout file
    final TextView answerLabel = (TextView) findViewById(R.id.textView1);
    Button getAnswerButton = (Button) findViewById(R.id.button1);

    getAnswerButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            String answer = mCrystalBall.getAnAnswer();

            //Update the label with our dynamic answer
            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);
}

}

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Robert Shaw, are you getting the same error as in the title of this post? If so, check out the instructions I added to the Teacher's Notes. If you need more help, post a new question in the Forum and tag me (this one is getting hard to follow).

4 Answers

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

The Android tools were updated recently and have caused some frustrating differences like this. I've recorded a fix that will be available shortly. I'm sorry it's giving you trouble. Once you get past this the rest of the course still applies with minimal or no differences.

Anyhow, in your Package Explorer, do you have another project called appcompat_v7? This now gets created when you create a new project. And you need it there to use Theme.AppCompat.

Ben Junya
Ben Junya
12,365 Points

Hi Ben,

I really appreciate you taking the time to help me and answer my questions!

I do have an appcompat_v7 in my package explorer, however, I'm still getting the application to crash after changing the theme.

Understand that I'm a total newbie and have little to no programming experience. Please let me know what I need to do with appcompat_v7.

Ben

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Oh this is unfortunate. This is because of the updates but I haven't seen this issue yet. There are a few ways to get around this, but I haven't decided yet which is best. I'll need to address this with another video fix.

In the mean time, let's do this. Change your project back to using "AppTheme" like it was before. Then we're going to customize the AppTheme itself:

Add the following two lines to the file res/values/styles.xml inside the <style name="AppTheme"... tag:

<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>

Here's what the whole file should look like (you can just copy/paste this if you want):

<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>
Ben Junya
Ben Junya
12,365 Points

Hi Ben,

Everything is looking alright, but there's still one more problem.

First off, the theme I get on the phone has a white background still. Second, when I move TextView1 into the center (aligning horiz and vert), the app crashes. If TextView1 is in the upper lefthand corner, it works, but the second I move it, the application crashes.

You're totally helping me out here Ben. Huge thanks for being prompt and patient!

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Aw, I didn't check the background. Rats. That's an easy fix - you can change the background color of the whole RelativeLayout that makes up the entire screen layout. You just need to do it in the XML view, because trying to do it in the Graphical Layout is weird.

So in the XML tab, add the following line to your RelativeLayout element:

android:background="@android:color/black"

Here's what it looks like together:

  <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" >

    ... rest of code ...

As for the TextView...that shouldn't be crashing. After making the first change, can you try restarting Eclipse and see if you can add the TextView that way? If not, you can again edit the XML directly, replacing your TextView with this one:

        <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" />
Ben Junya
Ben Junya
12,365 Points

Hey Ben, I got this error after replacing the textview code with this one:

04-14 21:22:46.484: E/AndroidRuntime(1065): FATAL EXCEPTION: main 04-14 21:22:46.484: E/AndroidRuntime(1065): Process: com.example.crystalball, PID: 1065 04-14 21:22:46.484: E/AndroidRuntime(1065): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.crystalball/com.example.crystalball.MainActivity}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button

The application is still crashing unfortunately.

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Sounds like perhaps your IDs for TextView and Button got swapped, or something else got swapped in your code. Post your layout XML and onCreate() code in here if you aren't able to figure out which was switched.

Thanks for your patience working through this; even though it might seem like a pain, it's hopefully a good learning experience for you. :)

Ben Junya
Ben Junya
12,365 Points

Hey Ben,

Yes, I am totally learning a lot from your course. It's well written and quite easy to understand.

Although I don't understand 100% of each and every single line, I am slowly getting the hang of it.

Here's ActivityMain.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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="@android:color/black"
    tools:context="com.example.crystalball.MainActivity$PlaceholderFragment" >

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

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

    <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" />

</RelativeLayout>

And here's 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();

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

        // Declare our View variables here
        final TextView answerLabel = (TextView) findViewById(R.id.textView1);
        Button getAnswerButton = (Button) findViewById(R.id.button1);

        getAnswerButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                String answer = mCrystalBall.getAnAnswer();
                // Update the label with our dynamic answer
                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);
    }


}

Thanks again!

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Looks perfect. :-/ Can you try cleaning the project and uninstalling/reinstalling it?

  1. Project > Clean in Eclipse
  2. Uninstall from the emulator
  3. Run the project from Eclipse

What line number does it give in your error message for that ClassCastException? You can paste more of the error message in here if you need to.

Ben Junya
Ben Junya
12,365 Points

Ah ha! It works! Eureka!

The only exception is that the color of TextView1 is purple :/ - aside from that, it should be an easy fix.

Thanks again Ben!

One final question - in regards to compile errors like we just had here, is it wise to "clean" the project every now and then if something screwy happens like just now?

Ben Jakuben
Ben Jakuben
Treehouse Teacher

It's always good to try it when things aren't working quite as expected. :) Generally speaking, it's sometimes necessary when changing things in the res directory (adding or updating files, changing layouts). Sometimes the previous versions get cached somewhere in a way that only a fresh clean can get rid of.

Siddharth Saravanan
Siddharth Saravanan
1,208 Points

Hey Ben, I am having the same problem, but I don't think it is exactly the same. I don't have any errors in the project anywhere, but when I open the app in the emulator, it says " Unfortunately, Crystal Ball has stopped". I have tried making new emulators but it still gives me the message. I have tried everything you have said above, but it still didn't work. Even after I did everything you said, I changed it all back so its just like the video and I tried again, and I still got the message in the emulator. I just finished the video "adding an image", in the "pretty little things" chapter, and I watched all the videos from the beginning of the whole course "how to build a simple android app", to where I am right now, and I cant seem to find a solution at all. Please help, it would be well appreciated.

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Hi Siddharth,

You should have some errors in the logcat console inside Eclipse when this happens. Can you please copy all your errors and then create a new post in the Forum and paste them in there? Those should help us troubleshoot the problem. Tag me in the new post to make sure I see it.

Siddharth Saravanan
Siddharth Saravanan
1,208 Points

I already have. But I don't know how to tag you or people so can you tell me how?

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Siddharth Saravanan, type the "@" symbol and then start typing a name and a drop-down should appear with a list of matches. :)

Please ensure that your app activity extends "Activity" and not "ActionBarActivity".

Ben Junya
Ben Junya
12,365 Points

Can you elaborate? Please post directions on how to fix this

public class MainActivity extends Activity (correct)

public class MainActivity extends ActionBarActivity (incorrect)

Ben Junya
Ben Junya
12,365 Points

That just created more errors. It didn't fix anything.

Here's MainActivity

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();

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

    // Declare our View variables here
    final TextView answerLabel = (TextView) findViewById(R.id.textView1);
    Button getAnswerButton = (Button) findViewById(R.id.button1);

    getAnswerButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            String answer = mCrystalBall.getAnAnswer();
            // Update the label with our dynamic answer
            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);
}

}

Changing ActionBarActivity to Activity merely caused more problems and Eclipse refuses to compile the project, or let it run on an emulator or android device.

Hi, I'm also having the problem where any theme I put into the manifest causes my app to crash, even though the code has no errors. I need to achieve the look of Theme.Black.NoTitleBar.Fullscreen, but nothing works for some reason. I tried going to styles.xml and typing the recommended phrases, and the "windowFullscreen" got rid of the status bar (clock, etc.), but nothing (including "windowNoTitle") will get rid of the title. I've tried everything I can think of for like five hours, but still the title remains. Any help would be greatly appreciated. Thanks

Ben Junya
Ben Junya
12,365 Points

Hey Chase,

If you look at the above response Ben has posted, it works out very well. Give it a shot. It worked for me.