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) Testing and Debugging The Android Log

J Smillie
J Smillie
39,714 Points

Having problems debugging my FunFacts Android App.

Hi,

This is my first forum post. It's not directly about a Treehouse question but about

So I have been following this first Android course and making my own FunFacts app in Android Studio alongside it. But when I came to run the:

Log.d(TAG, "We're logging from the onCrate() Method!");

In stage 6 I found that I have a flaw/bug in my code. I don't want to retrace my steps backward through the course or restart the app from scratch.

This is what the Debug Log (or logcat?) show me when I try and run my app:

09-18 15:06:26.603 1192-1192/com.wolfonmoon.funfacts E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.wolfonmoon.funfacts, PID: 1192 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.wolfonmoon.funfacts/com.wolfonmoon.funfacts.FunFactsActivity}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) at android.app.ActivityThread.access$800(ActivityThread.java:135) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5017) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button at com.wolfonmoon.funfacts.FunFactsActivity.onCreate(FunFactsActivity.java:21) at android.app.Activity.performCreate(Activity.java:5231) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)             at android.app.ActivityThread.access$800(ActivityThread.java:135)             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)             at android.os.Handler.dispatchMessage(Handler.java:102)             at android.os.Looper.loop(Looper.java:136)             at android.app.ActivityThread.main(ActivityThread.java:5017)             at java.lang.reflect.Method.invokeNative(Native Method)             at java.lang.reflect.Method.invoke(Method.java:515)             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)             at dalvik.system.NativeStart.main(Native Method)

Can someone help me if they have the time? It would really help me out Thank you!

5 Answers

android.widget.TextView cannot be cast to android.widget.Button

This seems to be the fatal error. You appear to be casting a TextView initializer to a Button type, and it's throwing the computer off. Cast types are declared after the equals sign and in parenthesis as seen below. Be sure your TextView variable looks like the following in your FunFactsActivity.java:

TextView factView = (TextView) findViewById(R.id.factView);

...updated to match YOUR view id and variable name, however. :)

J Smillie
J Smillie
39,714 Points

Thanks for your answer!

I'm fairly certain that I have the exact same code as Ben in the video - which is why I'm having such a hard time discerning where the problem is happening. The code I have for this is:

    final TextView factLabel = (TextView) findViewById(R.id.factTextView);

    final Button showFactButton = (Button) findViewById(R.id.showFactButton);

    final RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);

I cannot find where I must be assigning TextView to a button. Is there a way to find the exact line of code where this is occurring?

J Smillie
J Smillie
39,714 Points

The following two lines of code from the Debug log would suggest that the problem occurs at line 21 in FunFactsActivity.java:

Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Button

at com.wolfonmoon.funfacts.FunFactsActivity.onCreate(FunFactsActivity.java:21)

which would mean that this line of code:

        private FactBook mFactBook = new FactBook();

is somehow declaring a Textview a Button?

Compared side-by-side with my declaration of mFactBook, yours is correct...but what I don't get is why it's saying you cannot cast a TextView to a Button when our cast types all seem to be in order.

Have you tried doing a Clean of your app? Maybe the error isn't even real?

J Smillie
J Smillie
39,714 Points

Okay so I have managed to fi the code. I think it was just a mistake I had made in the ordering. But my Debug Log is still not showing the Log message "We're logging from the onCreate()Method!"

Sorry to just paste a large swathe of code again, but here I go... Can anyone tell me why the Debug log does not show my message from this:

         public class FunFactsActivity extends Activity {

     public static final String TAG = FunFactsActivity.class.getSimpleName();

     private FactBook mFactBook = new FactBook();
     private ColorWheel mColorWheel = new ColorWheel();

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

    // Declare our View variables and assign the views from the layout file
    final TextView factLabel = (TextView) findViewById(R.id.factTextView);
    final Button showFactButton = (Button) findViewById(R.id.showFactButton);
    final RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);

    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
                    String fact = mFactBook.getFact();
                    //Update the label with our dynamic fact
                    factLabel.setText(fact);


                    int color = mColorWheel.getColor();
                    relativeLayout.setBackgroundColor(color);
                    showFactButton.setTextColor(color);

            }
        };
        showFactButton.setOnClickListener(listener);



       //Toast.makeText (this, "Yay! Our Activity was created!", Toast.LENGTH_LONG).show();

         Log.d(TAG, "We're logging from the onCreate() Method!");
}

}

Hmm, uncomment the toast and see if that shows up. If not, use those handy dandy line breaks and see where it's stopping. :)

J Smillie
J Smillie
39,714 Points

will try that! thanks for your help!

Jeremiah Shore
Jeremiah Shore
31,168 Points

(Windows) I was also not seeing the log message. I restarted the virtual device I was testing on and the message started showing up.