-2

I have a public class with one public static String method. I have to getString() and I know that before that I have to extract String ressource and pass Context and I dont know how to pass it .

Another class uses public static String method

Code example:

public class StringUtil {

    public static String sumUp(int nr1, int nr2) {
        int sum = nr1 + nr2;
        String result;

        //all what is in ""  
        result =.getString(R.string.result) + sum;
        return result;

    }
}

ButtonListener class uses public static String method. Code:

public class ButtonListener extends MainActivity implements View.OnClickListener {
MainActivity activity;
EditText et1;
EditText et2;

public ButtonListener(MainActivity activity) {
    this.activity = activity;
}

@Override
public void onClick(View view) {

    switch (view.getId()){
        case R.id.sum:
            et1 = (EditText) activity.findViewById(R.id.nr1_edit_text);
            et2 = (EditText)activity.findViewById(R.id.nr2_edit_text);

            String nr1In = et1.getText().toString();
            String nr2In = et1.getText().toString();

            int nr1 = Integer.parseInt(nr1In);
            int nr2 = Integer.parseInt(nr2In);

            Intent intent = new Intent(activity, IntentActivity.class);
            intent.putExtra("result", StringUtil.sumUp(nr1, nr2));
            activity.startActivity(intent);
            break;
    }

}
}

IntentActivity class ´getIntent´ Code:

public class IntentActivity extends MainActivity{

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


       TextView textView = (TextView)findViewById(R.id.result);

        Intent intent = getIntent();

        String fResult = intent.getStringExtra("result");
        textView.setText(fResult);


    }
}

And MainActivity Code:

public class MainActivity extends AppCompatActivity {

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



        ButtonListener bl = new ButtonListener(this);

        Button sumUp= (Button)findViewById(R.id.sum_button);
        sumUp.setOnClickListener(bl);

    }
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
lounchy
  • 76
  • 2
  • 11

3 Answers3

1
public class StringUtil {

    public static String sumUp(int nr1, int nr2, Context context){
        int sum = nr1 +nr2;
        String result;

        result = context.getString(R.string.result) + sum;
        return result;

        }
    }

And use it like:

StringUtil.sumUp(1,2, this); // from Activity
StringUtil.sumUp(1,2, getContext()); // from Fragment
JoKr
  • 4,976
  • 8
  • 27
  • 39
0

Replace your StringUtil class with this.

public class StringUtil {

    public static String sumUp(Context mContext, int nr1, int nr2){
        int sum = nr1 +nr2;
        String result;

        //all what is in ""  
        result = mContext.getString(R.string.result) + sum;
        return result;

    }
}

Edit

And call this method from your ButtonListener activity.

intent.putExtra("result", StringUtil.sumUp(ButtonListener.this, nr1, nr2));
Vishal Chhodwani
  • 2,567
  • 5
  • 27
  • 40
  • Sorry in the beginig i had to show all my code and explane better what actualy i need. I just updated the post with all activities, this one is tricky ( at least for me) – lounchy Jan 13 '17 at 13:27
  • @lounchy I have Update My answer. Please take a look. You just need to pass context from your ButtonListener Activity. – Vishal Chhodwani Jan 13 '17 at 13:42
  • aplication crashes. Because of `result = mContext.getString(R.string.result) + sum;` and `intent.putExtra("result", StringUtil.sumUp(ButtonListener.this, nr1, nr2));` Error mesage: `java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference` – lounchy Jan 13 '17 at 13:58
0

You have 2 ways: 1.to pass it as a parameter for your method:

          public static String sumUp(int nr1, int nr2 ,Context context){
            int sum = nr1 +nr2;
            String result;
            //all what is in ""  
            result = .getString(R.string.result) + sum;
            return result;              
            }

2.to add context to the class constructor and then you can use it inside your method:

public class StringUtil {
  Context context;

  public void StringUtil (Context context){
  this.context = context;
  }


    public static String sumUp(int nr1, int nr2){
    int sum = nr1 +nr2;
    String result;
    //all what is in ""  
    result = .getString(R.string.result) + sum;
    return result;

    }
}
zMabrook
  • 932
  • 7
  • 9