我正在尝试使用ListView
填充ArrayAdapter
,我正在填充来自EditText
的输入。我的程序似乎编译正常,但在应用程序启动时它立即崩溃。我怀疑它与我试图设置我的列表视图有关。我不确定我初始化错误,以至于我的应用程序立即崩溃。任何和所有提示将不胜感激。提前谢谢!
这是我对ArrayList
和Adapter
的全局声明。
ArrayList<scoreScreen> savedScores = new ArrayList<>();
ScoreAdapter adapter = new ScoreAdapter(this, savedScores);
ListView listView = (ListView) findViewById(R.id.dispScores);
listView.setAdapter(adapter);
我的适配器类:
private class ScoreAdapter extends ArrayAdapter<scoreScreen> {
private ScoreAdapter(Context context, ArrayList<scoreScreen> scores) {
super(context, 0, scores);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
scoreScreen score1 = getItem(position);
if (convertView == null){
convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_scores, parent, false);
}
TextView holeNum = (TextView) convertView.findViewById(R.id.holeNum);
holeNum.setText(score1.hole);
return convertView;
}
}
我的ListView
方法中的onCreate
。
ListView listview = (ListView) findViewById(R.id.listView1);
listview.setAdapter(adapter);
我假设我的问题不在我的EditText
输入中,因为它们位于OnClickListener
方法内,但只是因为我已将其附加在下面。
public View.OnClickListener onClickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
EditText input1 = (EditText) findViewById(R.id.scorePrompt);
TextView output1 = (TextView) findViewById(R.id.textTotal);
String blankCheck = input1.getText().toString(); //CHANGE INPUT IN scorePrompt TO STRING
switch (view.getId()) {
case R.id.buttTotal:
if (blankCheck.equals("")) {
Toast blankError = Toast.makeText(getApplicationContext(), "YOU CANT SKIP HOLES JERK", Toast.LENGTH_LONG);
blankError.show();
break;
} else {
int num1 = Integer.parseInt(input1.getText().toString()); //Get input from text box
int sum = num1 + score2;
score2 = sum;
output1.setText("Your score is : " + Integer.toString(sum));
//savedScores.add(input1.getText().toString());
scoreScreen addScore = new scoreScreen("Score is" + num1);
adapter.add(addScore);
j++;
input1.setText(""); //Clear input text box
break;
}
case R.id.allScores: //CHANGE THIS TO AN EDIT BUTTON, ADD A HOLE NUMBER COUNTER AT TOP OF SCREEN!!!!!
output1.setText("you messed up");
break;
case R.id.editScore: //Need to set up Save Array before we can edit
//CURRENTLY ONLY DISPLAYS THE LAST NUNMBER IN THE TEXTEDIT, NEED TO SET UP LISTVIEW!!!!!!
for (int i=0; i < j; i++){
// output1.setText(savedScores.get(i));
} break;
}
}
};
按要求添加 onCreate
方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*Button scoresAct = (Button) findViewById(R.id.allScores); //THIS IS TO GO TO ALL SCORES ACTIVITY
scoresAct.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent scoreScreen = new Intent(MainActivity.this, AllScoresAct.class);
startActivity(scoreScreen);
}
});*/
Button sumScores = (Button) findViewById(R.id.buttTotal);
Button saveScores = (Button) findViewById(R.id.allScores);
Button changeScores = (Button) findViewById(R.id.editScore);
sumScores.setOnClickListener(onClickListener);
saveScores.setOnClickListener(onClickListener);
changeScores.setOnClickListener(onClickListener);
ListView listview = (ListView) findViewById(R.id.listView1);
listview.setAdapter(adapter);
}
将我的适配器和ArrayList移动到我的onCreate
后,我收到一个新错误。我做了一些关于空指针的研究,但我已经初步化了这两个。下面是我的logcat,有什么想法吗?感谢
答案 0 :(得分:1)
确保super.onCreate(savedInstanceState)
是你在onCreate()中调用的第一件事,并且在setContentView()
之前也没有尝试从布局xml访问任何视图。
编辑:
使用onCreate()
方法初始化适配器,而不是全局地执行。
onCreate() {
.......
this.adapter = new ScoreAdapter(this, savedScores);
}
答案 1 :(得分:0)
这是我的ArrayList和Adapter的全局声明。
ArrayList<scoreScreen> savedScores = new ArrayList<>(); ScoreAdapter adapter = new ScoreAdapter(this, savedScores);
您可以全局声明,但对于适配器,必须在this
之内/之后使用onCreate
参数,因为错误显示
在onCreate()
之前,活动无法使用系统服务
例如
private ArrayList<ScoreScreen> savedScores = new ArrayList<>();
private ScoreAdapter adapter;
@Override
protected void onCreate(Bundle b) {
...
adapter = new ScoreAdapter(this, savedScores);
}