sharedpreferences - Declared Shared Preferences in Android causes error when run on Android device? -
the ide doesn't seem detect error. when system run on physical device, system crashes when gets class. seems problem? shared preferences declared? how can fix this?
package com.example.mobile_app; import android.os.bundle; import android.app.activity; import android.content.sharedpreferences; import android.view.menu; import android.view.window; import android.widget.textview; public class post_4 extends activity { static int time = post_1.gettime(), post_error = post_3.get_pos_err(); int percent; float quotient_float = (float)post_error/(float)time; float computation=0; sharedpreferences value = getapplicationcontext().getsharedpreferences("values", mode_private); sharedpreferences.editor editor = value.edit(); textview percentage_display; @override protected void oncreate(bundle savedinstancestate) { requestwindowfeature(window.feature_no_title); super.oncreate(savedinstancestate); setcontentview(r.layout.activity_post_4); percentage_display = (textview)findviewbyid(r.id.textview1); store(); } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.post_4, menu); return true; } private void store(){ computation=value.getfloat("value_quotient", quotient_float)-quotient_float/value.getfloat("value_quotient", quotient_float); if(value.getfloat("value_quotient", quotient_float)==0.0f || quotient_float < value.getfloat("value_quotient", quotient_float)) { editor.putfloat("value_quotient", quotient_float); editor.commit(); if(value.getfloat("value_quotient", quotient_float)==0.0f) percentage_display.settext("system cannot make comparison on first use."); else percentage_display.settext("positive "+math.round(computation*100)+"%."); } else percentage_display.settext("negative "+math.round(computation*100)+"%."); } }
error log:
08-29 01:42:59.390: e/androidruntime(29985): fatal exception: main 08-29 01:42:59.390: e/androidruntime(29985): java.lang.runtimeexception: unable instantiate activity componentinfo{com.example.mobile_app/com.example.mobile_app.post_4}: java.lang.nullpointerexception 08-29 01:42:59.390: e/androidruntime(29985): @ android.app.activitythread.performlaunchactivity(activitythread.java:1991) 08-29 01:42:59.390: e/androidruntime(29985): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2092) 08-29 01:42:59.390: e/androidruntime(29985): @ android.app.activitythread.access$600(activitythread.java:133) 08-29 01:42:59.390: e/androidruntime(29985): @ android.app.activitythread$h.handlemessage(activitythread.java:1203) 08-29 01:42:59.390: e/androidruntime(29985): @ android.os.handler.dispatchmessage(handler.java:99) 08-29 01:42:59.390: e/androidruntime(29985): @ android.os.looper.loop(looper.java:137) 08-29 01:42:59.390: e/androidruntime(29985): @ android.app.activitythread.main(activitythread.java:4797) 08-29 01:42:59.390: e/androidruntime(29985): @ java.lang.reflect.method.invokenative(native method) 08-29 01:42:59.390: e/androidruntime(29985): @ java.lang.reflect.method.invoke(method.java:511) 08-29 01:42:59.390: e/androidruntime(29985): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:789) 08-29 01:42:59.390: e/androidruntime(29985): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:556) 08-29 01:42:59.390: e/androidruntime(29985): @ dalvik.system.nativestart.main(native method) 08-29 01:42:59.390: e/androidruntime(29985): caused by: java.lang.nullpointerexception 08-29 01:42:59.390: e/androidruntime(29985): @ android.content.contextwrapper.getapplicationcontext(contextwrapper.java:101) 08-29 01:42:59.390: e/androidruntime(29985): @ com.example.mobile_app.post_4.<init>(post_4.java:16) 08-29 01:42:59.390: e/androidruntime(29985): @ java.lang.class.newinstanceimpl(native method) 08-29 01:42:59.390: e/androidruntime(29985): @ java.lang.class.newinstance(class.java:1319) 08-29 01:42:59.390: e/androidruntime(29985): @ android.app.instrumentation.newactivity(instrumentation.java:1053) 08-29 01:42:59.390: e/androidruntime(29985): @ android.app.activitythread.performlaunchactivity(activitythread.java:1982) 08-29 01:42:59.390: e/androidruntime(29985): ... 11 more
sharedpreferences value = getapplicationcontext().getsharedpreferences("values", mode_private); sharedpreferences.editor editor = value.edit();
move 2 lines function store. in case when class created try access context of application using getapplicationcontext
function time context wont created wait oncreate
happen , save these 2 values. hope help.
it clear crash log context not created. see line
caused by: java.lang.nullpointerexception 08-29 01:42:59.390: e/androidruntime(29985): @ android.content.contextwrapper.getapplicationcontext(contextwrapper.java:101)
do thing instead.
sharedpreferences value; sharedpreferences.editor editor; @override protected void oncreate(bundle savedinstancestate) { requestwindowfeature(window.feature_no_title); super.oncreate(savedinstancestate); setcontentview(r.layout.activity_post_4); percentage_display = (textview)findviewbyid(r.id.textview1); value = getapplicationcontext().getsharedpreferences("values", mode_private); editor = value.edit(); store(); }
Comments
Post a Comment