java - Android init Array Problems -
i've been stucked find what's wrong code below.
here's 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; (i=0 ; < 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 disable line code should run } } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } }
here's 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>
the problem when put line of code insert value of int array loop. when ran , put string input, got error message
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 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): @ com.andri.hilltest.mainactivity.onclick(mainactivity.java:52) 04-11 10:50:22.049: e/androidruntime(17743): @ android.view.view.performclick(view.java:3511) 04-11 10:50:22.049: e/androidruntime(17743): @ android.view.view$performclick.run(view.java:14105) 04-11 10:50:22.049: e/androidruntime(17743): @ android.os.handler.handlecallback(handler.java:605) 04-11 10:50:22.049: e/androidruntime(17743): @ android.os.handler.dispatchmessage(handler.java:92) 04-11 10:50:22.049: e/androidruntime(17743): @ android.os.looper.loop(looper.java:137) 04-11 10:50:22.049: e/androidruntime(17743): @ android.app.activitythread.main(activitythread.java:4609) 04-11 10:50:22.049: e/androidruntime(17743): @ java.lang.reflect.method.invokenative(native method) 04-11 10:50:22.049: e/androidruntime(17743): @ java.lang.reflect.method.invoke(method.java:511) 04-11 10:50:22.049: e/androidruntime(17743): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:789) 04-11 10:50:22.049: e/androidruntime(17743): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:556) 04-11 10:50:22.049: e/androidruntime(17743): @ dalvik.system.nativestart.main(native method) 04-11 10:55:22.109: i/process(17743): sending signal. pid: 17743 sig: 9
i don't know what's goin on code, hope out there solve problem. hope.... attention... terima kasih.
if not want specify length of array have use list...
arraylist<integer> array_angka = new arraylist<integer>();
you add follows
array_angka.add(posisi_huruf);
you can retrieve arraylist follows
array_angka.get(0);
remember if not know ahead of time length of array recreating entire array every time increase size of array, arraylist give dynamically growing array.
Comments
Post a Comment