gpt4 book ai didi

java - Android MainActivity 打印/将值转换为 0/null?

转载 作者:行者123 更新时间:2023-11-29 19:42:09 24 4
gpt4 key购买 nike

主要 Activity

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 - 不是 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;
}

}

结果日志

 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 bundle

最佳答案

您没有设置 pIN 和 pOUT 值。因为您将 pIN 和 pOUT 声明为公共(public)变量,所以您可以这样设置它们。

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

正如@brian661 上面提到的,您还可以创建一个构造函数来初始化 PassIN 和 PassOUT 变量。在这种情况下,请考虑将 PassIN 和 PassOUT 设为私有(private),因为您不需要直接访问它们:

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
}

编辑:

使用您提供的代码,更改PassINPassOUT 的唯一方法是调用Kappa()

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

我猜测 trnsferRP 值发生了变化,因此您可能希望将 public void Kappa() 的签名更改为 public void Kappa(String trnsferRP)因此您可以传入字符串。

关于java - Android MainActivity 打印/将值转换为 0/null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38493892/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com