I'm trying to create a mock up of a workout app that tracks the sets and reps of an exercise.
Here is how the layout looks:
What I'm trying to do is when I press NEW it creates a new instance of Exercise is made. Exercise contains a list of Sets. Each set contains an integer variable called Reps. What I want to do is have an Exercise object created (in this instance a pullup) and then when I click NEW it will create a new Set to add to the list, and when I press the TAP button it will update the Rep value of the set. So eventually I want to end up with a list of Sets, each of which have a rep value.
For Example: Pullup : (5) (6) (5) (5) (3) Which is a pullup with five sets, the first set had 5 reps, the second had 6 reps, the third had 5 reps and so on.
Here is the code I have written:
Sets.java
package com.example.soufin.pullupsv3;
/**
* Created by Soufin on 6/26/2015.
*/
public class Sets {
// private int _id;
private int _reps;
//constructor
//Sets(int reps) {
// setReps(reps);
//}
//getter
public int getReps()
{
return this._reps;
}
//setter
public void setReps(int reps)
{
this._reps = reps;
}
}
Exercise.java
package com.example.soufin.pullupsv3;
import java.util.List;
import com.example.soufin.pullupsv3.Sets;
/**
* Created by Soufin on 6/26/2015.
*/
public class Exercise {
//public int ID;
public String _name;
public List<Sets> _sets;
//getter
public String getName()
{
return this._name;
}
//setter
public void setName(String name)
{
this._name = name;
}
//getter
public List getSets() {return this._sets; }
// setter
//public void setSets(int reps){this._sets.add(new Sets());}
}
Workout.java (the main file)
package com.example.soufin.pullupsv3;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.R.*;
import com.example.soufin.pullupsv3.Sets;
import com.example.soufin.pullupsv3.Exercise;
import org.w3c.dom.Text;
public class Workout extends ActionBarActivity {
Exercise pullup;
Button wNew;
Button wTap;
Button wEnd;
TextView displayText;
int newIndex;
int tapCount;
int[] score = new int[5];
boolean flag = false;
String result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_workout);
pullup = new Exercise();
pullup.setName("Pullup");
wNew = (Button) findViewById(R.id.newSetButton);
wTap = (Button) findViewById(R.id.tapButton);
wEnd = (Button) findViewById(R.id.endWorkoutButton);
displayText = (TextView) findViewById(R.id.displayScore);
wNew.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Sets tempSet = new Sets(); // create new Set on click of mNew
wTap.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tapCount += 1; // increment based on number of taps
}
});
tempSet.setReps(tapCount);
pullup._sets.add(tempSet); // add Set with reps (by taps) into List in Exercise
result = pullup.getSets().toString(); //store result
}
});
displayText.setText(result); //display result
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_workout, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Unfortunately, when I click the NEW button, the app crashes.
Here is what logcat says:
06-27 17:36:09.499 1236-1236/com.example.soufin.pullupsv3 D/dalvikvm﹕ Late-enabling CheckJNI
06-27 17:36:09.827 1236-1236/com.example.soufin.pullupsv3 I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
06-27 17:36:09.827 1236-1236/com.example.soufin.pullupsv3 W/dalvikvm﹕ VFY: unable to resolve virtual method 409: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
06-27 17:36:09.827 1236-1236/com.example.soufin.pullupsv3 D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
06-27 17:36:09.831 1236-1236/com.example.soufin.pullupsv3 I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
06-27 17:36:09.831 1236-1236/com.example.soufin.pullupsv3 W/dalvikvm﹕ VFY: unable to resolve virtual method 431: Landroid/content/res/TypedArray;.getType (I)I
06-27 17:36:09.831 1236-1236/com.example.soufin.pullupsv3 D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
06-27 17:36:09.919 1236-1236/com.example.soufin.pullupsv3 D/dalvikvm﹕ GC_FOR_ALLOC freed 140K, 7% free 3308K/3520K, paused 15ms, total 16ms
06-27 17:36:09.935 1236-1236/com.example.soufin.pullupsv3 D/dalvikvm﹕ GC_FOR_ALLOC freed 4K, 6% free 3524K/3744K, paused 7ms, total 8ms
06-27 17:36:10.011 1236-1236/com.example.soufin.pullupsv3 I/dalvikvm-heap﹕ Grow heap (frag case) to 5.927MB for 2536932-byte allocation
06-27 17:36:10.019 1236-1242/com.example.soufin.pullupsv3 D/dalvikvm﹕ GC_FOR_ALLOC freed <1K, 4% free 6001K/6224K, paused 6ms, total 6ms
06-27 17:36:10.151 1236-1236/com.example.soufin.pullupsv3 D/libEGL﹕ loaded /system/lib/egl/libEGL_xap.so
06-27 17:36:10.151 1236-1236/com.example.soufin.pullupsv3 D/eglCodecCommon﹕ TcpStream::connect() - management hostname is 10.71.34.101
06-27 17:36:10.151 1236-1236/com.example.soufin.pullupsv3 D/eglCodecCommon﹕ TcpStream::connect() - connecting host: 10.71.34.1
06-27 17:36:10.171 1236-1236/com.example.soufin.pullupsv3 D/﹕ HostConnection::get() New Host Connection established 0xb8786ca8, tid 1236
06-27 17:36:10.171 1236-1236/com.example.soufin.pullupsv3 D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_xap.so
06-27 17:36:10.175 1236-1236/com.example.soufin.pullupsv3 D/libEGL﹕ loaded /system/lib/egl/libGLESv2_xap.so
06-27 17:36:10.255 1236-1236/com.example.soufin.pullupsv3 W/EGL_xap﹕ eglSurfaceAttrib not implemented
06-27 17:36:10.255 1236-1236/com.example.soufin.pullupsv3 E/OpenGLRenderer﹕ Getting MAX_TEXTURE_SIZE from GradienCache
06-27 17:36:10.255 1236-1236/com.example.soufin.pullupsv3 E/OpenGLRenderer﹕ MAX_TEXTURE_SIZE: 16384
06-27 17:36:10.259 1236-1236/com.example.soufin.pullupsv3 E/OpenGLRenderer﹕ Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
06-27 17:36:10.259 1236-1236/com.example.soufin.pullupsv3 E/OpenGLRenderer﹕ MAX_TEXTURE_SIZE: 16384
06-27 17:36:10.259 1236-1236/com.example.soufin.pullupsv3 D/OpenGLRenderer﹕ Enabling debug mode 0
06-27 17:36:15.815 1236-1236/com.example.soufin.pullupsv3 W/EGL_xap﹕ eglSurfaceAttrib not implemented
06-27 17:36:19.395 1236-1236/com.example.soufin.pullupsv3 D/AndroidRuntime﹕ Shutting down VM
06-27 17:36:19.395 1236-1236/com.example.soufin.pullupsv3 W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa4d6ab20)
06-27 17:36:19.403 1236-1236/com.example.soufin.pullupsv3 E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.soufin.pullupsv3, PID: 1236
java.lang.NullPointerException
at com.example.soufin.pullupsv3.Workout$1.onClick(Workout.java:59)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
What am I doing wrong? And is the logic for my code alright?