vendredi 26 février 2016

Im doing a stepcounter that save stats and resets the counter when the date is changed. [duplicate]

This question already has an answer here:

app crashes on launch, more specifically in the "getAllData" method, im recieving this error, im att loss, what to do?

Attempt to invoke virtual method 'android.database.Cursor com.example.arman.yourstep.DatabaseHelper.getAllData()' on a null object reference

here is the broadcastreceiver class

public class MyBootReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
        Intent i = new Intent(context, MainActivity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(i);
    }else  if(intent.getAction().equals("android.intent.action.DATE_CHANGED")){
        StatsActivity sa = new StatsActivity();
        sa.newday();
    }
}

}

here is the mainactivity class

public class MainActivity extends AppCompatActivity implements SensorEventListener{


private StatsActivity sa;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    count = (TextView)findViewById(R.id.count);
    sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

    sa = new StatsActivity();


@Override
public void onSensorChanged(SensorEvent event){
    if(activityWalk){
        count.setText(String.valueOf(event.values[0]));
        steps = event.values[0];
        sa.newStep();

    }
    if (event.values [0] == 10000){
        notificationManager.notify(1337,builder.build());
    }

}

here is the statsactivity class that holds the stats

public class StatsActivity extends AppCompatActivity  {
private TextView avgStep, totalStep, meterStep;
private int steps,days;
private DatabaseHelper dBase;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_stats);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    dBase = new DatabaseHelper(this);

    getStats();

    avgStep = (TextView)findViewById(R.id.avgCount);
    totalStep = (TextView)findViewById(R.id.totalCount);
    meterStep = (TextView)findViewById(R.id.meterCount);

    float meter = new Float(0.762);

    meterStep.setText(Float.toString(steps * meter));
    totalStep.setText(Float.toString(steps));
    avgStep.setText(Float.toString(steps / days));
}

private void getStats(){
    Cursor result = dBase.getAllData();
    if (result.getCount()==0){
        //Make error handling
    }
    try {
        days = result.getInt(0);   //here is where i crash
        steps = result.getInt(1);
    }catch (Exception e){
        Log.d("db", e.getMessage());
    }
    result.close();
}

public int getDays(){
    getStats();
    return days;
}

public int getSteps(){
    getStats();
    return steps;
}

public void newday(){
    getStats();
    days++;
    dBase.insertData(days, steps);
}

public void newStep(){
    getStats();
    steps++;
    dBase.insertData(days, steps);
}

}

hopefully someone can see what im doing wrong.

Aucun commentaire:

Enregistrer un commentaire