簡體   English   中英

無法解析為變量,可以在哪里聲明

[英]cannot be resolved to a variable,where to declare it

cant cannot be resolved to a variable ,我知道它的用途,但是我不知道如何解決它。我是否必須在其他地方聲明它? 在哪

我有這個 :

public void calculeaza() {

    totaltest = 0;
    String[] cant = new String[allcant.size()];

    for (int j = 0; j < allcant.size(); j++) {

        cant[j] = allcant.get(j).getText().toString();
        if (cant[j].matches("")) {
            Toast.makeText(this,
                    "Ati omis cantitatea de pe pozitia " + (j + 1),
                    Toast.LENGTH_SHORT).show();
            cant[j] = Float.toString(0);

        }

和這個 :

public void salveaza(){
    try {

        File myFile = new File("/sdcard/mysdfile.txt");
        myFile.createNewFile();
        FileOutputStream fOut = new FileOutputStream(myFile);
        OutputStreamWriter myOutWriter = 
                                new OutputStreamWriter(fOut);
        myOutWriter.append(cant[1]);
        myOutWriter.close();
        fOut.close();
        Toast.makeText(getBaseContext(),
                "Done writing SD 'mysdfile.txt'",
                Toast.LENGTH_SHORT).show();
    } catch (Exception e) {
        Toast.makeText(getBaseContext(), e.getMessage(),
                Toast.LENGTH_SHORT).show();
    }
}

既然你宣告cantcalculeaza()你不能用它salveaza() 如果要在方法之間共享它,則應在外部將其聲明為實例變量

您可以在此處了解有關Java范圍的更多信息: Java編程:5變量范圍

使用ArrayList代替String []並將其聲明為類字段。

public class MyClass{

    private ArrayList<String> cant;  // <---- accessible by all methods in the class.

    public void calculeaza() {

        cant = new ArrayList<String>();

        for (int j = 0; j < allcant.size(); j++) {

              cant.add(allcant.get(j).getText().toString());

             if (cant.get(j).matches("")) {
                 Toast.makeText(this,
                      "Ati omis cantitatea de pe pozitia " + (j + 1),
                      Toast.LENGTH_SHORT).show();
                 cant.get(j) = Float.toString(0);

             }
        ....

     public void salveaza(){ 

        try {

            File myFile = new File("/sdcard/mysdfile.txt");
            myFile.createNewFile();
            FileOutputStream fOut = new FileOutputStream(myFile);
            OutputStreamWriter myOutWriter = 
                               new OutputStreamWriter(fOut);
            myOutWriter.append(cant[1]);
            myOutWriter.close();
            fOut.close();
            Toast.makeText(getBaseContext(),
                "Done writing SD 'mysdfile.txt'",
                Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            Toast.makeText(getBaseContext(), e.getMessage(),
                Toast.LENGTH_SHORT).show();
        } 
    }

 }

有很多更好的方法可以做到這一點,但這可以解決您的問題。 使用ArrayList是因為它比嘗試在類級別初始化數組要容易得多。

如果要使用字符串數組,則可以調用cant String[] cant = new String[allcant.size()]; 在不同的方法中,您不能在方法內部聲明它。

在方法內部聲明變量使其成為局部方法,這意味着它僅存在於該方法內,而無法從外部看到或使用。 您最好的選擇是將其聲明為實例變量。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM