该应用程序必须计算所选国家/地区的人口平均值。现在,当用户单击“ btnSubmit”按钮时,该应用程序将停止。代码有什么问题?以及如何使正确?
public class Main2Activity extends AppCompatActivity implements View.OnClickListener {
public static final String KEY_AVERAGE = "average";
String[] countries = {"Urugay", "Paraguay", "Jamaica", "Peru", "Mexico"};
int[] population = {6770000, 2300000, 500000, 6310000, 7000000};
Button btnSubmit;
int sum;
ListView countriesList;
int average;
ArrayAdapter<String> adapter;
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]);
}
“国家”和“人口”相互关联
ListView countriesList = findViewById(R.id.countriesList);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, countries);
countriesList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
countriesList.setAdapter(adapter);
多选适配器
btnSubmit = findViewById(R.id.btnSubmit);
btnSubmit.setOnClickListener(this);
}
public void onClick(View view) {
SparseBooleanArray checked = countriesList.getCheckedItemPositions();
ArrayList<String> selectedItems = new ArrayList<String>();
for (int i = 0; i < checked.size(); i++) {
int position = checked.keyAt(i);
if (checked.valueAt(i))
selectedItems.add(adapter.getItem(position));
所选位置的数组
}
String[] outputStrArr = new String[selectedItems.size()];
for (int i = 0; i < selectedItems.size(); i++) {
outputStrArr[i] = selectedItems.get(i);
}
int sum = 0;
for (int i = 0; i < population.length; i++)
{
sum += population[i];
average = sum/population.length;
}
Intent intent = new Intent(this, Main3Activity.class);
intent.putExtra("KEY_AVERAGE", average);
startActivity(intent);
}
}
日志
10-07 01:11:12.150 2034-2049/com.example.admin.app1 W/art: Suspending all threads took: 27.225ms
10-07 01:11:12.159 2034-2049/com.example.admin.app1 I/art: Background partial concurrent mark sweep GC freed 2226(140KB) AllocSpace objects, 0(0B) LOS objects, 50% free, 996KB/2020KB, paused 29.733ms total 233.034ms
10-07 01:11:12.207 2034-2034/com.example.admin.app1 W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
10-07 01:11:12.380 2034-2034/com.example.admin.app1 I/art: Rejecting re-init on previously-failed class java.lang.Class<android.support.v4.view.ViewCompat$OnUnhandledKeyEventListenerWrapper>
10-07 01:11:12.414 2034-2034/com.example.admin.app1 I/art: Rejecting re-init on previously-failed class java.lang.Class<android.support.v4.view.ViewCompat$OnUnhandledKeyEventListenerWrapper>
10-07 01:11:12.609 2034-2049/com.example.admin.app1 I/art: Background sticky concurrent mark sweep GC freed 597(31KB) AllocSpace objects, 0(0B) LOS objects, 16% free, 1680KB/2020KB, paused 11.964ms total 34.524ms
10-07 01:11:12.756 2034-2034/com.example.admin.app1 I/Choreographer: Skipped 61 frames! The application may be doing too much work on its main thread.
10-07 01:11:12.932 2034-2056/com.example.admin.app1 W/EGL_emulation: eglSurfaceAttrib not implemented
10-07 01:11:12.932 2034-2056/com.example.admin.app1 W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xe31543a0, error=EGL_SUCCESS
10-07 01:11:13.252 2034-2034/com.example.admin.app1 W/art: Before Android 4.1, method int android.support.v7.widget.DropDownListView.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
10-07 01:11:13.776 2034-2034/com.example.admin.app1 I/Choreographer: Skipped 30 frames! The application may be doing too much work on its main thread.
10-07 01:11:17.860 2034-2034/com.example.admin.app1 D/AndroidRuntime: Shutting down VM
10-07 01:11:17.861 2034-2034/com.example.admin.app1 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.admin.app1, PID: 2034
java.lang.NullPointerException: Attempt to invoke virtual method 'android.util.SparseBooleanArray android.widget.ListView.getCheckedItemPositions()' on a null object reference
at com.example.admin.app1.Main2Activity.onClick(Main2Activity.java:52)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19749)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)