[英]Android NullPointer Exception in method kpg.initializate
我是android的新手,我正在嘗試制作一個關於cypher / decypher應用程序的“簡單”教程。 我有以下代碼
public class MainActivity extends AppCompatActivity {
KeyPairGenerator kpg;
KeyPair kp;
PublicKey publicKey;
PrivateKey privateKey;
byte[] encryptedBytes, decryptedBytes;
Cipher cipher, cipher1;
String encrypted, decrypted;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
generateKey();
TextView txtPuk = (TextView) findViewById(R.id.tViewPUK);
txtPuk.setText(kp.getPublic().toString());
}catch(Exception e){
e.printStackTrace();
Toast.makeText(getBaseContext(), e.toString(),Toast.LENGTH_LONG).show();
}
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void generateKey() throws NoSuchAlgorithmException, NoSuchPaddingException,
IllegalBlockSizeException,BadPaddingException,InvalidKeyException{
try{
kpg = KeyPairGenerator.getInstance("RSA");
kpg.initialize(1024); //NULLPOINTER HERE
kp = kpg.genKeyPair();
}catch(Exception e){
e.printStackTrace();
Toast.makeText(getBaseContext(), e.toString(),Toast.LENGTH_LONG).show();
}
}
public byte[] RSAEncrypt(final String plain) throws NoSuchAlgorithmException, NoSuchPaddingException,
IllegalBlockSizeException, BadPaddingException, InvalidKeyException {
;
publicKey = kp.getPublic();
privateKey = kp.getPrivate();
cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
encryptedBytes = cipher.doFinal(plain.getBytes());
return encryptedBytes;
}
public String RSADecrypt(final byte[] encryptedBytes) throws NoSuchAlgorithmException, NoSuchPaddingException,
InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
cipher1 = Cipher.getInstance("RSA");
cipher1.init(Cipher.DECRYPT_MODE, privateKey);
decryptedBytes = cipher1.doFinal(encryptedBytes);
decrypted = new String(decryptedBytes);
return decrypted;
}
}
但正如標題所說,我在kpg.initialize(1024)
得到了一個NullPointer。 由於理論似乎是正確的,我覺得這可能是一個我不遵循的盲目技巧,所以這可能是發生這種情況的原因
編輯:編輯代碼刪除還原劑代碼,如sugested
問題是在setContentView(R.layout.activity_main);
之前調用findViewById(R.id.tViewPUK)
setContentView(R.layout.activity_main);
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.