gpt4 book ai didi

java - 我怎样才能解决java.lang ."illegalStateException" "StringIndexOutOfBound" "InvocationTargetException"

转载 作者:行者123 更新时间:2023-12-01 14:47:42 27 4
gpt4 key购买 nike

有人可以帮我吗,我编写了这个应用程序,但它不起作用,而且我找不到解决方案。

第一个错误:由 java.lang.IllegalStateException 引起:无法执行 Activity 的方法

第二个错误:由 java.lang.StringIndexOutOfBoundException 引起

第三个错误:由 java.lang.reflect.InvocableTargetException 引起

package com.enkripsms;

import com.dbutil.dbhelper;
import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class tulisPesan extends Activity {
/**
* @see android.app.Activity#onCreate(Bundle)
*/
EditText pesan;
EditText kunci;
private SQLiteDatabase db = null;
private dbhelper data = null;

@Override

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// TODO Put your code here
setContentView(R.layout.tulispesan);
data = new dbhelper(this);
db = data.getWritableDatabase();
data.createTable(db);
}

public void sandikan ( View s ) {
kunci = (EditText)findViewById(R.id.kunci);
pesan = (EditText)findViewById(R.id.pesan);
String kuncinya = kunci.getText().toString();
String plain = pesan.getText().toString();
String chiper = "";

if (kuncinya.isEmpty()) { //(kunci == null)
Toast.makeText(this, "Maaf, kunci harus diisi", Toast.LENGTH_LONG).show();
}else{
String acak = "";
double acakN;
for (int a = 0; a<kuncinya.length(); a++) {
acakN = Math.random() * 25 + 65;
acak = acak + (char) acakN;
}
kunci.setText(acak);
}

if (kuncinya.length() != plain.length()) {
for (int a = 0; a<plain.length(); a++) {
String tambahk = "";
tambahk = tambahk + kuncinya.substring(0, 1);
kuncinya = tambahk;
}
kunci.setText(kuncinya);
}

for (int a = 0; a<plain.length(); a++) {
String plainBin = "0" + Integer.toBinaryString((int)plain.charAt(a));

if (plainBin.length() < 8)
plainBin = "0" + plainBin;

String keyBin = "0" + Integer.toBinaryString((int)kuncinya.charAt(a));

for (int b = 0; b<8; b++) {
chiper = chiper + Integer.toString((Integer.parseInt(plainBin.substring(b, b+1))) ^ Integer.parseInt(keyBin.substring(b, b+1)));
}
}
Intent hasilPesan = new Intent();
hasilPesan.setClass(this, hasilPesan.class);
hasilPesan.putExtra("chiper", chiper);
startActivity(hasilPesan);
}

public void inputData () {

}

public void kembali ( View k) {
Intent kembali = new Intent();
kembali.setClass(this, MainActivity.class);
startActivity(kembali);
}


}

这是我的完整错误

03-06 22:39:59.964: E/AndroidRuntime(330): FATAL EXCEPTION: main
03-06 22:39:59.964: E/AndroidRuntime(330): java.lang.IllegalStateException: Could not execute method of the activity
03-06 22:39:59.964: E/AndroidRuntime(330): at android.view.View$1.onClick(View.java:2144)
03-06 22:39:59.964: E/AndroidRuntime(330): at android.view.View.performClick(View.java:2485)
03-06 22:39:59.964: E/AndroidRuntime(330): at android.view.View$PerformClick.run(View.java:9080)
03-06 22:39:59.964: E/AndroidRuntime(330): at android.os.Handler.handleCallback(Handler.java:587)
03-06 22:39:59.964: E/AndroidRuntime(330): at android.os.Handler.dispatchMessage(Handler.java:92)
03-06 22:39:59.964: E/AndroidRuntime(330): at android.os.Looper.loop(Looper.java:123)
03-06 22:39:59.964: E/AndroidRuntime(330): at android.app.ActivityThread.main(ActivityThread.java:3683)
03-06 22:39:59.964: E/AndroidRuntime(330): at java.lang.reflect.Method.invokeNative(Native Method)
03-06 22:39:59.964: E/AndroidRuntime(330): at java.lang.reflect.Method.invoke(Method.java:507)
03-06 22:39:59.964: E/AndroidRuntime(330): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-06 22:39:59.964: E/AndroidRuntime(330): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-06 22:39:59.964: E/AndroidRuntime(330): at dalvik.system.NativeStart.main(Native Method)
03-06 22:39:59.964: E/AndroidRuntime(330): Caused by: java.lang.reflect.InvocationTargetException
03-06 22:39:59.964: E/AndroidRuntime(330): at java.lang.reflect.Method.invokeNative(Native Method)
03-06 22:39:59.964: E/AndroidRuntime(330): at java.lang.reflect.Method.invoke(Method.java:507)
03-06 22:39:59.964: E/AndroidRuntime(330): at android.view.View$1.onClick(View.java:2139)
03-06 22:39:59.964: E/AndroidRuntime(330): ... 11 more
03-06 22:39:59.964: E/AndroidRuntime(330): Caused by: java.lang.StringIndexOutOfBoundsException
03-06 22:39:59.964: E/AndroidRuntime(330): at com.enkripsms.tulisPesan.sandikan(tulisPesan.java:66)
03-06 22:39:59.964: E/AndroidRuntime(330): ... 14 more

请有人帮助我,谢谢..

最佳答案

在你的for循环中:

chiper = chiper + Integer.toString((Integer.parseInt(plainBin.substring(b, b+1))) ^ Integer.parseInt(keyBin.substring(b, b+1)));

超过字符串的长度。

所以看看这一行并检查您是否想要循环 8 次

for (int b = 0; b<8; b++) {

记得你说过在设置 chiper 时要查看 b+1,所以你的问题的原因是当你在循环中的某个时刻点击 b+1 时没有值(很可能是最后一个循环)

关于java - 我怎样才能解决java.lang ."illegalStateException" "StringIndexOutOfBound" "InvocationTargetException",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15252190/

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