我一直坚持在下面找到我的代码有什么问题。
这是 MainActivity.java
package com.andri.hilltest;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
EditText txtPlain, txtKey, txtCipher;
Button btnEncrypt;
String char_db = "ABCDEFGHIJKLMNOPQRSTUVWXYZabc";
int array_angka[];
int i;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtPlain = (EditText) findViewById(R.id.txtPlain);
txtKey = (EditText) findViewById(R.id.txtKey);
txtCipher = (EditText) findViewById(R.id.txtCipher);
btnEncrypt = (Button) findViewById(R.id.btnEncrypt);
btnEncrypt.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v == btnEncrypt){
String plainTextInput = txtPlain.getText().toString();
char[] array_huruf = plainTextInput.toCharArray();
//int[] angka2;
for (i=0 ; i < array_huruf.length ; i++){
int posisi_huruf = char_db.indexOf(array_huruf[i]);
Toast.makeText(this, "Tombol ditekan " + posisi_huruf , Toast.LENGTH_SHORT).show();
array_angka[i] = posisi_huruf; //if I disable this line the code should run well
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
这是 MainActivity.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Plaintext"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/txtPlain"
android:hint="Masukkan plaintext"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Key"
/>
<EditText
android:layout_width="fill_parent"
android:hint="Masukkan Key"
android:layout_height="wrap_content"
android:id="@+id/txtKey"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Encrypt"
android:id="@+id/btnEncrypt"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Cipher (Hill Cipher)"
/>
<EditText
android:layout_width="fill_parent"
android:hint=""
android:layout_height="wrap_content"
android:id="@+id/txtCipher"
/>
</LinearLayout>
问题是当我放置一行代码从循环中插入int数组的值时。当我运行并输入一些字符串作为输入时,我收到此错误消息
log.txt的
04-11 10:50:22.039: D/AndroidRuntime(17743): Shutting down VM
04-11 10:50:22.039: W/dalvikvm(17743): threadid=1: thread exiting with uncaught exception (group=0x40a621f8)
04-11 10:50:22.049: E/AndroidRuntime(17743): FATAL EXCEPTION: main
04-11 10:50:22.049: E/AndroidRuntime(17743): java.lang.NullPointerException
04-11 10:50:22.049: E/AndroidRuntime(17743): at com.andri.hilltest.MainActivity.onClick(MainActivity.java:52)
04-11 10:50:22.049: E/AndroidRuntime(17743): at android.view.View.performClick(View.java:3511)
04-11 10:50:22.049: E/AndroidRuntime(17743): at android.view.View$PerformClick.run(View.java:14105)
04-11 10:50:22.049: E/AndroidRuntime(17743): at android.os.Handler.handleCallback(Handler.java:605)
04-11 10:50:22.049: E/AndroidRuntime(17743): at android.os.Handler.dispatchMessage(Handler.java:92)
04-11 10:50:22.049: E/AndroidRuntime(17743): at android.os.Looper.loop(Looper.java:137)
04-11 10:50:22.049: E/AndroidRuntime(17743): at android.app.ActivityThread.main(ActivityThread.java:4609)
04-11 10:50:22.049: E/AndroidRuntime(17743): at java.lang.reflect.Method.invokeNative(Native Method)
04-11 10:50:22.049: E/AndroidRuntime(17743): at java.lang.reflect.Method.invoke(Method.java:511)
04-11 10:50:22.049: E/AndroidRuntime(17743): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
04-11 10:50:22.049: E/AndroidRuntime(17743): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
04-11 10:50:22.049: E/AndroidRuntime(17743): at dalvik.system.NativeStart.main(Native Method)
04-11 10:55:22.109: I/Process(17743): Sending signal. PID: 17743 SIG: 9
我不知道这段代码有什么用处,我希望有人可以帮助解决我的问题。我真的很希望....感谢您的关注...... Terima kasih。
答案 0 :(得分:5)
您忘记初始化array_angka
// setting size to this scoping to just those 2 line of code
array_angka = new int[array_huruf.length];
答案 1 :(得分:1)
如果您不想指定数组的长度,则必须使用列表...
ArrayList<Integer> array_angka = new ArrayList<Integer>();
然后您将按如下方式添加
array_angka.add(posisi_huruf);
您可以从ArrayList中检索如下
array_angka.get(0);
请记住,如果你每次增加数组的大小时都不知道数组的长度是重新创建整个数组,那么这就是ArrayList为你提供一个动态增长的数组。
答案 2 :(得分:0)
在使用之前,您需要将array_angka
初始化为对象。它默认指向null。