主要活动
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-不是活动
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;
}
}
结果记录
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
我有一个传递值的问题,我试图将计算后的值(从Calc类(不是Activity)转换为MainActivity)以使用/提示输出。但是该值变为0 / null。
它应该显示(样本数据)
输入:3
输出:1
有人可以帮我吗?还是我的方法做错了吗?
已经尝试了sharedpreference和intent捆绑包
最佳答案
您未设置pIN和pOUT值。因为您将pIN和pOUT声明为公共变量,所以可以这样设置它们。
Calc A = new Calc();
A.PassIN = 3;
A.PassOUT = 1;
正如上面提到的@ brian661,您还可以创建一个构造函数来初始化PassIN和PassOUT变量。在这种情况下,请考虑将PassIN和PassOUT设为私有,因为您无需直接访问它们:
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
}
编辑:
使用您提供的代码,更改
PassIN
和PassOUT
的唯一方法是调用Kappa()
。Calc A = new Calc();
A.Kappa();
我猜测trnsferRP值会发生变化,因此您可能希望将
public void Kappa()
的签名更改为public void Kappa(String trnsferRP)
,以便可以传入字符串。