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 (2014) Improving Our Code Simple Refactoring: Using a Class

Can't run app

My code is identical to the instructor but when i run it on the emulator it says "unfortunately, fun facts has stopped." i think something is wrong with the emulator

Could you provide the full error message from logcat and your source code (it might seem identical, but a single typo can cause errors to occur)?

Source Code: package com.lukan.funfacts;

import java.util.Random;

import android.support.v7.app.ActionBarActivity; import android.support.v7.app.ActionBar; import android.support.v4.app.Fragment; import android.graphics.Color; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.RelativeLayout; import android.widget.TextView; import android.os.Build;

public class FunFactsActivity extends ActionBarActivity {

private FactBook mFactBook = new FactBook();

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

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
        //Declare view variable
        final TextView factLabel = (TextView) findViewById(R.id.factTextView);
        Button showFactButton = (Button) findViewById(R.id.showFactButton);
        final RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);

        View.OnClickListener listener = new View.OnClickListener(){
        public void OnClick(View view){
            String fact = mFactBook.getFact();

            factLabel.setText(fact);
            relativeLayout.setBackgroundColor(Color.RED);
        }
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub  
        }
        };
        showFactButton.setOnClickListener(listener);

    }
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.fun_facts, 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_fun_facts, container, false);
        return rootView;
    }
}

} LogCat: 04-23 11:31:07.010: D/dalvikvm(1048): Not late-enabling CheckJNI (already on) 04-23 11:31:09.090: D/AndroidRuntime(1048): Shutting down VM 04-23 11:31:09.090: W/dalvikvm(1048): threadid=1: thread exiting with uncaught exception (group=0xb1a36ba8) 04-23 11:31:09.170: E/AndroidRuntime(1048): FATAL EXCEPTION: main 04-23 11:31:09.170: E/AndroidRuntime(1048): Process: com.lukan.funfacts, PID: 1048 04-23 11:31:09.170: E/AndroidRuntime(1048): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lukan.funfacts/com.lukan.funfacts.FunFactsActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. 04-23 11:31:09.170: E/AndroidRuntime(1048): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 04-23 11:31:09.170: E/AndroidRuntime(1048): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 04-23 11:31:09.170: E/AndroidRuntime(1048): at android.app.ActivityThread.access$800(ActivityThread.java:135) 04-23 11:31:09.170: E/AndroidRuntime(1048): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 04-23 11:31:09.170: E/AndroidRuntime(1048): at android.os.Handler.dispatchMessage(Handler.java:102) 04-23 11:31:09.170: E/AndroidRuntime(1048): at android.os.Looper.loop(Looper.java:136) 04-23 11:31:09.170: E/AndroidRuntime(1048): at android.app.ActivityThread.main(ActivityThread.java:5017) 04-23 11:31:09.170: E/AndroidRuntime(1048): at java.lang.reflect.Method.invokeNative(Native Method) 04-23 11:31:09.170: E/AndroidRuntime(1048): at java.lang.reflect.Method.invoke(Method.java:515) 04-23 11:31:09.170: E/AndroidRuntime(1048): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 04-23 11:31:09.170: E/AndroidRuntime(1048): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 04-23 11:31:09.170: E/AndroidRuntime(1048): at dalvik.system.NativeStart.main(Native Method) 04-23 11:31:09.170: E/AndroidRuntime(1048): Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity. 04-23 11:31:09.170: E/AndroidRuntime(1048): at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:108) 04-23 11:31:09.170: E/AndroidRuntime(1048): at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:57) 04-23 11:31:09.170: E/AndroidRuntime(1048): at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:98) 04-23 11:31:09.170: E/AndroidRuntime(1048): at com.lukan.funfacts.FunFactsActivity.onCreate(FunFactsActivity.java:26) 04-23 11:31:09.170: E/AndroidRuntime(1048): at android.app.Activity.performCreate(Activity.java:5231) 04-23 11:31:09.170: E/AndroidRuntime(1048): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 04-23 11:31:09.170: E/AndroidRuntime(1048): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 04-23 11:31:09.170: E/AndroidRuntime(1048): ... 11 more

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

  • <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.lukan.funfacts" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="14" android:targetSdkVersion="19" />
  • <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme">
  • <activity android:name="com.lukan.funfacts.FunFactsActivity" android:label="@string/app_name">
  • <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>

Styles:

  • <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.DarkActionBar">
  • <!-- 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="Theme.AppCompat.Light.DarkActionBar">
  • <!-- All customizations that are NOT specific to a particular API-level can go here. --> </style> </resources>

(Not sure which layout it is but here's two files I found for the layout) Layout: <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.lukan.funfacts.FunFactsActivity" tools:ignore="MergeRootFrame" />

Fragment Layout:

  • <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/relativeLayout" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/Green" 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.lukan.funfacts.FunFactsActivity$PlaceholderFragment"> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Did you know?" android:textColor="@color/transWhite" android:textSize="24sp" /> <TextView android:id="@+id/factTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView2" android:layout_centerVertical="true" android:text="Ants stretch when they wake up in the morning." android:textColor="@color/White" android:textSize="24sp" /> <Button android:id="@+id/showFactButton" style="?android:attr/buttonStyleSmall" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:background="@color/White" android:text="Show Another Fun Fact" android:textColor="@color/Green" /> </RelativeLayout>

1 Answer

Hello,

Two things:

1) You should remove the following segement of code since you already define an onClick method right above it.

@Override
        public void onClick(View v) {
            // TODO Auto-generated method stub  
        }

2) You need to make sure that the theme set for FunFactsActivity is set to AppCompat or some child of AppCompat. This is the cause of your error currently.

Im kind of new to all this, but do I change the theme in the Styles.XML file?

There are a few places that the theme could have been set to something wrong. Could you try posting your styles.xml file, your layout xml file, and your AndroidManifest.xml file?