I’m building a calorie counting app. From the home screen, I’m trying to open the “Create entry” activity, but it crashes the instant I open it. I can’t tell why.
public class CreateEntryActivity extends AppCompatActivity {
private Spinner spinnerFood;
private EditText editTextGrams;
private EditText editTextDate;
private Button buttonSave;
private Button buttonCreateFood;
private List<Food> foods;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_entry);
Food apple = new Food("Apple", 0.25, 0.5, 0.1, "None",1);
foods.add(apple);
ArrayAdapter<Food> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, foods);
spinnerFood.setAdapter(adapter);
spinnerFood = findViewById(R.id.spinnerFoodEntry);
editTextGrams = findViewById(R.id.editTextGramsEntry);
editTextDate = findViewById(R.id.editTextDateEntry);
buttonSave = findViewById(R.id.buttonSaveEntry);
buttonCreateFood = findViewById(R.id.buttonCreateFood);
buttonCreateFood.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(CreateEntryActivity.this, CreateFoodActivity.class);
startActivity(intent);
Intent i = getIntent();
Food food = (Food) i.getSerializableExtra("FOOD_KEY_");
foods.add(food);
}
});
}
}
If I remove all the code and only leave the xml, the activity opens. I’m pretty much a newbie, can anyone tell me what I’m doing wrong, please?
New contributor
Tudor Lungu is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1