It is necessary that the application calculates the arithmetic average population countries selected by the user. But now the application only displays in main3activity 0.

 public class Main2Activity extends AppCompatActivity implements View.OnClickListener { String[] countries = {"Urugay", "Paraguay", "Jamaica", "Peru", "Mexico"}; int[] population = {6770000, 2300000, 500000, 6310000, 7000000}; Button btnSubmit; int sum; ListView countriesList; int average; @RequiresApi(api = Build.VERSION_CODES.N) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); Map<String, Integer> countryData = new HashMap<>(countries.length); for (int i = 0; i < countries.length; i++) { countryData.put(countries[i], population[i]); } countryData.forEach((key, value) -> System.out.println(key + " -> " + value)); ListView countriesList = findViewById(R.id.countriesList); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, countries); countriesList.setAdapter(adapter); int sum = 0; for (int i = 0; i < population.length; i++) { sum += population[i]; average = sum / population.length; } btnSubmit = findViewById(R.id.btnSubmit); btnSubmit.setOnClickListener(this); } @Override public void onClick(View view) { Intent intent = new Intent(this, Main3Activity.class); intent.putExtra("average", average); startActivity(intent); } } 

Main3Activity

 public class Main3Activity extends AppCompatActivity { TextView tvView; int average; int defaultValue = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main3); tvView = (TextView) findViewById(R.id.tvView); Intent mIntent = getIntent(); average = mIntent.getIntExtra("average", 0); tvView.setText("Average: " + average); } } 

Tell me what's wrong?

  • Try instead of average = mIntent.getIntExtra("average", 0); use average = mIntent.getExtras().getInt("average", 0); - YurySPb
  • @Yuriy SPb, in main3activity everything is exactly the application produces "Average: 0" - Ahill
  • why do you calculate the "average" in a cycle? You copied your code exactly? output, for example, in Toast, your arithmetic average over the button, before putExtra. - Andrey Mihalev
  • @AndreyMihalev, you need to pass the average value of the number of population selected through the multiplechoice of countries in main3activity. Toast is no good - Ahill
  • one
    What does he write to the glass when "throwing out"? - pavlofff

2 answers 2

You need to learn what are local variables and class variables. The list of countries and the adapter that you create in the onCreate method is not the same as what you declared as class variables. In the end, it turns out that in the onClick method you refer to class variables, but they are empty (null), since you created onCreate other variables and put values ​​there, which led you to a NullPointerException. If you want to use class variables, then in the onCreate method you need to remove the data type.

The onClick method contains a lot of things that are not needed to solve your problem. Here I am a little changed. At the beginning, check that the user has selected at least one country, if you do not, then you will catch zero when pressed.

  public void onClick(View view) { if (countriesList.getCheckedItemCount() == 0) { Toast.makeText(this, "Выберите стары!", Toast.LENGTH_SHORT).show(); } else { SparseBooleanArray checked = countriesList.getCheckedItemPositions(); int sum = 0; for (int i = 0; i < checked.size(); i++) { if (checked.valueAt(i)) { sum += population[checked.keyAt(i)]; } } average = sum / countriesList.getCheckedItemCount(); Intent intent = new Intent(this, Main3Activity.class); intent.putExtra("KEY_AVERAGE", average); startActivity(intent); } } 
  • And most importantly, I am also just learning, so you should not take my answer as the ultimate truth) - Andry Max

Replace:

 intent.putExtra("KEY_AVERAGE", average); 

on:

 intent.putExtra(KEY_AVERAGE, average);