简体   繁体   中英

Why won't Log.i work?

Here's my code. I want to make sure that randomNumber remains the same until the user chooses the right number, so I'm trying to display that number via Log.i. However, whenever my button is clicked, nothing is printed to the android monitor.

int randomNumber = (int) (Math.random() * 50) + 1;

public void checkNumber(View view) {
    Log.i("Number", Integer.toString(randomNumber));
    EditText numberEntered = (EditText) findViewById(R.id.numberEntered);
    int numberEnteredInt = Integer.parseInt(numberEntered.getText().toString());
    if(!(numberEnteredInt <= 50 && numberEnteredInt >= 1)) {
        //some code
    } else if(numberEnteredInt < randomNumber) {
        //some code
    } else if(numberEnteredInt > randomNumber) {
        //some code
    } else {
        //some code
        randomNumber = (int) (Math.random() * 50) + 1;
    }
}

Just by running this app multiple times, it seems that the rest of my code is working properly (so I don't actually need Log to work in this case), but I am curious why nothing is being displayed to the monitor. Thanks.

Update: Ok, now it's working perfectly; I don't know what was happening before. I noticed a couple of you recommended using "" + randomNumber instead of Integer.toString(randomNumber). Is the former more efficient? Thanks again.

Are you sure checkNumber() is called in your onClickListener? If so, try adding \\n to the Log message.

    Log.i("Number", "\n" + randomNumber);

I don't believe you even need to use the Integer.toString() at all. You can just replace that whole line with:

Log.i("NUMBER", "" + randomNumber);

If you are calling the checkNumber() method as the onClick for the button, it should absolutely be printing to the console. Make sure your console is set to DEBUG mode, and that the line above the printouts doesn't read "No debuggable applications", but instead says the name of your app.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM