java - Unable to retrieve the value of textfield -
i newbee in android development. had developed app contains login, credentials must passed in text field , later call webservice. facing issue user , password not getting copied @ required position. please me out.
package com.authorwjf.http_get; import java.io.ioexception; import java.io.inputstream; import java.io.unsupportedencodingexception; import org.apache.http.httpentity; import org.apache.http.httpresponse; import org.apache.http.client.httpclient; import org.apache.http.client.methods.httppost; import org.apache.http.entity.stringentity; import org.apache.http.impl.client.defaulthttpclient; import org.apache.http.protocol.basichttpcontext; import org.apache.http.protocol.httpcontext; import android.app.activity; import android.os.asynctask; import android.os.bundle; import android.util.log; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.edittext; public class main extends activity implements onclicklistener { edittext txtusername; edittext txtpassword; @override public void oncreate(bundle savedinstancestate) { txtusername=(edittext)this.findviewbyid(r.id.edittext1); txtpassword=(edittext)this.findviewbyid(r.id.edittext2); super.oncreate(savedinstancestate); setcontentview(r.layout.main); findviewbyid(r.id.my_button).setonclicklistener(this); } @override public void onclick(view arg0) { button b = (button)findviewbyid(r.id.my_button); b.setclickable(false); new longrunninggetio().execute(); } private class longrunninggetio extends asynctask <void, void, string> { protected string getasciicontentfromentity(httpentity entity) throws illegalstateexception, ioexception { inputstream in = entity.getcontent(); stringbuffer out = new stringbuffer(); int n = 1; while (n>0) { byte[] b = new byte[4096]; n = in.read(b); if (n>0) out.append(new string(b, 0, n)); } return out.tostring(); } @override protected string doinbackground(void... params) { httpclient httpclient = new defaulthttpclient(); httpcontext localcontext = new basichttpcontext(); string user= txtusername.gettext().tostring(); string pass= txtpassword.gettext().tostring(); system.out.println("userrrr"+user); system.out.println(pass); //string user="at@ril.com"; //string pass= "123456"; string accesstokenqry = "{"+ "\"uid\":\""+user+"\","+ "\"password\":\""+pass+"\","+ "\"consumptiondeviceid\":\"fder-et3w-3adw2-2erf\","+ "\"consumptiondevicename\":\"samsung tab\""+ "}"; httppost httppost = new httppost("http://devssg01.ril.com:8080/v2/dip/auth/login"); httppost.setheader("content-type", "application/json"); httppost.setheader("x-api-key", "l7xx7914b8704b2d4b029ab9c4b1b9c66dbf"); stringentity input; try { input = new stringentity(accesstokenqry); httppost.setentity(input); } catch (unsupportedencodingexception e1) { // todo auto-generated catch block e1.printstacktrace(); } string text = null; try { httpresponse response = httpclient.execute(httppost, localcontext); httpentity entity = response.getentity(); text = getasciicontentfromentity(entity); } catch (exception e) { return e.getlocalizedmessage(); } return text; } protected void onpostexecute(string results) { if (results!=null) { edittext et = (edittext)findviewbyid(r.id.my_edit); et.settext(results); } button b = (button)findviewbyid(r.id.my_button); b.setclickable(true); } } }
the logcat output is:
08-10 01:20:23.977: w/dalvikvm(760): threadid=14: thread exiting uncaught exception (group=0x414c4700) 08-10 01:20:23.984: e/androidruntime(760): fatal exception: asynctask #4 08-10 01:20:23.984: e/androidruntime(760): java.lang.runtimeexception: error occured while executing doinbackground() 08-10 01:20:23.984: e/androidruntime(760): @ android.os.asynctask$3.done(asynctask.java:299) 08-10 01:20:23.984: e/androidruntime(760): @ java.util.concurrent.futuretask.finishcompletion(futuretask.java:352) 08-10 01:20:23.984: e/androidruntime(760): @ java.util.concurrent.futuretask.setexception(futuretask.java:219) 08-10 01:20:23.984: e/androidruntime(760): @ java.util.concurrent.futuretask.run(futuretask.java:239) 08-10 01:20:23.984: e/androidruntime(760): @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1080) 08-10 01:20:23.984: e/androidruntime(760): @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:573) 08-10 01:20:23.984: e/androidruntime(760): @ java.lang.thread.run(thread.java:841) 08-10 01:20:23.984: e/androidruntime(760): caused by: java.lang.nullpointerexception 08-10 01:20:23.984: e/androidruntime(760): @ com.authorwjf.http_get.main$longrunninggetio.doinbackground(main.java:66) 08-10 01:20:23.984: e/androidruntime(760): @ com.authorwjf.http_get.main$longrunninggetio.doinbackground(main.java:1) 08-10 01:20:23.984: e/androidruntime(760): @ android.os.asynctask$2.call(asynctask.java:287) 08-10 01:20:23.984: e/androidruntime(760): @ java.util.concurrent.futuretask.run(futuretask.java:234) 08-10 01:20:23.984: e/androidruntime(760): ... 3 more
you trying initialize edittext
s before layout loaded.
if want edittext on layout, must initialize after layout loaded.
here correct code:
@override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); findviewbyid(r.id.my_button).setonclicklistener(this); txtusername=(edittext)this.findviewbyid(r.id.edittext1); txtpassword=(edittext)this.findviewbyid(r.id.edittext2); }
Comments
Post a Comment