Random Direction displaying as null when added to array strings

时间:2017-04-24 17:28:24

标签: java android

Okay so I have a long array, in it some of the strings need a random direction string in the middle.

heres my class (kind of, I made this from scratch as an example):

public class Example extends AppCompatActivity implements View.OnClickListener{

String[] StringList = new String[3];
String[] Directions = new String[3];

int idy = new Random().nextInt(Directions.length);
String ranDirection = (Directions[idy]);

private TextView TextOut;

@Override
protected void onCreate(Bundle savedInstanceState) { //executes when the page is opened

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

        populateStringArray();
        populateDirectionArray();

        int idx = new SecureRandom().nextInt(StringList.length);
        String RandomTextOut = (StringList[idx]);

        TextOut = (TextView)findViewById(R.id.textView6);
        TextOut.setText(RandomTextOut);


    }

private void populateDirectionArray(){
    Directions[0] = "front";
    Directions[1] = "right";
    Directions[2] = "left";


}

private void populateStringArray(){

StringList[0] = "blah blah " + ranDirection + " blah blah";
StringList[1] = "blah blah stuff";
StringList[2] = "stuff and things " + ranDirection + " More stuff"
//I don't feel like adding more examples, my project has 150 strings in the array if that helps.

}

}//Class End

The problem is that when I view my item out of StringList, it would say "blah blah null blah blah"

instead of "blah blah front blah blah"

I tried putting

int idy = new Random().nextInt(Directions.length);
String ranDirection = (Directions[idy]);

Inside the onCreate method, that made it local and it was an error.

now theres no error, but it displays as null.

I also tried adding

String ranDirection;

to the start of the class and in the onCreate method:

ranDirection = (Directions[idy]);

to define it.

Nothing seems to work, I can't get it to display something other than null.

0 个答案:

没有答案