i'm working on android
app , nullpointerexception
. here androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.locking" android:versioncode="1" android:versionname="1.0" > <uses-sdk android:minsdkversion="14" android:targetsdkversion="21" /> <uses-permission android:name="android.permission.internet" /> <uses-permission android:name="android.permission.access_network_state" /> <uses-permission android:name="android.permission.write_external_storage" /> <application android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name=".logonactivity" android:label="@string/app_name" android:launchmode="singletop" android:windowsoftinputmode="adjustpan"> <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <activity android:name=".registeractivity" android:label="@string/app_name" android:launchmode="singletop" android:windowsoftinputmode="adjustpan" /> <activity android:name=".statactivity" android:label="@string/app_name" android:launchmode="singletop" /> <activity android:name=".mainactivity" android:label="@string/app_name" android:launchmode="singletop" android:windowsoftinputmode="adjustpan" /> </application> </manifest>
will post runtime error. here logcat
output:
05-08 01:05:06.261: w/system.err(1609): java.util.concurrent.executionexception: java.lang.nullpointerexception 05-08 01:05:06.271: w/dalvikvm(1609): threadid=12: thread exiting uncaught exception (group=0xb2d23b20) @ java.util.concurrent.futuretask.report(futuretask.java:93) @ java.util.concurrent.futuretask.get(futuretask.java:163) @ android.os.asynctask.get(asynctask.java:483) @ com.locking.statactivity.oncreate(statactivity.java:79) @ android.app.activity.performcreate(activity.java:5231) @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1087) @ android.app.activitythread.performlaunchactivity(activitythread.java:2159) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2245) @ android.app.activitythread.access$800(activitythread.java:135) @ android.app.activitythread$h.handlemessage(activitythread.java:1196) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:136) @ android.app.activitythread.main(activitythread.java:5017) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:515) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:779) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:595) @ dalvik.system.nativestart.main(native method) caused by: java.lang.nullpointerexception @ com.locking.statactivity$getstats.doinbackground(statactivity.java:139) @ com.locking.statactivity$getstats.doinbackground(statactivity.java:1) @ android.os.asynctask$2.call(asynctask.java:288) @ java.util.concurrent.futuretask.run(futuretask.java:237) @ android.os.asynctask$serialexecutor$1.run(asynctask.java:231) @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1112) @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:587) @ java.lang.thread.run(thread.java:841) fatal exception: asynctask #2 process: com.locking, pid: 1609 java.lang.runtimeexception: error occured while executing doinbackground() @ android.os.asynctask$3.done(asynctask.java:300) @ java.util.concurrent.futuretask.finishcompletion(futuretask.java:355) @ java.util.concurrent.futuretask.setexception(futuretask.java:222) @ java.util.concurrent.futuretask.run(futuretask.java:242) @ android.os.asynctask$serialexecutor$1.run(asynctask.java:231) @ java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor.java:1112) @ java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor.java:587) @ java.lang.thread.run(thread.java:841) caused by: java.lang.nullpointerexception @ com.locking.statactivity$getstats.doinbackground(statactivity.java:139) @ com.locking.statactivity$getstats.doinbackground(statactivity.java:1) @ android.os.asynctask$2.call(asynctask.java:288) @ java.util.concurrent.futuretask.run(futuretask.java:237) ... 4 more eglsurfaceattrib not implemented eglsurfaceattrib not implemented android.view.windowleaked: activity com.locking.statactivity has leaked window com.android.internal.policy.impl.phonewindow$decorview{b315c460 v.e..... r......d 0,0-456,144} added here @ android.view.viewrootimpl.<init>(viewrootimpl.java:348) @ android.view.windowmanagerglobal.addview(windowmanagerglobal.java:248) @ android.view.windowmanagerimpl.addview(windowmanagerimpl.java:69) @ android.app.dialog.show(dialog.java:286) @ com.locking.statactivity$getstats.onpreexecute(statactivity.java:108) @ android.os.asynctask.executeonexecutor(asynctask.java:587) @ android.os.asynctask.execute(asynctask.java:535) @ com.locking.statactivity.oncreate(statactivity.java:79) @ android.app.activity.performcreate(activity.java:5231) @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1087) @ android.app.activitythread.performlaunchactivity(activitythread.java:2159) @ android.app.activitythread.handlelaunchactivity(activitythread.java:2245) @ android.app.activitythread.access$800(activitythread.java:135) @ android.app.activitythread$h.handlemessage(activitythread.java:1196) @ android.os.handler.dispatchmessage(handler.java:102) @ android.os.looper.loop(looper.java:136) @ android.app.activitythread.main(activitythread.java:5017) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:515) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:779) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:595) @ dalvik.system.nativestart.main(native method)
here code statactivity
.
public class statactivity extends activity { int userid; int temp; int lockset = 0; int templock; int doorset = 0; string lock_op; string door_op; jsonarray status_user = null; textview lockstat; textview doorstat; textview datestat; string statdisplay = "lock open"; string statdisplay2 = "lock closed"; string doordisplay = "door open"; string doordisplay2 = "door closed"; private static final string tag_success = "success"; private static final string tag_message = "message"; private static final string tag_status = "status"; private static final string tag_lock = "lock_op"; private static final string tag_door = "door_op"; private static final string tag_date = "date_modified"; private progressdialog pdialog; lobbyfunctions lobbyfunction = new lobbyfunctions(); userfunctions userfunction = new userfunctions(); userfunctions socketconnect = new userfunctions(); userfunctions sktconnect = new userfunctions(); protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); intent extras = getintent(); userid = extras.getintextra("userid", 0); temp = extras.getintextra("temp", 0); try { // lobbies json , parse them. wait until async task complete before continuing new getstats().execute().get(); } catch (interruptedexception e) { e.printstacktrace(); } catch (executionexception e) { e.printstacktrace(); } } class getstats extends asynctask<string, string, string>{ @override protected void onpreexecute() { super.onpreexecute(); lockstat = (textview)findviewbyid(r.id.switchstat); pdialog = new progressdialog(statactivity.this); pdialog.setmessage("fetching status.."); pdialog.setindeterminate(false); pdialog.setcancelable(true); pdialog.show(); } @override protected string doinbackground(string... args) { jsonobject json3 = lobbyfunction.getstatus(); try{ int success = json3.getint(tag_success); string message = json3.getstring(tag_message); string status = json3.getstring(tag_status); jsonarray status_user = new jsonarray(status); jsonobject w = status_user.getjsonobject(0); string lock = w.getstring(tag_lock); string door = w.getstring(tag_door); string date = w.getstring(tag_date); if(lock == "0") { lock = statdisplay; } else{ lock = statdisplay2; } if( door == "0") { door = doordisplay; } else{ door = doordisplay2; } lockstat.settext(lock); doorstat.settext(door); }catch(jsonexception e){ e.printstacktrace(); } return null; } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } }
the problem is:
you have initialize textview
lockstat = (textview)findviewbyid(r.id.switchstat);
in onpreexecute()
method.
this line doorstat.settext(door);
giving nullpointer exception
because haven't initialize textview. initialize , keep in mind need update textviews in onpostexecute()
method instead if doinbackground()
.
note : don't use ==
compare strings. instead of use equals()
.
Comments
Post a Comment