英文:
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<String> VarArrayList = new ArrayList<String>();
VarArrayList.add("Zain");
VarArrayList.add("Sarmad");
VarArrayList.add("Aanish");
VarArrayList.add("Haider");
}
答案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'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<String> VarArrayList = new ArrayList<String>();
VarArrayList.add("Zain");
VarArrayList.add("Sarmad");
VarArrayList.add("Aanish");
VarArrayList.add("Haider");
}
}
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
)来命名你的对象:
英文:
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<String> VarArrayList = new ArrayList<String>();
VarArrayList.add("Zain");
VarArrayList.add("Sarmad");
VarArrayList.add("Aanish");
VarArrayList.add("Haider");
}
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">
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论