Note that there are some explanatory texts on larger screens.

plurals
  1. PODeclared Shared Preferences in Android causes error when run on Android device?
    primarykey
    data
    text
    <p>The IDE doesn't seem to detect any error. But when the system is run on the physical device, the system crashes when it gets to this class. What seems to be the problem? Could it be the shared preferences declared? How can I fix this?</p> <pre><code> 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 &lt; 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 by "+Math.round(computation*100)+"%."); } else percentage_display.setText("NEGATIVE by "+Math.round(computation*100)+"%."); } } </code></pre> <p>Error Log:</p> <pre><code>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 to 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): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1991) 08-29 01:42:59.390: E/AndroidRuntime(29985): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2092) 08-29 01:42:59.390: E/AndroidRuntime(29985): at android.app.ActivityThread.access$600(ActivityThread.java:133) 08-29 01:42:59.390: E/AndroidRuntime(29985): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1203) 08-29 01:42:59.390: E/AndroidRuntime(29985): at android.os.Handler.dispatchMessage(Handler.java:99) 08-29 01:42:59.390: E/AndroidRuntime(29985): at android.os.Looper.loop(Looper.java:137) 08-29 01:42:59.390: E/AndroidRuntime(29985): at android.app.ActivityThread.main(ActivityThread.java:4797) 08-29 01:42:59.390: E/AndroidRuntime(29985): at java.lang.reflect.Method.invokeNative(Native Method) 08-29 01:42:59.390: E/AndroidRuntime(29985): at java.lang.reflect.Method.invoke(Method.java:511) 08-29 01:42:59.390: E/AndroidRuntime(29985): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 08-29 01:42:59.390: E/AndroidRuntime(29985): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556) 08-29 01:42:59.390: E/AndroidRuntime(29985): at 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): at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:101) 08-29 01:42:59.390: E/AndroidRuntime(29985): at com.example.mobile_app.Post_4.&lt;init&gt;(Post_4.java:16) 08-29 01:42:59.390: E/AndroidRuntime(29985): at java.lang.Class.newInstanceImpl(Native Method) 08-29 01:42:59.390: E/AndroidRuntime(29985): at java.lang.Class.newInstance(Class.java:1319) 08-29 01:42:59.390: E/AndroidRuntime(29985): at android.app.Instrumentation.newActivity(Instrumentation.java:1053) 08-29 01:42:59.390: E/AndroidRuntime(29985): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1982) 08-29 01:42:59.390: E/AndroidRuntime(29985): ... 11 more </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload