我正在尝试为微调器制作适配器,但是我在 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