gpt4 book ai didi

java - 微调器导致应用程序崩溃

转载 作者:行者123 更新时间:2023-11-30 09:08:20 25 4
gpt4 key购买 nike

我是 Stackoverflow 的新手,也是 Android 编程的新手

我在我的 AndroidApp 中完成了这段代码:

public class MainActivity extends ActionBarActivity {

private Spinner spinner;
private LinkedList<Integer> list;

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

if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}

this.ageSpinner();


}

private void ageSpinner(){

list = new LinkedList<Integer>();
spinner = (Spinner) this.findViewById(R.id.spinner1);

for(Integer i=0; i<=4; i++){


list.add(i);

}

ArrayAdapter<Integer> spinnerAdapter = new ArrayAdapter<Integer>(this, android.R.layout.simple_spinner_item, list);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

spinner.setAdapter(spinnerAdapter);

}

我的 logCat 抛出一个 NullPointerException

我尝试执行此代码,将 Linkelist 更改为 ArrayList、String Array,但我的应用程序崩溃了。

最佳答案

试试这段代码

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

if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
spinner = (Spinner)findViewById(R.id.spinner1);
ageSpinner();
ArrayAdapter<Integer> spinnerAdapter = new ArrayAdapter<Integer>(this, android.R.layout.simple_spinner_item, list);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerAdapter);
}
private void ageSpinner(){

list = new ArrayList<Integer>();
for(int i=0; i<=4; i++){
list.add(i);


}

关于java - 微调器导致应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23584409/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com