Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/359.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法解析为变量,在何处声明它_Java_Android - Fatal编程技术网

Java 无法解析为变量,在何处声明它

Java 无法解析为变量,在何处声明它,java,android,Java,Android,我无法将解析为变量,我知道它的用途,但不知道如何修复它。我必须在其他地方声明它吗?在哪里 我有这个: 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(

我无法将
解析为变量,我知道它的用途,但不知道如何修复它。我必须在其他地方声明它吗?在哪里

我有这个:

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);

        }

由于您在
calculeaza()
中声明了
cant
,因此无法在
salveaza()
中使用它。如果要在方法之间共享它,则应该在外部将其声明为


您可以在此处了解有关Java作用域的更多信息:

使用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();
        } 
    }

 }
公共类MyClass{

private ArrayList cant;//如果要在其他方法中使用调用的字符串数组cant
String[]cant=new String[allcant.size()];
,则不能在方法中声明它


在一个方法中声明一个变量会使它成为一个局部方法,这意味着它只存在于该方法中,从外部看不到或无法使用。您最好的选择是将它声明为一个实例变量。

@Nambari这将很难,因为代码无法编译!哪一行显示了错误?@Simon:IDE可能是什么意思显示错误,我们需要行号详细信息等。问题是无法[]是在public void calculeaza中声明的,我正试图在public void salveaza中使用它,对吗?我必须在范围之外声明它?你能给我一个简单的例子说明我怎么做吗?谢谢!@SorinGrecu这就是为什么他发布了一个链接。在你的类内部,但在你的方法外部,键入
private String[]cant=new String[allcant.size()];
。您需要确保
allcant
也是一个实例变量。我现在得到
赋值的左侧必须是一个变量
at
cant.get(j)=Float.toString(0);
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();
        } 
    }

 }