Android Intent - got crazy -
ok, i've been programming day , searched everywhere nothing yet. hope, me little bit. have 2 activities , want them communicate. here code
mainactivity
public class mainactivity extends activity{ imageview imgsettings; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); imgsettings = (imageview) findviewbyid(r.id.imgviewsettings); imgsettings.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent intent = new intent(mainactivity.this, appsettings.class); startactivity(intent); } }); } }
manifest
<application android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name=".mainactivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <activity android:name=".appsettings" android:label="@string/title_activity_app_settings" > </activity> </application>
appsettings
package com.example.oldie; import java.io.bufferedreader; import java.io.inputstream; import java.io.inputstreamreader; import java.io.outputstreamwriter; import android.app.activity; import android.os.bundle; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.textview; import android.widget.toast; public class appsettings extends activity implements onclicklistener { textview lblkeyword, txtkeyword; textview lblnumber, txtnumber; textview lbladdress, txtaddress; button btnok; string filename = "preferences.txt"; public static int count = 0; private static string keyword = ""; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); lblkeyword = (textview) findviewbyid(r.id.lblkeyword); txtkeyword = (textview) findviewbyid(r.id.txtkeyword); lblnumber = (textview) findviewbyid(r.id.lblnumber); txtnumber = (textview) findviewbyid(r.id.txtnumber); lbladdress = (textview) findviewbyid(r.id.lbladdress); txtaddress = (textview) findviewbyid(r.id.txtaddress); btnok = (button) findviewbyid(r.id.btnok); btnok.setonclicklistener(this); lblkeyword.setonclicklistener(this); lblnumber.setonclicklistener(this); lbladdress.setonclicklistener(this); //metritis gia na emfanisei mono mia fora minima if(count==0){ toast.maketext(getapplicationcontext(), "tab on labels more information", toast.length_long).show(); count = count + 1; } updateuifromfiles(); } @override public void onclick(view v) { switch(v.getid()){ case r.id.lblkeyword: toast.maketext(getapplicationcontext(), "set keyword set trigger sms checker", toast.length_short).show(); break; case r.id.lblnumber: toast.maketext(getapplicationcontext(), "set number app communicate in case of emmergency", toast.length_short).show(); break; case r.id.lbladdress: toast.maketext(getapplicationcontext(), "set home address\nex. agias lavras 19, piraeus, greece", toast.length_short).show(); break; case r.id.btnok: if((txtkeyword.gettext().tostring().equals("")) || (txtaddress.gettext().tostring().equals("")) ||(txtnumber.gettext().tostring().equals(""))){ toast.maketext(this, "please fill text fields.", toast.length_short).show(); } else{ savepreferences(); keyword = txtkeyword.gettext().tostring(); finish(); } break; } } private void savepreferences() { //thewrithike anagkaio na apothikeutoun ta stoixeia se ena arxeio gia na mi xathoun. try{ outputstreamwriter outfile = new outputstreamwriter(openfileoutput(filename, 0)); outfile.write(txtkeyword.gettext().tostring() + "\n" + txtnumber.gettext().tostring() + "\n" + txtaddress.gettext().tostring()); outfile.close(); toast.maketext(this, "the contents saved in file.", toast.length_long).show(); } catch(throwable t) { toast.maketext(this, "exception: "+t.tostring(), toast.length_long).show(); } } private void updateuifromfiles() { try { inputstream infile = openfileinput(filename); if (infile != null){ inputstreamreader tmp = new inputstreamreader(infile); bufferedreader reader = new bufferedreader(tmp); txtkeyword.settext(reader.readline()); txtnumber.settext(reader.readline()); txtaddress.settext(reader.readline()); infile.close(); } } catch (java.io.filenotfoundexception e) { // that's ok, haven't created yet } catch (throwable t) { toast.maketext(this, "exception: "+t.tostring(), toast.length_long).show(); } } public string getmessage(){ //methodos gia na parei service keyword. return keyword; } }
the problem is
06-19 22:17:03.789: e/androidruntime(17204): fatal exception: main 06-19 22:17:03.789: e/androidruntime(17204): java.lang.runtimeexception: unable start activity componentinfo{com.example.oldie/com.example.oldie.appsettings}: java.lang.nullpointerexception 06-19 22:17:03.789: e/androidruntime(17204): @ android.app.activitythread.performlaunchactivity(activitythread.java:1967) 06-19 22:17:03.789: e/androidruntime(17204): @ android.app.activitythread.handlelaunchactivity(activitythread.java:1992) 06-19 22:17:03.789: e/androidruntime(17204): @ android.app.activitythread.access$600(activitythread.java:127) 06-19 22:17:03.789: e/androidruntime(17204): @ android.app.activitythread$h.handlemessage(activitythread.java:1158) 06-19 22:17:03.789: e/androidruntime(17204): @ android.os.handler.dispatchmessage(handler.java:99) 06-19 22:17:03.789: e/androidruntime(17204): @ android.os.looper.loop(looper.java:137) 06-19 22:17:03.789: e/androidruntime(17204): @ android.app.activitythread.main(activitythread.java:4441) 06-19 22:17:03.789: e/androidruntime(17204): @ java.lang.reflect.method.invokenative(native method) 06-19 22:17:03.789: e/androidruntime(17204): @ java.lang.reflect.method.invoke(method.java:511) 06-19 22:17:03.789: e/androidruntime(17204): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:784) 06-19 22:17:03.789: e/androidruntime(17204): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:551) 06-19 22:17:03.789: e/androidruntime(17204): @ dalvik.system.nativestart.main(native method) 06-19 22:17:03.789: e/androidruntime(17204): caused by: java.lang.nullpointerexception 06-19 22:17:03.789: e/androidruntime(17204): @ com.example.oldie.appsettings.oncreate(appsettings.java:39) 06-19 22:17:03.789: e/androidruntime(17204): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1049) 06-19 22:17:03.789: e/androidruntime(17204): @ android.app.activitythread.performlaunchactivity(activitythread.java:1931) 06-19 22:17:03.789: e/androidruntime(17204): ... 11 more
the code intent doesn't work @ all. app crashes , ends. i'm 1 step before taking pills control anger... i' don't know what's wrong , intent doesn't work. please me.
in appsettings activity, setting content view r.layout.activity_main. i'm guessing not intended. not have textviews expect find. calls find them return null, causes exception when invoke setonclicklistener().
Comments
Post a Comment