-1

I am trying to populate a ListView using an ArrayAdapter that I am filling with input from an EditText. My program seems to compile fine but during app start up it immediately crashes. I suspect that it has something to do with me trying to set-up my list view. I am not sure what I am initializing wrong such that my app instantly crashes. Any and all tips would be greatly appreciated. Thanks in advance!

This is my global declarations for my ArrayList and Adapter.

ArrayList<scoreScreen> savedScores = new ArrayList<>();
ScoreAdapter adapter = new ScoreAdapter(this, savedScores);

ListView listView = (ListView) findViewById(R.id.dispScores);
listView.setAdapter(adapter);

My Adapter Class:

 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;
    }
}

My ListView inside of my onCreate method.

 ListView listview = (ListView) findViewById(R.id.listView1);
    listview.setAdapter(adapter);

I am assuming my problem is not inside my EditText inputs since they are inside of an OnClickListener method, but just incase I have attached it below.

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 method added as requested:

@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);

}

After moving my adapter and ArrayList into my onCreate, I get a new error. I did some research on null pointers, but I have already initialized both of these. Below is my logcat, any ideas? Thanks

Raymond Nguyen
  • 65
  • 2
  • 10

2 Answers2

1

Make sure super.onCreate(savedInstanceState) is the first thing you are calling in your onCreate() and also not trying to access any view from the layout xml before setContentView().

EDIT:

Initialize the adapter in onCreate() method instead of doing it globally.

onCreate() {
   .......
   this.adapter = new ScoreAdapter(this, savedScores);
}
Satan Pandeya
  • 3,747
  • 4
  • 27
  • 53
Amit
  • 3,422
  • 3
  • 28
  • 39
0

This is my global declarations for my ArrayList and Adapter.

ArrayList<scoreScreen> savedScores = new ArrayList<>();
ScoreAdapter adapter = new ScoreAdapter(this, savedScores);

You can declare those globally, but for the Adapter, the this parameter must be used within/after onCreate, as the error says

System services not available to Activities before onCreate()

For example

private ArrayList<ScoreScreen> savedScores = new ArrayList<>();
private ScoreAdapter adapter;

@Override
protected void onCreate(Bundle b) {
    ...

    adapter = new ScoreAdapter(this, savedScores);
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • I have since moved both my `ArrayList` and my `adapter` into the my `onCreate` but the app is still crashing upon startup. Furthermore, I am receiving an error when I am attempting to add into my `ArrayList` during my `OnClickListener` method (this happens whether or not my `ArrayList` is declared inside of my `onCreate` or globally. Any ideas why? – Raymond Nguyen Aug 07 '17 at 03:51
  • Well, this specific code fixes the initial error. I can't answer why else your app crashes without seeing your stacktraces – OneCricketeer Aug 07 '17 at 04:05
  • I have added in the logcat to my main post. It seems like my ListArray was not properly initialized but I cannot figure out the reason. – Raymond Nguyen Aug 07 '17 at 05:24
  • Well, typically that error arises when you use findViewById on an XML layout that doesn't actually contain the id you're looking for. That's explained in the documentation of the findViewById method return types – OneCricketeer Aug 07 '17 at 06:28
  • 1
    Hi cricket! You are exactly right! I was going back and forth between my xml and java files and found that I was incorrectly referencing a `ListView` id in the wrong xml. – Raymond Nguyen Aug 07 '17 at 07:04