为什么符号 ‘add’ 无法解析?

huangapple go评论85阅读模式
英文:

Why the symbol 'add' cannot be resolved?

问题

我想创建一个数组列表`add`方法不起作用

package com.zaination.listview;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ListView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

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

    ListView VarListView = (ListView) findViewById(R.id.ListView);
    ArrayList<String> VarArrayList = new ArrayList<String>();
    VarArrayList.add("Zain");
    VarArrayList.add("Sarmad");
    VarArrayList.add("Aanish");
    VarArrayList.add("Haider");

}
英文:

I want to make an array list, but the add method is not working.

package com.zaination.listview;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ListView;

import java.util.ArrayList;


public class MainActivity extends AppCompatActivity {

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

    ListView VarListView = (ListView) findViewById(R.id.ListView);
    ArrayList&lt;String&gt; VarArrayList = new ArrayList&lt;String&gt;();
    VarArrayList.add(&quot;Zain&quot;);
    VarArrayList.add(&quot;Sarmad&quot;);
    VarArrayList.add(&quot;Aanish&quot;);
    VarArrayList.add(&quot;Haider&quot;);

}

为什么符号 ‘add’ 无法解析?

答案1

得分: 1

如果您将该代码放入onCreate(或任何其他)方法中,它可能会正常工作。

英文:

If you would put that code into onCreate (or any other) method it would probably work.

答案2

得分: 0

需要按照以下方式进行更新

```java
package com.zaination.listview;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

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

        ListView varListView = (ListView) findViewById(R.id.ListView);
        ArrayList<String> varArrayList = new ArrayList<String>();
        varArrayList.add("Zain");
        varArrayList.add("Sarmad");
        varArrayList.add("Aanish");
        varArrayList.add("Haider");
    }
}

另外,还有一个提示,属性名 VarArrayList 不遵循 Java 命名规范,应该以小写字母 varArrayList 开头。


<details>
<summary>英文:</summary>

it&#39;s need to be updated like this.


    package com.zaination.listview;

    import androidx.appcompat.app.AppCompatActivity;

    import android.os.Bundle;
    import android.widget.ListView;

    import java.util.ArrayList;

    public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
    ListView VarListView = (ListView) findViewById(R.id.ListView);
    ArrayList&lt;String&gt; VarArrayList = new ArrayList&lt;String&gt;();
    VarArrayList.add(&quot;Zain&quot;);
    VarArrayList.add(&quot;Sarmad&quot;);
    VarArrayList.add(&quot;Aanish&quot;);
    VarArrayList.add(&quot;Haider&quot;);
    }
}


also another hint attr name `VarArrayList` not follow java naming convention, should start with small letter `varArrayList`

</details>



# 答案3
**得分**: 0

你把代码写在了任何方法的外部。它必须在任何一种方法中。

最简单的方法是直接写在onCreate方法中:

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

    ListView varListView = findViewById(R.id.ListView);
    ArrayList<String> varArrayList = new ArrayList<String>();
    varArrayList.add("Zain");
    varArrayList.add("Sarmad");
    varArrayList.add("Aanish");
    varArrayList.add("Haider");
}

PS
还有一些小的改进(也部分被Android Studio推荐)你可以进行:

  • 你可以省略掉(ListView)的强制转换,因为它是多余的。
  • 你可以在初始化时省略掉ArrayList的标识符string
  • 你应该按照小驼峰命名法的约定(如varArrayList)来命名你的对象:

为什么符号 ‘add’ 无法解析?

英文:

You wrote the code outside of any method. It has to be in any kind of method.

The easiest way is to write it directly into the onCreate method:

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

    ListView VarListView = (ListView) findViewById(R.id.ListView);
    ArrayList&lt;String&gt; VarArrayList = new ArrayList&lt;String&gt;();
    VarArrayList.add(&quot;Zain&quot;);
    VarArrayList.add(&quot;Sarmad&quot;);
    VarArrayList.add(&quot;Aanish&quot;);
    VarArrayList.add(&quot;Haider&quot;);
}

PS:
There are some small improvements (also partly recommended by Android Studio) you could do:

  • you can ommit the cast (ListView) because it's redundant
  • you can ommit the ArrayList identifier string on initialization
  • you should name your objects (like VarArrayList) after the lower camel case naming convention (varArrayList):

<img src="https://i.stack.imgur.com/rRRce.png" width="250">

huangapple
  • 本文由 发表于 2020年8月30日 16:35:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63655541.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定