我正在尝试为spinners创建适配器,但是我在answertime.setAdapter(adapter)上得到了NullPointerException;为什么是这样?我的数组在strings.xml中应该是什么? 这是完整的代码。
package lijap.app.starcraft2counters;
import java.io.File;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
public class Settings extends Activity implements OnItemSelectedListener {
Spinner answertime;
Spinner gametime;
Spinner missesallowed;
String[] answerseconds = { "Unlimited", "1 second", "2 seconds",
"3 seconds", "4 seconds", "5 seconds" };
String[] gameminutes = { "Unlimited", "1 minute", "2 minutes", "3 minutes",
"4 minutes", "5 minutes" };
String[] numberofmisses = { "Unlimited", "5", "10", "15", "20", "25" };
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.AnswerSeconds, R.layout.custom_spinner_list);
adapter.setDropDownViewResource(R.layout.custom_spinner);
answertime.setAdapter(adapter);
ArrayAdapter adapter2 = ArrayAdapter.createFromResource(this, R.array.GameMinutes, R.layout.custom_spinner_list);
adapter.setDropDownViewResource(R.layout.custom_spinner);
gametime.setAdapter(adapter);
ArrayAdapter adapter3 = ArrayAdapter.createFromResource(this, R.array.NumberOfMisses, R.layout.custom_spinner_list);
adapter.setDropDownViewResource(R.layout.custom_spinner);
missesallowed.setAdapter(adapter);
answertime = (Spinner) findViewById(R.id.s_answertime);
answertime.setAdapter(adapter);
answertime.setOnItemSelectedListener(this);
missesallowed = (Spinner) findViewById(R.id.s_mistakenumber);
missesallowed.setAdapter(adapter2);
missesallowed.setOnItemSelectedListener(this);
gametime = (Spinner) findViewById(R.id.s_gametime);
gametime.setAdapter(adapter3);
gametime.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
感谢任何帮助 - Lijap
答案 0 :(得分:3)
您需要初始化
Spinner answertime;
Spinner gametime;
Spinner missesallowed;
否则,以下行会为您提供NullPointerException
:
answertime.setAdapter(adapter);
gametime.setAdapter(adapter);
missesallowed.setAdapter(adapter);
要初始化Spinner
,您应该在致电setContentView
后添加以下内容:
answertime = findViewById(R.id.answertime);
gametime = findViewById(R.id.gametime);
missesallowed = findViewById(R.id.missesallowed);
答案 1 :(得分:2)
您永远不会初始化answertime
,gametime
或missesallowed
。你需要做这样的事情,
answertime = findViewById(R.id.<the id for this spinner>);
gametime = findViewById(R.id.<the id for this spinner>);
missesallowed = findViewById(R.id.<the id for this spinner>);
之前打电话给setAdapter()
。
另外,这是另一个问题,
ArrayAdapter adapter2 = ArrayAdapter.createFromResource(this, R.array.GameMinutes, R.layout.custom_spinner_list);
adapter.setDropDownViewResource(R.layout.custom_spinner);
gametime.setAdapter(adapter);
请注意,您在引用adapter2
中构造了一些内容,然后修改了adapter
,并将adapter
设置为微调器的适配器。这就是你想要的吗?
答案 2 :(得分:0)
还要确保将视图可见性设置为View.VISIBLE