Activity Will Get Get Destroyed and Turn Back To Main Activity Error: E/AndroidRuntime: FATAL EXCEPTION: main

huangapple go评论64阅读模式
英文:

Activity Will Get Get Destroyed and Turn Back To Main Activity Error: E/AndroidRuntime: FATAL EXCEPTION: main

问题

I'm here to provide translations for the non-code portions of your text. Here's the translation:

"So I've been looking around stackoverflow + reddit + docs and still no solution about this, I've tried so many catch blocks but no clue I tried with bytearray but still the same problem. this was the error:

ERROR: E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.namecompany.nameoftheapp, PID: NOOFPID
java.lang.IllegalArgumentException: bad base-64
at android.util.Base64.decode(Base64.java:161)
at android.util.Base64.decode(Base64.java:136)
at android.util.Base64.decode(Base64.java:118)
at com.nameoftheapp.company.EncryptDecrypt$2.onClick(EncryptDecrypt.java:56)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) ERROR

Code:
try {

encode.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

encode.setText(Base64.encodeToString(inputText.getText().toString().getBytes(), Base64.DEFAULT));
}
});

decode.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

decode.setText(new String(Base64.decode(inputText.getText().toString(), Base64.DEFAULT)));

}

});

}

catch (Exception e) {
Toast.makeText(EncryptDecrypt.this,"Numbers Cannot Be Decoded!",Toast.LENGTH_SHORT).show();
} code"

UPDATE::::: While Testing the encoding part was fine, but sometimes the decoding would completely get out the class/activity and would get to the main and show me the error above the code:::: UPDATE

If you have any solution or inteffered with this problem before please lmk. Thanks in Advance

英文:

So I've been looking around stackoverflow + reddit + docs and still no solution about this, I've tried so many catch blocks but no clue I tried with bytearray but still the same problem. this was the error:

ERROR """  E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.namecompany.nameoftheapp, PID: NOOFPID
    java.lang.IllegalArgumentException: bad base-64
        at android.util.Base64.decode(Base64.java:161)
        at android.util.Base64.decode(Base64.java:136)
        at android.util.Base64.decode(Base64.java:118)
        at com.nameoftheapp.company.EncryptDecrypt$2.onClick(EncryptDecrypt.java:56)
        at android.view.View.performClick(View.java:6294)
        at android.view.View$PerformClick.run(View.java:24770)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) """ ERROR

'''
**Code: '''
try {

                encode.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {


                        encode.setText(Base64.encodeToString(inputText.getText().toString().getBytes(), Base64.DEFAULT));
                    }
                });


                decode.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        decode.setText(new String(Base64.decode(inputText.getText().toString(), Base64.DEFAULT)));

                    }
                });

            }
           
            catch (Exception e) {
                Toast.makeText(EncryptDecrypt.this,"Numbers Cannot Be Decoded!",Toast.LENGTH_SHORT).show();
            }        ''''code** '''

UPDATE::::: While Testing the encoding part was fine , but sometimes the decoding would completely get out the class/activity and would get to the main and show me the error above the code:::: UPDATE

If you have any solution or inteffered with this problem before please lmk. Thanks in Advance

答案1

得分: 1

问题出在你的try/catch块的结构上。你有一个形状类似于这样的东西:

try {
    encode.setOnClickListener(...);
    decode.setOnClickListener(...);
} catch (Exception e) {
    // ...
}

这只会捕获在设置监听器时抛出的异常。它对从监听器内部抛出的异常不会产生任何影响。

尝试改为这样:

decode.setOnClickListener(
    @Override
    public void onClick(View v) {
        try {
            decode.setText(...);
        } catch (Exception e) {
            // 在这里显示Toast消息
        }
    }
);
英文:

The problem is in the structure of your try/catch block. You have something that is shaped like this:

try {
    encode.setOnClickListener(...);
    decode.setOnClickListener(...);
} catch (Exception e) {
    // ...
}

This will only catch exceptions thrown while setting the listeners. It will not have any effect on exceptions thrown from inside the listeners.

Try this instead:

decode.setOnClickListener(
    @Override
    public void onClick(View v) {
        try {
            decode.setText(...);
        } catch (Exception e) {
            // show toast message here
        }
    }
);

huangapple
  • 本文由 发表于 2020年8月11日 03:19:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63346675.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定