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) Coding the Fun Facts Properties of Arrays

Unfortunately, FunFacts has stopped working

from the beginning up to this point, everything were fun and easy but after deleting the If Condition and adding the Arrays, I am facing app crash as soon as I run the app. help please

Hello,

Could you post the full error output from Android Studio? Additionally, could you also post your code? Both of those would help us in assisting you with your issue.

It doesn't seem like there is an error and here is my whole code

package com.ceelcali.funfacts;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.util.Random;


public class FunFactsActivity extends Activity {

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

        //Declare our variables and assign the views from the layout files

        final TextView FactLabel;
        FactLabel = (TextView) findViewById(R.id.FactstextView);
        Button FactsButton = (Button) findViewById(R.id.FactsButton);
        View.OnClickListener Listener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {


           //The Button was clicked, so update the fact label with a new fact
                String fact = "";
                //Randomly select a fact
                Random randomGenerator = new Random(); // Construct a new random number generator
                int randomNumber = randomGenerator.nextInt(fact.length());


     String[] facts = {"Ma Ogtahay Maxamed Gaalkacyo inuu yahay ninka ugu Qiirada badan Jaamacadda uguna Gardarrada badan?","Ma Ogtahay Cilaaji inuu Cabo 2 Galaas oo Cusbo eh Maalin walbo?","Ma Ogtahay Shaafac inuu wacad ku maray inuu furi doona Beeytul Maal loogu Maciino Ardayda. Ibrahim Katiitoowna inuu aad usoo dhaweeyay Fikrada Shaafac ee furitaanka Beeytul maalka uuna noqoyay qufkii ugu horeeyay ee Lacag dhigo Siraajina uu madaxa ka lulay, cod dheerna ku sheegay in Ibraahim Katiitoow la Turfatooreeyay! ","Ma Ogtahay Axmed GeesDiir inuu Asal ka soo jeeda Teetaaree JUFFO hoosana Wadaagaan Milkiilaha  AliBaba?","Ma ogtahay in Ubo Kuuskuus lasoo deristay Cabsi xoogan uuna habeenki Sariirta Hoos geshto Faash dadaal uu ugu jiro sidii uu isaga difaaci lahaay Buuq oo maalmo ka hor ku goodiyay inuu Wacha-Wacha iyo style 169 uu Ubo ku dili doona . "};


                fact = facts[randomNumber];

                FactLabel.setText(fact);



            }
        };
        FactsButton.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();
        return id == R.id.action_settings || super.onOptionsItemSelected(item);
    }
}

Hello,

Do you have the error output from logcat? To get this, run your app in debug mode, then select the android bar at the bottom of Android Studio. For more information about viewing the Android system log, please reference this page.

I run the app in debug mode and I'm pasting here what I copied from logcat:

05-30 11:44:18.263 1406-1406/com.ceelcali.funfacts D/libEGL﹕ loaded /system/lib/egl/libEGL_genymotion.so 05-30 11:44:18.275 1406-1406/com.ceelcali.funfacts D/﹕ HostConnection::get() New Host Connection established 0xb8e6ef70, tid 1406 05-30 11:44:18.315 1406-1406/com.ceelcali.funfacts D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_genymotion.so 05-30 11:44:18.315 1406-1406/com.ceelcali.funfacts D/libEGL﹕ loaded /system/lib/egl/libGLESv2_genymotion.so 05-30 11:44:18.443 1406-1406/com.ceelcali.funfacts W/EGL_genymotion﹕ eglSurfaceAttrib not implemented 05-30 11:44:18.447 1406-1406/com.ceelcali.funfacts E/OpenGLRenderer﹕ Getting MAX_TEXTURE_SIZE from GradienCache 05-30 11:44:18.451 1406-1406/com.ceelcali.funfacts E/OpenGLRenderer﹕ MAX_TEXTURE_SIZE: 16384 05-30 11:44:18.463 1406-1406/com.ceelcali.funfacts E/OpenGLRenderer﹕ Getting MAX_TEXTURE_SIZE from Caches::initConstraints() 05-30 11:44:18.467 1406-1406/com.ceelcali.funfacts E/OpenGLRenderer﹕ MAX_TEXTURE_SIZE: 16384 05-30 11:44:18.467 1406-1406/com.ceelcali.funfacts D/OpenGLRenderer﹕ Enabling debug mode 0 05-30 11:44:20.351 1406-1406/com.ceelcali.funfacts D/AndroidRuntime﹕ Shutting down VM 05-30 11:44:20.351 1406-1406/com.ceelcali.funfacts W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa4cddb20) 05-30 11:44:20.351 1406-1406/com.ceelcali.funfacts E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.ceelcali.funfacts, PID: 1406 java.lang.IllegalArgumentException: n <= 0: 0 at java.util.Random.nextInt(Random.java:175) at com.ceelcali.funfacts.FunFactsActivity$1.onClick(FunFactsActivity.java:35) at android.view.View.performClick(View.java:4438) at android.view.View$PerformClick.run(View.java:18422) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5001) 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:785) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) at dalvik.system.NativeStart.main(Native Method) 05-30 11:45:25.767 1475-1475/com.ceelcali.funfacts I/System.out﹕ Sending WAIT chunk 05-30 11:45:25.767 1475-1475/com.ceelcali.funfacts W/ActivityThread﹕ Application com.ceelcali.funfacts is waiting for the debugger on port 8100... 05-30 11:45:26.399 1475-1481/com.ceelcali.funfacts I/dalvikvm﹕ Debugger is active 05-30 11:45:26.575 1475-1475/com.ceelcali.funfacts I/System.out﹕ Debugger has connected 05-30 11:45:26.575 1475-1475/com.ceelcali.funfacts I/System.out﹕ waiting for debugger to settle... 05-30 11:45:26.775 1475-1475/com.ceelcali.funfacts I/System.out﹕ waiting for debugger to settle... 05-30 11:45:26.979 1475-1475/com.ceelcali.funfacts I/System.out﹕ waiting for debugger to settle... 05-30 11:45:27.187 1475-1475/com.ceelcali.funfacts I/System.out﹕ waiting for debugger to settle... 05-30 11:45:27.383 1475-1475/com.ceelcali.funfacts I/System.out﹕ waiting for debugger to settle... 05-30 11:45:27.603 1475-1475/com.ceelcali.funfacts I/System.out﹕ waiting for debugger to settle... 05-30 11:45:27.843 1475-1475/com.ceelcali.funfacts I/System.out﹕ waiting for debugger to settle... 05-30 11:45:28.071 1475-1475/com.ceelcali.funfacts I/System.out﹕ waiting for debugger to settle... 05-30 11:45:28.451 1475-1475/com.ceelcali.funfacts I/System.out﹕ waiting for debugger to settle... 05-30 11:45:28.651 1475-1475/com.ceelcali.funfacts I/System.out﹕ waiting for debugger to settle... 05-30 11:45:28.855 1475-1475/com.ceelcali.funfacts I/System.out﹕ waiting for debugger to settle... 05-30 11:45:29.055 1475-1475/com.ceelcali.funfacts I/System.out﹕ waiting for debugger to settle... 05-30 11:45:29.259 1475-1475/com.ceelcali.funfacts I/System.out﹕ waiting for debugger to settle... 05-30 11:45:29.459 1475-1475/com.ceelcali.funfacts I/System.out﹕ waiting for debugger to settle... 05-30 11:45:29.663 1475-1475/com.ceelcali.funfacts I/System.out﹕ waiting for debugger to settle... 05-30 11:45:29.863 1475-1475/com.ceelcali.funfacts I/System.out﹕ waiting for debugger to settle... 05-30 11:45:30.067 1475-1475/com.ceelcali.funfacts I/System.out﹕ waiting for debugger to settle... 05-30 11:45:30.267 1475-1475/com.ceelcali.funfacts I/System.out﹕ debugger has settled (1454) 05-30 11:45:30.503 1475-1475/com.ceelcali.funfacts D/libEGL﹕ loaded /system/lib/egl/libEGL_genymotion.so 05-30 11:45:30.503 1475-1475/com.ceelcali.funfacts D/﹕ HostConnection::get() New Host Connection established 0xb8e7d848, tid 1475 05-30 11:45:30.539 1475-1475/com.ceelcali.funfacts D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_genymotion.so 05-30 11:45:30.539 1475-1475/com.ceelcali.funfacts D/libEGL﹕ loaded /system/lib/egl/libGLESv2_genymotion.so 05-30 11:45:30.651 1475-1475/com.ceelcali.funfacts W/EGL_genymotion﹕ eglSurfaceAttrib not implemented 05-30 11:45:30.651 1475-1475/com.ceelcali.funfacts E/OpenGLRenderer﹕ Getting MAX_TEXTURE_SIZE from GradienCache 05-30 11:45:30.655 1475-1475/com.ceelcali.funfacts E/OpenGLRenderer﹕ MAX_TEXTURE_SIZE: 16384 05-30 11:45:30.675 1475-1475/com.ceelcali.funfacts E/OpenGLRenderer﹕ Getting MAX_TEXTURE_SIZE from Caches::initConstraints() 05-30 11:45:30.675 1475-1475/com.ceelcali.funfacts E/OpenGLRenderer﹕ MAX_TEXTURE_SIZE: 16384 05-30 11:45:30.675 1475-1475/com.ceelcali.funfacts D/OpenGLRenderer﹕ Enabling debug mode 0 05-30 11:46:07.671 1475-1475/com.ceelcali.funfacts D/AndroidRuntime﹕ Shutting down VM 05-30 11:46:07.671 1475-1475/com.ceelcali.funfacts W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa4cddb20) 05-30 11:46:07.679 1475-1475/com.ceelcali.funfacts E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.ceelcali.funfacts, PID: 1475 java.lang.IllegalArgumentException: n <= 0: 0 at java.util.Random.nextInt(Random.java:175) at com.ceelcali.funfacts.FunFactsActivity$1.onClick(FunFactsActivity.java:35) at android.view.View.performClick(View.java:4438) at android.view.View$PerformClick.run(View.java:18422) at android.os.Handler.handleCallback(Handler.java:733) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5001) 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:785) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) at dalvik.system.NativeStart.main(Native Method) 05-30 11:46:09.403 1475-1475/com.ceelcali.funfacts I/Process﹕ Sending signal. PID: 1475 SIG: 9

1 Answer

Hello,

I see the error now. In your code

            String fact = "";
            //Randomly select a fact
            Random randomGenerator = new Random(); // Construct a new random number generator
            int randomNumber = randomGenerator.nextInt(fact.length());

You are calling fact.length(), but currently fact is "", an empty string, so it has a length of zero. What you want to do is put your facts string array above this code and then change fact.length() to facts.length. To look like

String[] facts = {"Ma Ogtahay Maxamed Gaalkacyo inuu yahay ninka ugu Qiirada badan Jaamacadda uguna Gardarrada badan?","Ma Ogtahay Cilaaji inuu Cabo 2 Galaas oo Cusbo eh Maalin walbo?","Ma Ogtahay Shaafac inuu wacad ku maray inuu furi doona Beeytul Maal loogu Maciino Ardayda. Ibrahim Katiitoowna inuu aad usoo dhaweeyay Fikrada Shaafac ee furitaanka Beeytul maalka uuna noqoyay qufkii ugu horeeyay ee Lacag dhigo Siraajina uu madaxa ka lulay, cod dheerna ku sheegay in Ibraahim Katiitoow la Turfatooreeyay! ","Ma Ogtahay Axmed GeesDiir inuu Asal ka soo jeeda Teetaaree JUFFO hoosana Wadaagaan Milkiilaha  AliBaba?","Ma ogtahay in Ubo Kuuskuus lasoo deristay Cabsi xoogan uuna habeenki Sariirta Hoos geshto Faash dadaal uu ugu jiro sidii uu isaga difaaci lahaay Buuq oo maalmo ka hor ku goodiyay inuu Wacha-Wacha iyo style 169 uu Ubo ku dili doona . "};

            String fact = "";
            //Randomly select a fact
            Random randomGenerator = new Random(); // Construct a new random number generator
            int randomNumber = randomGenerator.nextInt(facts.length);

That's at least a dirty method of fixing the issue. Ideally, you'd want to move facts somewhere even better, like a string array resource, but that's a bit beyond this course I think. Please let us know if that helps or if you need more assistance. If you do need more assistance, could you please provide your updated code and error messages so that we can help you from where you are at.

That's the Best Answer, It solved, Thank you very much Mr James Simshaw,

You're welcome. Have fun with Android development.