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

时间:2016-07-21 02:27:33

标签: java android android-intent sharedpreferences

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 - 非活动

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

}

结果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

我有一个传递值的问题,我正在尝试传递计算的值并从类Calc [非活动]转换为MainActivity以使用/提示输出。但是值变为0 / null .. 它应该显示(样本数据) IN:3 OUT:1

所以有人可以帮助我吗?或者我的方法做错了吗?

已尝试过sharedpreference和intent bundle

1 个答案:

答案 0 :(得分:2)

您没有设置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
}

修改:

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

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

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