Why do the random number crashes my Android app?

811 views Asked by At

I am trying to show a random fact and change the background color each time the fact changes.

I do it using a random number on the MainActivity but as soon as I press the button to generate and show that random number the app crashes.

Here is the link to the project on GitHub: https://github.com/ishay1999/FunFacts

Here is the error shown in the logCat:

 06-09 16:36:27.125  14551-14551/com.howtoevery.funfacts E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.howtoevery.funfacts, PID: 14551
    android.content.res.Resources$NotFoundException: String resource ID #0x0
            at android.content.res.Resources.getText(Resources.java:275)
            at android.widget.TextView.setText(TextView.java:4261)
            at com.howtoevery.funfacts.MainActivity$1.onClick(MainActivity.java:56)
            at android.view.View.performClick(View.java:4763)
            at android.view.View$PerformClick.run(View.java:19821)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5274)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)

and here is the MainActivity.java code:

package com.howtoevery.funfacts;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
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 java.util.Random;


public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // Creating the random
        final Random rand = new Random();


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final TextView factLabel;
        final Button showFactButton;

        factLabel = (TextView) findViewById(R.id.fact);
        final ViewGroup backGround = (RelativeLayout) findViewById(R.id.mainBackground);
        showFactButton = (Button) findViewById(R.id.factButton);

        View.OnClickListener myListener = new View.OnClickListener() {

            int randomNumber;

            @Override
            public void onClick(View view) {
                randomNumber = rand.nextInt(1); // randomNumber will be 0 or 1

                switch (randomNumber) {
                    case 0: { // in case it is 0, show fact number 1 with green color
                        factLabel.setText(R.string.fact1);
                        backGround.setBackgroundColor(getResources().getColor(R.color.android_green));
                        showFactButton.setTextColor(getResources().getColor(R.color.android_green));
                    } break;

                    case 1: { // in case it is 1, show fact number 2 with orange color
                        factLabel.setText(R.string.fact2);
                        backGround.setBackgroundColor(getResources().getColor(R.color.orangish));
                        showFactButton.setTextColor(getResources().getColor(R.color.orangish));
                    } break;
                }

                factLabel.setText(randomNumber); // show the number generated with randomNumber, ONLY FOR DEBUG PURPOSES

            }
        };

        showFactButton.setOnClickListener(myListener);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.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();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

My expectation from the app is that it will show a different fact and change the color (now, for debugging, I want to see the text changes to the random number that was generated and that the color has changed)

Why isn't it working as I expect?

2

There are 2 answers

1
Blackbelt On BEST ANSWER

the problem is here is this line: factLabel.setText(randomNumber);. You want to set the String representing your randmNumber. Use

factLabel.setText(String.valueOf(randomNumber)); 

the version of setText you are using looks up for a String with id the int your provided. If this can't be found, it throws that exception

1
Gabriella Angelova On

Check all of the resources you want to use as R.color.<something> or R.string.<something> . According to the number of the line where is your exception (MainActivity.java:56 I think) you could find which resource exactly is not pressent in your app and go to the certain file (colors.xml, strings.xml or etc.) and add it