gpt4 book ai didi

java - Android:当应用程序尝试加密 .txt 文件时出现 NullPointerException

转载 作者:行者123 更新时间:2023-12-02 04:30:15 25 4
gpt4 key购买 nike

我正在尝试从我的虚拟设备 (Genymotion) 加密/解密文件。我将向您展示我的代码和异常(exception)情况。我猜想 nullPointerException 来 self 在 onActivityResult 方法中声明 View v = null 的行,但我不知道如何修复它。

异常

07-22 13:34:10.241  10419-10419/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: application.nikola.com.encryptdecrypt, PID: 10419
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=file:///storage/emulated/0/nikola.txt }} to activity {application.nikola.com.encryptdecrypt/application.nikola.com.encryptdecrypt.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getId()' on a null object reference
at android.app.ActivityThread.deliverResults(ActivityThread.java:3539)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3582)
at android.app.ActivityThread.access$1300(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1327)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getId()' on a null object reference
at application.nikola.com.encryptdecrypt.MainActivity.onActivityResult(MainActivity.java:70)
at android.app.Activity.dispatchActivityResult(Activity.java:6135)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3535)
            at android.app.ActivityThread.handleSendResult(ActivityThread.java:3582)
            at android.app.ActivityThread.access$1300(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1327)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
07-22 13:34:10.263 765-1598/? W/ActivityManager﹕ Force finishing activity application.nikola.com.encryptdecrypt/.MainActivity
07-22 13:34:10.350 765-1598/? E/ActivityManager﹕ Invalid thumbnail dimensions: 576x576
07-22 13:34:10.400 765-2035/? I/OpenGLRenderer﹕ Initialized EGL, version 1.4
07-22 13:34:10.466 765-2035/? W/EGL_emulation﹕ eglSurfaceAttrib not implemented
07-22 13:34:10.466 765-2035/? W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0x9ea263c0, error=EGL_SUCCESS
07-22 13:34:10.903 765-786/? W/ActivityManager﹕ Activity pause timeout for ActivityRecord{2ef175ff u0 application.nikola.com.encryptdecrypt/.MainActivity t26 f}
07-22 13:34:11.032 1006-1265/? W/EGL_emulation﹕ eglSurfaceAttrib not implemented
07-22 13:34:11.033 1006-1265/? W/OpenGLRenderer﹕ Failed to set EGL_SWAP_BEHAVIOR on surface 0xa095a020, error=EGL_SUCCESS
07-22 13:34:11.786 2640-2640/? I/art﹕ Explicit concurrent mark sweep GC freed 3007(201KB) AllocSpace objects, 0(0B) LOS objects, 24% free, 16MB/21MB, paused 301us total 21.960ms
07-22 13:34:11.808 2640-2640/? I/art﹕ Explicit concurrent mark sweep GC freed 3080(224KB) AllocSpace objects, 4(252KB) LOS objects, 24% free, 15MB/21MB, paused 173us total 7.668ms
07-22 13:34:11.822 2640-2640/? I/art﹕ Explicit concurrent mark sweep GC freed 3(96B) AllocSpace objects, 0(0B) LOS objects, 24% free, 15MB/21MB, paused 237us total 13.937ms
07-22 13:34:12.735 10419-10419/? I/Process﹕ Sending signal. PID: 10419 SIG: 9
07-22 13:34:12.738 765-823/? W/AudioTrack﹕ AUDIO_OUTPUT_FLAG_FAST denied by client
07-22 13:34:12.764 765-805/? W/InputDispatcher﹕ channel '25d12a93 application.nikola.com.encryptdecrypt/application.nikola.com.encryptdecrypt.MainActivity (server)' ~ Consumer closed input channel or an error occurred. events=0x9
07-22 13:34:12.764 765-805/? E/InputDispatcher﹕ channel '25d12a93 application.nikola.com.encryptdecrypt/application.nikola.com.encryptdecrypt.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
07-22 13:34:12.777 765-782/? I/ActivityManager﹕ Process application.nikola.com.encryptdecrypt (pid 10419) has died
07-22 13:34:12.778 765-981/? I/WindowState﹕ WIN DEATH: Window{25d12a93 u0 application.nikola.com.encryptdecrypt/application.nikola.com.encryptdecrypt.MainActivity}
07-22 13:34:12.778 765-981/? W/InputDispatcher﹕ Attempted to unregister already unregistered input channel '25d12a93 application.nikola.com.encryptdecrypt/application.nikola.com.encryptdecrypt.MainActivity (server)'

主要 Activity

public class MainActivity extends Activity implements View.OnClickListener {

Button btnEncrypt;
Button btnDecrypt;

final int ACTIVITY_CHOOSE_FILE = 1;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

btnEncrypt = (Button) this.findViewById(R.id.btnActivity1);
btnEncrypt.setOnClickListener(this);

btnDecrypt = (Button) this.findViewById(R.id.btnActivity2);
btnDecrypt.setOnClickListener(this);
}

@Override
public void onClick(View v) {

Intent chooseFile;
Intent intent;
chooseFile = new Intent(Intent.ACTION_GET_CONTENT);
chooseFile.setType("file*//*");
intent = Intent.createChooser(chooseFile, "Choose a file");
startActivityForResult(intent, ACTIVITY_CHOOSE_FILE);


}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

EncryptDecrypt crypto = new EncryptDecrypt();
View v = null;
String filePath = null;
switch(requestCode) {
case ACTIVITY_CHOOSE_FILE: {
if (resultCode == RESULT_OK){
Uri uri = data.getData();
filePath = uri.getPath();

switch(v.getId()) {
case R.id.btnActivity1:
crypto.encryptor(filePath);
break;
case R.id.btnActivity2:
crypto.decryptor(filePath);
break;
}
}
}
}



}
}

这是我使用的加密/解密算法。

public class EncryptDecrypt {

public void encryptor(String inputFilePath) {

FileOutputStream fos = null;
File file = new File(inputFilePath);
String keyString = "140405PX_0.$88";
String algorithm = "DESede";
try {
FileInputStream fileInputStream = new FileInputStream(file);
byte[] fileByteArray = new byte[fileInputStream.available()];
fileInputStream.read(fileByteArray);
for (byte b : fileByteArray) {
System.out.println(b);
}
SecretKey secretKey = getKey(keyString);
Cipher cipher = Cipher.getInstance(algorithm);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new CipherOutputStream(new FileOutputStream("encrypt.file"), cipher));
objectOutputStream.writeObject(fileByteArray);
objectOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}

public void decryptor(String inputFilePath) {

String outputFilePath = "decrypt.txt";

String keyString = "140405PX_0.$88";
String algorithm = "DESede";
try {
File inputFileNAme = new File(inputFilePath);
FileInputStream fileInputStream = new FileInputStream(inputFileNAme);
FileOutputStream fileOutputStream = new FileOutputStream(outputFilePath);
SecretKey secretKey = getKey(keyString);
Cipher cipher = Cipher.getInstance(algorithm);
cipher.init(Cipher.DECRYPT_MODE, secretKey);
ObjectInputStream objectInputStream = new ObjectInputStream(new CipherInputStream(fileInputStream, cipher));
System.out.println(objectInputStream.available());
fileOutputStream.write((byte[]) objectInputStream.readObject());
fileOutputStream.flush();
fileOutputStream.close();
fileInputStream.close();
objectInputStream.close();
} catch (Exception e) {
e.printStackTrace();
}

}

public static SecretKey getKey(String message) throws Exception {
String messageToUpperCase = message.toUpperCase();
byte[] digestOfPassword = messageToUpperCase.getBytes();
byte[] keyBytes = Arrays.copyOf(digestOfPassword, 24);
SecretKey key = new SecretKeySpec(keyBytes, "DESede");
return key;
}


}

最佳答案

    View v = null; // v is null
String filePath = null;
switch(requestCode) {
case ACTIVITY_CHOOSE_FILE: {
if (resultCode == RESULT_OK){
Uri uri = data.getData();
filePath = uri.getPath();

switch(v.getId()) { // v is still null
case R.id.btnActivity1:
crypto.encryptor(filePath);
break;
case R.id.btnActivity2:
crypto.decryptor(filePath);
break;
}
}
}
}

您的v为空,并且您正在尝试执行v.getId()。这就是为什么您会收到 NullPointerException

为了解决此问题,您可能需要向 btnActivity1btnActivity2 添加 OnClickListener。

   btnActivity1.setOnCLickListerner(new OnClickListener() {
@Override
public void onClick(View arg0) {
crypto.encryptor(filePath);
}
});
}

btnActivity2.setOnCLickListerner(new OnClickListener() {
@Override
public void onClick(View arg0) {
crypto.decryptor(filePath);
}
});
}

关于java - Android:当应用程序尝试加密 .txt 文件时出现 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31570354/

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