如何在另一种方法中使用变量?

时间:2017-04-23 20:09:21

标签: java android

我有变量int myInt的基本问题。在void aktualizujRynek()中,我为myInt赋值2。在void setImage()中,变量的值为0,而不是2.为什么?

public class MainActivity extends AppCompatActivity {

int myInt;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

aktualizujRynek();
setImage();

}


public void aktualizujRynek(){
final Thread thread1 = new Thread(new Runnable() {
myInt = 2;
});
thread1.start();
}


public void setImage(){
if (myInt ==2){  
//do something
}
}


}

2 个答案:

答案 0 :(得分:0)

您应该覆盖import Data.List (elemIndex) after xs x y = case x `elemIndex` xs of Just i -> y `elem` (drop (i + 1) xs) Nothing -> False 方法并将代码放在此方法中。从run()致电setImage()以获得正确的结果。

试试这个:

run()

希望这会起作用〜

答案 1 :(得分:-1)

我想使用voidsetImage()设置我的图像,当满足2个条件时,if(myInt1 == 2&& myInt2 == 3)。但是当我在void setImage中使用myInt1和myInt2时,它们的值为0,尽管我赋值为2和3。

public class MainActivity extends AppCompatActivity {

int myInt1;
int myInt2;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

aktualizujRynek();
aktualizujPark();
setImage();

}


public void aktualizujRynek(){
final Thread thread1 = new Thread(new Runnable() {
    @Override
    public void run() {
        myInt1  = 2; // in my real code i take this value from HTTP response

    }
});

thread1.start();
}

public void aktualizujPark(){
final Thread thread2 = new Thread(new Runnable() {
    @Override
    public void run() {
        myInt2  = 3; // in my real code i take this value from HTTP response

    }
});

thread2.start();
}


public void setImage(){
if (myInt1 == 2  && myInt2 == 3){  
//do something
}
}


}