Wafi Ismail Wafi Ismail - 4 months ago 22
Android Question

Android MainActivity print/convert the value into 0/null?

MainActivity

public class MainActivity extends AppCompatActivity {


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

Calc A = new Calc();
int pIN = A.pIN();
int pOUT = A.pOUT();

System.out.println("IN : " + pIN);
System.out.println("OUT : " + pOUT);

}

}


clascc Calc - Not an Activity

public class Calc extends MainActivity {


public int PassIN;
public int PassOUT;

public String convertStringToHex(String str) {

char[] chars = str.toCharArray();

StringBuffer hex = new StringBuffer();
for (int i = 0; i < chars.length; i++) {
hex.append(Integer.toHexString((int) chars[i]));
}

return hex.toString();
}

public String convertHexToDec(String hex) {

StringBuilder sb = new StringBuilder();
StringBuilder temp = new StringBuilder();

for (int i = 0; i < hex.length() - 1; i += 2) {

String output = hex.substring(i, (i + 2));

int decimal = Integer.parseInt(output, 16);

temp.append(decimal);
}

return temp.toString();
}

public void Kappa() {


String trnsferRP = "VDV2b0301";

Calc strToHex = new Calc();
String hex = strToHex.convertStringToHex(trnsferRP);
Calc strToDec = new Calc();
String dec = strToDec.convertHexToDec(hex);

String cut = dec.substring(10);

String A = cut.substring(0, 2);
String B = cut.substring(2, 4);
String C = cut.substring(4, 6);
String D = cut.substring(6, 8);

int cvA = Integer.parseInt(A);
int cvB = Integer.parseInt(B);
int cvC = Integer.parseInt(C);
int cvD = Integer.parseInt(D);

int calcA = (cvA - 48);
int calcB = (cvB - 48);
int calcC = (cvC - 48);
int calcD = (cvD - 48);

this.PassIN = calcA + calcB;
this.PassOUT = calcC + calcD;
}

public int pIN(){
return PassIN;
}

public int pOUT(){
return PassOUT;
}

}


Result logcat

07-21 01:29:10.672 26194-26224/com.example.kappa.passvalue2 W/EGL_emulation: eglSurfaceAttrib not implemented
07-21 01:29:10.672 26194-26224/com.example.kappa.passvalue2 W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xae3e3580, error=EGL_SUCCESS
07-21 01:29:20.426 26373-26373/com.example.kappa.passvalue2 W/System: ClassLoader referenced unknown path: /data/app/com.example.kappa.passvalue2-1/lib/x86
07-21 01:29:20.722 26373-26373/com.example.kappa.passvalue2 W/System: ClassLoader referenced unknown path: /data/app/com.example.kappa.passvalue2-1/lib/x86
07-21 01:29:20.772 26373-26373/com.example.kappa.passvalue2 W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
07-21 01:29:20.827 26373-26373/com.example.kappa.passvalue2 I/System.out: IN : 0
07-21 01:29:20.827 26373-26373/com.example.kappa.passvalue2 I/System.out: OUT : 0
07-21 01:29:20.837 26373-26399/com.example.kappa.passvalue2 D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true


I have a problem to pass the value,I'm trying to pass the value calculated and converted from class Calc [not an Activity] to MainActivity to be used/prompt out.But the value became 0/null..
it should display (sample data)
IN : 3
OUT : 1

so can anybody help me? or my method wrong on doing this?

already tried sharedpreference and intent bundle

Answer

You are not setting the pIN and pOUT values. Because you declared pIN and pOUT as public variables, you can set them like this.

Calc A = new Calc();
A.PassIN = 3;
A.PassOUT = 1;

As @brian661 mentioned above, you can also create a constructor that initializes the PassIN and PassOUT variables. In that case, consider making PassIN and PassOUT private since you would not need to access them directly:

public class Calc extends MainActivity {
    private int PassIN;
    private int PassOUT;
    public Calc(int PassIN, int PassOUT){
        this.PassIN = passIN;
        this.PassOUT = passOUT;
    }
    //rest of class
}

Edit:

Using the code you've provided, the only way to change PassIN and PassOUT is to call Kappa().

Calc A = new Calc();
A.Kappa();

I'm guessing that the trnsferRP value changes, so you would probably want to change the signature of public void Kappa() to public void Kappa(String trnsferRP) so you can pass in the string instead.