The local variable xxx is never read

后端 未结 6 1631
不思量自难忘°
不思量自难忘° 2021-01-29 13:24

This is my code:

boolean startGameBoolean;
Bundle extras = getIntent().getExtras();
extras.getInt(\"startGameBoolean\");
if (startGameBoolean = true){
    counte         


        
相关标签:
6条回答
  • 2021-01-29 13:38

    I had this same problem developing for Android.

    The reason you're seeing those warnings saying the "variable is never read" is because you're assigning it RIGHT AFTER creating it. Try creating the variable at the start of the class, then assigning it in an OnCreate() or other appropriate method that gets called when the class is first created.

    0 讨论(0)
  • 2021-01-29 13:44

    Shouldn't it be startGameBoolean = extras.getBoolean("startGameBoolean");?

    in the code you gave, boolean startGameBoolean; is not used anywhere. the warning means that although you declared it, it is not used in the block where it lives, therefore could (should) be removed.

    Edit:

    After seeing your addition, you use startGameBoolean, but you assign to it, while you should compare to it:

    if (startGameBoolean == true){
    //                    ^
        counter.start();
    }
    

    Also, assign the result of getBoolean() to the variable as I wrote in the first statement.

    0 讨论(0)
  • 2021-01-29 13:54

    I would venture to agree with Eclipse, it's not being used. The code you've show us doesn't have it being used anywhere.

    Update

    The variable is still never used because you assign it in your if condition; you never compare it (which is what it looks like you're trying to do).

    Change

    if (startGameBoolean = true){
    

    to

    if (startGameBoolean){
    
    0 讨论(0)
  • 2021-01-29 14:01
    Bundle extras = getIntent().getExtras();
    boolean startGameBoolean = extras.getBoolean("startGameBoolean");
    

    Something like this? You cant fetch boolean with getInt(). Perhaps, if you are dealing with int but with values 0 and 1. Then you should change the type of startGameBoolean to int and fetch it with getInt(), people do this sometimes.

    0 讨论(0)
  • 2021-01-29 14:05
    if (startGameBoolean = true){
    

    Should be..

    if (startGameBoolean == true){
    

    ..or better still..

    if (startGameBoolean){
    
    0 讨论(0)
  • 2021-01-29 14:05

    I had a similar error... for this line

    JLabel label = new JLabel("You have $" + amountInAccount + " in your account.");

    I used Quick fix (Ctrl+1) and chose Remove 'label', keep side-effect assignments...

    and the code became

    new JLabel("You have $" + amountInAccount + " in your account.");

    0 讨论(0)
提交回复
热议问题