从另一个方法调用一个方法时如何传递args

时间:2015-06-01 09:02:45

标签: java android methods parameters

我编写了以下代码来创建密钥对,在本地存储私钥,然后从该文件中读取私钥。

当我尝试调用方法savePrivateKey();和retrievePrivateKey(); from testData(View view)我得到一个错误,说(String [])不能应用于()。我希望能够在testData(View view);

中调用上述两个函数
public class EncryptionActivity extends ActionBarActivity {
private static final String TAG = EncryptionActivity.class.getSimpleName();

TextView textPublicKey;
TextView textPrivateKey;
Button buttonTest;
TextView privateKey;
Integer n;
String FILENAME = "privateKey";


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

    // output keys to screen
    textPrivateKey = (TextView)findViewById(R.id.textPrivateKey);
    textPrivateKey.setMovementMethod(new ScrollingMovementMethod());
    // textPublicKey = (TextView)findViewById(R.id.textPublicKey);
}

private void AsymmetricAlgorithmRSA() {
    // Generate key pair for 1024-bit RSA encryption and decryption
    Key publicKey = null;
    Key privateKey = null;
    try {
        KeyPairGenerator kpg = KeyPairGenerator.getInstance("RSA");
        kpg.initialize(1024);
        KeyPair kp = kpg.genKeyPair();
        publicKey = kp.getPublic();
        privateKey = kp.getPrivate();
    } catch (Exception e) {
        Log.e(TAG, "RSA key pair error");
    }

    //textPublicKey.setText(String.valueOf(publicKey));
    //textPrivateKey.setText(String.valueOf(privateKey));

}

public void savePrivateKey(String[] args) throws FileNotFoundException {
    try {
        // store private key locally
        String string = String.valueOf(privateKey);

        FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
        fos.write(string.getBytes());
        fos.close();
    }
    catch (Exception e) {
        Log.e(TAG, "Error saving file.");
    }
}

public void retrievePrivateKey(String[] args) throws FileNotFoundException {
    try {
        FileInputStream fis = openFileInput(FILENAME);
        StringBuffer fileContent = new StringBuffer("");

        byte[] buffer = new byte[1024];

        while ((n = fis.read(buffer)) != -1) ;
        {
            fileContent.append(new String(buffer, 0, n));
        }

        textPrivateKey.setText(String.valueOf(fileContent));
    }
    catch(IOException e) {
            Log.e(TAG, "Error opening file.");
        }
}

public void testData(View view){

    AsymmetricAlgorithmRSA();
    savePrivateKey();
    retrievePrivateKey();
}

2 个答案:

答案 0 :(得分:1)

savePrivateKeyretrievePrivateKey都接受String[],但他们不使用它们。只需删除这些冗余参数规范就可以了:

public void savePrivateKey() throws FileNotFoundException {
    // code here...
}

public void retrievePrivateKey() throws FileNotFoundException {
    // code here...
}

答案 1 :(得分:0)

savePrivateKey(); - >没有参数的方法。但是你实现了一个带参数的方法,如String [] public void savePrivateKey(String [] args)抛出FileNotFoundException ..传递给String []作为参数或更改方法签名。