I'm working on an application that retrieve data from users: age, height, weigth and type of fitness activities.
I didn't find any solution about this problem
There is a npe on getitemselected on a custom spinner (the fitness activities spinner).
My goal is to see the item selected in the spinner and set a value called "percentuale" to make some calculation that I'll push to another activity in order to display it on a pie chart (here I've another problem but I'll deal with that later).
Here is the code:
Button next;
RadioGroup radioGender;
RadioButton male;
RadioButton female;
ArrayAdapter<String> adapterFitness;
ArrayAdapter<String> adapterHour;
Spinner spinnerFitness;
Spinner spinnerHour;
private RadioButton maleButton;
private double percentuale;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
initialize();
setCustomSpinner(spinnerFitness, R.id.fitness, adapterFitness, R.layout.rowfitness, Fitness);
// DIALOG
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.welcome_page).setTitle("Ciao!");
builder.setPositiveButton("Avanti", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog dialog = builder.create();
dialog.show();
final EditText textAge = (EditText) findViewById(R.id.age);
final EditText textHeight = (EditText) findViewById(R.id.height);
final EditText textWeight = (EditText) findViewById(R.id.weight);
spinnerFitness.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// TODO Auto-generated method stub
String item = parent.getItemAtPosition(position).toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}});
// ON CLICK LISTENER
next.setOnClickListener(new OnClickListener() {
public void onClick(final View v) {
if (textAge.getText().toString().length() == 0 || textAge.getText().toString().length() >= 3) {
textAge.setError("L'età è un parametro fondamentale per impostare un dieta corretta");
} else if (textHeight.getText().toString().length() == 0
|| textHeight.getText().toString().length() >= 4) {
textHeight.setError("L'altezza è un parametro fondamentale per impostare un dieta corretta");
} else if (textWeight.getText().toString().length() == 0
|| textWeight.getText().toString().length() >= 4) {
textWeight.setError("Il peso è un parametro fondamentale per impostare un dieta corretta");
} else {
String spinVal = spinnerFitness.getSelectedItem().toString();
switch (spinVal) {
case "Atleta professionista":
percentuale = 88 / 100;
case "Atleta agonista":
percentuale = 72 / 100;
case "In forma":
percentuale = 56 / 100;
case "Soggetto attivo":
percentuale = 40 / 100;
case "Sedentario":
percentuale = 24 / 100;
default:
percentuale = 1;
}
final Intent openDiary = new Intent(v.getContext(), Diary.class);
final Bundle bundle = new Bundle();
final int age = Integer.parseInt(textAge.getText().toString());
final double weight = Double.parseDouble(textWeight.getText().toString());
final int height = Integer.parseInt(textHeight.getText().toString());
// Per MASCHIO
if (maleButton.isChecked()) {
double valueBas = 66.4730 + (13.7516 * weight) + (5.0033 * height) - (6.775 * age);
bundle.putDouble("valueBasale", valueBas);
bundle.putDouble("valueDCN", valueBas + percentuale * valueBas);
openDiary.putExtras(bundle);
}
// Per DONNA
else {
double valueBas = 655.095 + (9.5634 * weight) + (1.8496 * height) - (4.6756 * age);
bundle.putDouble("valueBasale", valueBas);
bundle.putDouble("valueDCN", valueBas + percentuale * valueBas);
openDiary.putExtras(bundle);
}
startActivity(openDiary);
}
}
});
}
// INITIALIZE
public void initialize() {
next = (Button) findViewById(R.id.buttonNext);
maleButton = (RadioButton) findViewById(R.id.male);
radioGender = (RadioGroup) findViewById(R.id.gender);
radioGender.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
}
});
}
// SET SPINNER
public void setCustomSpinner(Spinner spinner, int id, ArrayAdapter<String> adapter, int layout, String[] list) {
adapter = new MyCustomAdapter(this, layout, list);
spinner = (Spinner) findViewById(id);
spinner.setAdapter(adapter);
spinner.setSelection(0);
}
public void setSpinner(Spinner spinner, int id, ArrayAdapter<String> adapter, int layout, String[] list) {
adapter = new ArrayAdapter<>(this, layout, list);
spinner = (Spinner) findViewById(id);
spinner.setAdapter(adapter);
spinner.setSelection(0);
}
// SPINNERS ITEM
private static final String[] Fitness = new String[] { "Atleta professionista", "Atleta agonista", "In forma",
"Soggetto attivo", "Sedentario" };
// CUSTOM ADAPTER
public class MyCustomAdapter extends ArrayAdapter<String> {
public MyCustomAdapter(Context context, int textViewResourceId, String[] objects) {
super(context, textViewResourceId, objects);
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
public View getCustomView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View rowFitness = inflater.inflate(R.layout.rowfitness, parent, false);
TextView labelFitness = (TextView) rowFitness.findViewById(R.id.atletaPro);
labelFitness.setText(Fitness[position]);
ImageView iconFitness = (ImageView) rowFitness.findViewById(R.id.iconFitness);
if (Fitness[position] == "Atleta professionista") {
iconFitness.setImageResource(R.drawable.clothing548);
} else {
iconFitness.setImageResource(R.drawable.flexions);
}
return rowFitness;
}
}
stack trace
02-26 12:20:58.438: I/art(1807): Not late-enabling -Xcheck:jni (already on) 02-26 12:21:05.377: D/OpenGLRenderer(1807): Render dirty regions requested: true 02-26 12:21:05.379: D/(1807): HostConnection::get() New Host Connection established 0xa6e9e7c0, tid 1807 02-26 12:21:05.382: D/Atlas(1807): Validating map... 02-26 12:21:05.418: D/(1807): HostConnection::get() New Host Connection established 0xa6e9e220, tid 1832 02-26 12:21:05.429: I/OpenGLRenderer(1807): Initialized EGL, version 1.4 02-26 12:21:05.456: D/OpenGLRenderer(1807): Enabling debug mode 0 02-26 12:21:05.466: W/EGL_emulation(1807): eglSurfaceAttrib not implemented 02-26 12:21:05.466: W/OpenGLRenderer(1807): Failed to set EGL_SWAP_BEHAVIOR on surface 0xa6e9a360, error=EGL_SUCCESS 02-26 12:21:09.784: W/EGL_emulation(1807): eglSurfaceAttrib not implemented 02-26 12:21:09.784: W/OpenGLRenderer(1807): Failed to set EGL_SWAP_BEHAVIOR on surface 0xa6e9a360, error=EGL_SUCCESS 02-26 12:21:10.483: D/AndroidRuntime(1807): Shutting down VM 02-26 12:21:10.483: D/AndroidRuntime(1807): --------- beginning of crash
02-26 12:21:10.483: E/AndroidRuntime(1807): FATAL EXCEPTION: main 02-26 12:21:10.483: E/AndroidRuntime(1807): Process: com.freealimentarycoach, PID: 1807 02-26 12:21:10.483: E/AndroidRuntime(1807): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.freealimentarycoach/com.freealimentarycoach.Welcome}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setOnItemSelectedListener(android.widget.AdapterView$OnItemSelectedListener)' on a null object reference 02-26 12:21:10.483: E/AndroidRuntime(1807): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298) 02-26 12:21:10.483: E/AndroidRuntime(1807): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 02-26 12:21:10.483: E/AndroidRuntime(1807): at android.app.ActivityThread.access$800(ActivityThread.java:144) 02-26 12:21:10.483: E/AndroidRuntime(1807): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 02-26 12:21:10.483: E/AndroidRuntime(1807): at android.os.Handler.dispatchMessage(Handler.java:102) 02-26 12:21:10.483: E/AndroidRuntime(1807): at android.os.Looper.loop(Looper.java:135) 02-26 12:21:10.483: E/AndroidRuntime(1807): at android.app.ActivityThread.main(ActivityThread.java:5221) 02-26 12:21:10.483: E/AndroidRuntime(1807): at java.lang.reflect.Method.invoke(Native Method) 02-26 12:21:10.483: E/AndroidRuntime(1807): at java.lang.reflect.Method.invoke(Method.java:372) 02-26 12:21:10.483: E/AndroidRuntime(1807): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 02-26 12:21:10.483: E/AndroidRuntime(1807): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 02-26 12:21:10.483: E/AndroidRuntime(1807): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setOnItemSelectedListener(android.widget.AdapterView$OnItemSelectedListener)' on a null object reference 02-26 12:21:10.483: E/AndroidRuntime(1807): at com.freealimentarycoach.Welcome.onCreate(Welcome.java:62) 02-26 12:21:10.483: E/AndroidRuntime(1807): at android.app.Activity.performCreate(Activity.java:5937) 02-26 12:21:10.483: E/AndroidRuntime(1807): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) 02-26 12:21:10.483: E/AndroidRuntime(1807): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251) 02-26 12:21:10.483: E/AndroidRuntime(1807): ... 10 more 02-26 12:21:12.246: I/Process(1807): Sending signal. PID: 1807 SIG: 9 02-26 12:21:12.504: W/art(2020): Suspending all threads took: 6.358ms 02-26 12:21:12.508: I/art(2020): Background sticky concurrent mark sweep GC freed 2102(126KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 3MB/3MB, paused 7.968ms total 81.135ms 02-26 12:21:12.562: D/OpenGLRenderer(2020): Render dirty regions requested: true 02-26 12:21:12.566: D/(2020): HostConnection::get() New Host Connection established 0xae2dfd40, tid 2020 02-26 12:21:12.568: D/Atlas(2020): Validating map... 02-26 12:21:12.574: D/AndroidRuntime(2020): Shutting down VM 02-26 12:21:12.574: E/AndroidRuntime(2020): FATAL EXCEPTION: main 02-26 12:21:12.574: E/AndroidRuntime(2020): Process: com.freealimentarycoach, PID: 2020 02-26 12:21:12.574: E/AndroidRuntime(2020): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.freealimentarycoach/com.freealimentarycoach.Welcome}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setOnItemSelectedListener(android.widget.AdapterView$OnItemSelectedListener)' on a null object reference 02-26 12:21:12.574: E/AndroidRuntime(2020): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298) 02-26 12:21:12.574: E/AndroidRuntime(2020): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 02-26 12:21:12.574: E/AndroidRuntime(2020): at android.app.ActivityThread.access$800(ActivityThread.java:144) 02-26 12:21:12.574: E/AndroidRuntime(2020): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 02-26 12:21:12.574: E/AndroidRuntime(2020): at android.os.Handler.dispatchMessage(Handler.java:102) 02-26 12:21:12.574: E/AndroidRuntime(2020): at android.os.Looper.loop(Looper.java:135) 02-26 12:21:12.574: E/AndroidRuntime(2020): at android.app.ActivityThread.main(ActivityThread.java:5221)