我怎样才能解决java.lang。“illegalStateException”“StringIndexOutOfBound”“InvocationTargetException”

时间:2013-03-06 16:02:52

标签: java android

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

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

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

第三个错误:由java.lang.reflect.InvocationTargetException

引起
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
有人请帮助我,谢谢你..

1 个答案:

答案 0 :(得分:1)

在你的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++) {

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