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

Crystal Ball App Changing Theme Crashes App

I am going through the Crystal Ball track. When I change the theme from the default it crashes. If I put back the default App theme it works. The theme I am trying to switch to is the one in the videos "Theme.Black.NoTitleBar.Fullscreen" please help.

When the app is working my theme is set to android:theme="@style/AppBaseTheme" > Here is the XML:

<?xml version="1.0" encoding="utf-8"?> <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="7"
    android:targetSdkVersion="19" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppBaseTheme" >
    <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>

Once I change the Theme as the video says to android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen" > The app does not run on the emulator. Here is the XML file with the changed theme.

<?xml version="1.0" encoding="utf-8"?> <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="7"
    android:targetSdkVersion="19" />

<application
    android:allowBackup="true"
    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 the LogCat errors:

03-25 07:09:10.403: D/dalvikvm(1076): Not late-enabling CheckJNI (already on) 03-25 07:09:12.273: D/AndroidRuntime(1076): Shutting down VM 03-25 07:09:12.273: W/dalvikvm(1076): threadid=1: thread exiting with uncaught exception (group=0xb1a9bba8) 03-25 07:09:12.343: E/AndroidRuntime(1076): FATAL EXCEPTION: main 03-25 07:09:12.343: E/AndroidRuntime(1076): Process: com.example.crystalball, PID: 1076 03-25 07:09:12.343: E/AndroidRuntime(1076): 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. 03-25 07:09:12.343: E/AndroidRuntime(1076): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 03-25 07:09:12.343: E/AndroidRuntime(1076): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 03-25 07:09:12.343: E/AndroidRuntime(1076): at android.app.ActivityThread.access$800(ActivityThread.java:135) 03-25 07:09:12.343: E/AndroidRuntime(1076): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 03-25 07:09:12.343: E/AndroidRuntime(1076): at android.os.Handler.dispatchMessage(Handler.java:102) 03-25 07:09:12.343: E/AndroidRuntime(1076): at android.os.Looper.loop(Looper.java:136) 03-25 07:09:12.343: E/AndroidRuntime(1076): at android.app.ActivityThread.main(ActivityThread.java:5017) 03-25 07:09:12.343: E/AndroidRuntime(1076): at java.lang.reflect.Method.invokeNative(Native Method) 03-25 07:09:12.343: E/AndroidRuntime(1076): at java.lang.reflect.Method.invoke(Method.java:515) 03-25 07:09:12.343: E/AndroidRuntime(1076): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 03-25 07:09:12.343: E/AndroidRuntime(1076): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 03-25 07:09:12.343: E/AndroidRuntime(1076): at dalvik.system.NativeStart.main(Native Method) 03-25 07:09:12.343: E/AndroidRuntime(1076): Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. 03-25 07:09:12.343: E/AndroidRuntime(1076): at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:111) 03-25 07:09:12.343: E/AndroidRuntime(1076): at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:58) 03-25 07:09:12.343: E/AndroidRuntime(1076): at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:98) 03-25 07:09:12.343: E/AndroidRuntime(1076): at com.example.crystalball.MainActivity.onCreate(MainActivity.java:30) 03-25 07:09:12.343: E/AndroidRuntime(1076): at android.app.Activity.performCreate(Activity.java:5231) 03-25 07:09:12.343: E/AndroidRuntime(1076): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 03-25 07:09:12.343: E/AndroidRuntime(1076): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 03-25 07:09:12.343: E/AndroidRuntime(1076): ... 11 more

Thanks for the help.

Saludos nuevamente .

Gracias por actualizar tu pregunta y por seguir mis consejos . Ahora esta todo claro , acá te dejo muy posibles soluciones .

  • Has lo siguiente con tu archivo manifest ```xml <?xml version="1.0" encoding="utf-8"?>

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

<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppBaseTheme" > <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>

+ Ahora en tu "MainActivity.class" pon lo siguiente , recuerda extender a Activity
```java
public class Main_Login extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //FullScreen
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

                //
        setContentView(R.layout.my_layout_xml);
         }
}

Espero que te ayude , en caso contrario actualiza tu pregunta . Gracias

Saludos nuevamente .

Gracias por actualizar tu pregunta y por seguir mis consejos . Ahora esta todo claro , acá te dejo muy posibles soluciones .

  • Has lo siguiente con tu archivo manifest ```xml <?xml version="1.0" encoding="utf-8"?>

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

<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppBaseTheme" > <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>

+ Ahora en tu "MainActivity.class" pon lo siguiente , recuerda extender a Activity
```java
public class Main_Login extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //FullScreen
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

                //
        setContentView(R.layout.my_layout_xml);
         }
}

Espero que te ayude , en caso contrario actualiza tu pregunta . Gracias

                    Thanks for the help, 

I am posting my original MainActivity.java because adding the code suggested gave errors I do not know how to fix. How would I add the code suggested without causing errors?

package com.example.crystalball;

import android.graphics.drawable.AnimationDrawable;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {
        //Declaring Variables 
        private CrystalBall mCrystalBall = new CrystalBall();
        private TextView mAnswerLabel;
        private Button mGetAnswerButton;
        private ImageView mCrystalBallImage;



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

        // Assign our Views from the layout file
        mAnswerLabel = (TextView) findViewById(R.id.textView1); 
        mGetAnswerButton= (Button) findViewById(R.id.button1);
        mCrystalBallImage = (ImageView) findViewById(R.id.imageView1);

        mGetAnswerButton.setOnClickListener(new View.OnClickListener() {

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



                //Update the label with our dynamic answer
                mAnswerLabel.setText(answer);

                animateCrystalBall();
                animateAnswer();
                playSound();
            }
        });


    }

    private void animateCrystalBall(){

        mCrystalBallImage.setImageResource(R.drawable.ball_animation);
        AnimationDrawable ballAnimation = (AnimationDrawable) mCrystalBallImage.getDrawable(); 
        if (ballAnimation.isRunning()){
            ballAnimation.stop();
        }
        ballAnimation.start();
    }

    private void animateAnswer(){
        AlphaAnimation fadeInAnimation = new AlphaAnimation(0, 1);
        fadeInAnimation.setDuration(1500);
        fadeInAnimation.setFillAfter(true);

        mAnswerLabel.setAnimation(fadeInAnimation);
    }

    private void playSound(){
        MediaPlayer player = MediaPlayer.create(this, R.raw.crystal_ball); 
        player.start();
        player.setOnCompletionListener(new OnCompletionListener() {

            @Override
            public void onCompletion(MediaPlayer mp) {
                mp.release();
            }
        });
    }
    @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);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.activity_main, container,
                    false);
            return rootView;
        }
    }

}

5 Answers

Ok I have worked out a solution not clear why but it works. Cristian Palomino Rivera Thanks for the help sorry I was not able to understand what you were telling me to do.

  1. I created a new project and up my android support to version 4.0 and higher.
  2. I copied my code and refactored to new project name.
  3. I changed my extends from ActionBarActivity to Activity in my MainActivity.java.
  4. I commented out all code in my new project that was created by default and removed my Fragments under layout as I did before.

Here is my working Main Activity.java

package com.example.crystalball2;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {

    //Declaring Variables 
    private CrystalBall mCrystalBall = new CrystalBall();
    private TextView mAnswerLabel;
    private Button mGetAnswerButton;
    private ImageView mCrystalBallImage;



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


    // Assign our Views from the layout file
    mAnswerLabel = (TextView) findViewById(R.id.textView1); 
    mGetAnswerButton= (Button) findViewById(R.id.button1);
    mCrystalBallImage = (ImageView) findViewById(R.id.imageView1);

    mGetAnswerButton.setOnClickListener(new View.OnClickListener() {

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



            //Update the label with our dynamic answer
            mAnswerLabel.setText(answer);

            animateCrystalBall();
            animateAnswer();
            playSound();
        }
    });


}

private void animateCrystalBall(){

    mCrystalBallImage.setImageResource(R.drawable.ball_animation);
    AnimationDrawable ballAnimation = (AnimationDrawable) mCrystalBallImage.getDrawable(); 
    if (ballAnimation.isRunning()){
        ballAnimation.stop();
    }
    ballAnimation.start();
}

private void animateAnswer(){
    AlphaAnimation fadeInAnimation = new AlphaAnimation(0, 1);
    fadeInAnimation.setDuration(1500);
    fadeInAnimation.setFillAfter(true);

    mAnswerLabel.setAnimation(fadeInAnimation);
}

private void playSound(){
    MediaPlayer player = MediaPlayer.create(this, R.raw.crystal_ball); 
    player.start();
    player.setOnCompletionListener(new OnCompletionListener() {

        @Override
        public void onCompletion(MediaPlayer mp) {
            mp.release();
        }
    });
}
@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);
}

//Original Code

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

        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }
    }

    @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);
    }
*/
    /**
     * A placeholder fragment containing a simple view.
     */
    /*public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);
            return rootView;
        }
    }*/

}

Saludos.

Prueba esto , "android:Theme.Black.NoTitleBar.Fullscreen"

Te recomiendo adjuntar imagenes a tus preguntas para poder llegar a una respuesta exacta .

Saludos nuevamente .

Gracias por actualizar tu pregunta y por seguir mis consejos . Ahora esta todo claro , acá te dejo muy posibles soluciones .

+Has lo siguiente con tu archivo manifest

<?xml version="1.0" encoding="utf-8"?>

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

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppBaseTheme" >
    <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>

+Ahora en tu "MainActivity.class" pon lo siguiente , recuerda extender a Activity

public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

//FullScreen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

setContentView(R.layout.my_layout_xml);
 }
} 
  • Espero que te ayude , en caso contrario actualiza tu pregunta . Gracias

I am trying to integrate what you have put in the code and I keep getting errors can you show what you are saying to add fully integrated?

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Hey all,

Really sorry about this issue. The latest Android tools update is really causing problems with our older projects. Here's a Forum post that can help, and I added notes to the video page: https://teamtreehouse.com/forum/you-need-to-use-a-themeappcompat-theme-or-descendent-crystal-ball-crashes