Question

I'm trying to write code so that when the hints option is checked in settings it should display hints, but when I do s it says there is no getContext() method defined. What will this method do and where will i have to define this ?

Here is my code for the logic of my maths app:

package com.gamesup.braingame;

import java.util.Random;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Easy extends Activity implements OnClickListener{

EditText display;
// This Array says , I am an array that holds arrays
String [][] multiArray = {{"4 + 5 = ", "9"},     
        {"20 * 3 - 1 = ","59"},
        {"99 - 9 = ","90"},
        {"50 / 2 + 18 = ","43"},
        {"9 * 8 = ","72"},
        {"4 + 20 - 20 = ","4"},
        {"75 / 5 = ","15"},
        {"99 - 1 * 3 = ","96"},
        {"75 + 25 = ","100"}};  
TextView displayExpression;
TextView displayAnswer;
TextView setAnswer;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.easy);

    display = (EditText)findViewById(R.id.displayText);
    display.setText("?");

    displayExpression = (TextView) findViewById(R.id.expression);
    displayAnswer = (TextView) findViewById(R.id.answer_status);
    setAnswer = (TextView) findViewById(R.id.answer);
    Button generate = (Button) findViewById(R.id.random_gen);

    generate.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Random ranGenerate = new Random ();
            int random = ranGenerate.nextInt(multiArray.length) ;
            // Fetch your random question
            String Rquestion = multiArray[random][0];
            displayExpression.setText(Rquestion);
            displayAnswer.setText("");
            setAnswer.setText("?");

        }
    });

}

static boolean isEmpty = true;

public void num_Clicked(View v){
    Button btn = (Button) findViewById(v.getId());
    //getting the button object and using a view to get the id of the buttons

     if (v.getId()== R.id.del_button){
            String s = display.getText().toString();
            s = s.substring(0, s.length() - 1);
            display.setText(s);
            return;
     }


    if(isEmpty){
        display.setText(btn.getText());
        isEmpty = false;
    }
    else{
        display.append(btn.getText().toString()); 
    }



}

//xml attribute to views called android:onclick, that can be used to handle 
//clicks directly in the view's activity without need to implement any interface.

public void hash_Clicked(View v){

    // Get the Answer from your EditText
    String answer =  display.getText().toString();
    setAnswer.setText(answer);

    // Using a for loop iterate on the base index
    for(int i = 0; i < multiArray.length ; i++)
    {      
        // if the answer is in position 1 of Array [i] 
        if(answer.equals(multiArray[i][1]))
        {
            // We have found the answer, Congratulate the User 
            displayAnswer.setTextColor(getResources().getColor(R.color.green));
            displayAnswer.setText("CORRECT");
            break;

         }else{
             // Tell them how bad they are since they can't solve simple equations!
             displayAnswer.setTextColor(getResources().getColor(R.color.red));
             displayAnswer.setText("INCORRECT");

                         //this is where i am getting the error            
             if(Prefs.getHints(getContext())){ 

             }

         }


    }


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    super.onCreateOptionsMenu(menu);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.brain_game, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item){
    switch (item.getItemId()){
    case R.id.settings:
        startActivity(new Intent(this,Prefs.class));
        return true;
        // more items go here if any 
    }
    return false;
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

}


}

And the Prefs class:

package com.gamesup.braingame;

import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.os.Bundle;
import android.content.Context;


public class Prefs extends PreferenceActivity {

//option names and default values 
private static final String OPT_HINTS = "hints";
private static final boolean OPT_HINTS_DEF = true;

@Override
protected void onCreate(Bundle savedInstanceState){

    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.settings);
}


/**Get  the current value of the hints option*/
public static boolean getHints (Context context){
    return PreferenceManager.getDefaultSharedPreferences(context).getBoolean(OPT_HINTS, OPT_HINTS_DEF);

}

}
Was it helpful?

Solution

Just use this, you already are in an Activity, which is a Context:

if(Prefs.getHints(this)){ 

OTHER TIPS

Don't need to use other context because if you are calling that function in your Activity then you just need to pass this or yourActivityName.this which are also Context.

So change

  if(Prefs.getHints(this)){ 

or

  if(Prefs.getHints(YourActivityName.this)){ 
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top