tags:

views:

58

answers:

3

I have a class Scores.java I need to know whats the correct way to create these. I get "The constructor Scores is not defined. Do I have to extend off everything in Android??

package com.threeuglymen.android.stuff;

import android.app.Application;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;
import android.util.Log;


public class Scores {

    private Context mycontext;

    public Scores (Context context) {
        // TODO Auto-generated method stub
        this.mycontext = context;
    }

    public void resetScores(){
        try {
        SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(mycontext); 
        Editor edit = pref.edit();

        edit.putInt("correct", 0);
        edit.putInt("incorrect", 0);
        edit.commit();
        } catch (Exception e) {
            Log.d("Scores", "Exception:" + e.toString());
        }

        return;
    }

}

Thx for any guidance

Eric

A: 

You probably want:

public class ScoresSub extends Scores
{
  public ScoresSub(Context context)
  {
     super(context);
  }
}

Since you defined a constructor taking parameters in Scores, the compiler doesn't provide a no-arg one.

Matthew Flaschen
Matthew where would I put this? If I remove the Context context from the Scores() constructor. The app will build fine but once it hits the Scores score = new Scores()score.resetScores(); in MainActivity the try/Catch in scores hits for Null Pointer for SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(mycontext);
Ehask
@Ehask, do you need to have a no-arg constructor for Scores?
Matthew Flaschen
A: 

When you create a class you don't always new to extend from another, all clases extend from the class Object and if you extend another like in this case class Application it is because you want its function and you want to make a variation of it or override some method and i think in this case you don't even need to extend from Application, because your class is not intended to be an Application so:

public class Scores {

    private Context mycontext;

    public Scores () {
        this.mycontext = null;
    }

    public Scores (Context context) {
        // TODO Auto-generated method stub
        this.mycontext = context;
    }

    public void resetScores(){
        try {
        SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(mycontext); 
        Editor edit = pref.edit();

        edit.putInt("correct", 0);
        edit.putInt("incorrect", 0);
        edit.commit();
        } catch (Exception e) {
            Log.d("Scores", "Exception:" + e.toString());
        }

        return;
    }

}

and i don't think you should have a problem.

mklfarha
I goofed and posted code that I tried to test with. I get the same with or without the extends clause. Constructor Scores is not defined
Ehask
ups i didn't notice that you don't have a default constructor, you should always include it even if you don't use it public Scores(){} because Java uses it to create the object...
mklfarha
A: 

What you have currently should work. In your main activity, don't call

Scores score = new Scores();

instead call

Scores score = new Scores(getContext());

Let me know if this works.

Noel
Yup that did it I knew I was overlooking something stupid. Thx everyone
Ehask
Or if that code is inside a Context (such as an Activity) then just `new Scores(this);`
MatrixFrog