• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Unable to retrieve String value from shared preferences.

here my code...

Java:
package com.example.haider885.counter;

import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;


public class MainActivity extends ActionBarActivity {

    public String value;
    public int counter = Integer.parseInt(value);
  //  Integer.parseInt(getPreference("tvalue"))
  //  private TextView tv;
    public static final String PREFS_NAME = "CounterFile";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


       Typeface type = Typeface.createFromAsset(getAssets(),"fonts/256BYTES.TTF");
        TextView tv = (TextView) findViewById(R.id.textView1);
        tv.setTypeface(type);

        tv = (TextView) findViewById(R.id.textView1);
        SharedPreferences setting = getSharedPreferences(PREFS_NAME, 0);
        tv.setText(setting.getString("tvalue","000"));
        value = setting.getString("tvalue", "000");


    }


    public void ClickButton(View view)
    {
        counter++;
        TextView tv1 = (TextView) findViewById(R.id.textView1);
        tv1.setText("00" + String.valueOf(counter));
    }


    public  void ResetButton(View view)
    {

        counter = 0;
        TextView tv1 = (TextView) findViewById(R.id.textView1);
        tv1.setText("00");
    }

    public void setCounter()
    {
        TextView tv = (TextView) findViewById(R.id.textView1);
        SharedPreferences setting = getSharedPreferences(PREFS_NAME, 0);
        SharedPreferences.Editor editor = setting.edit();
        editor.putString("tvalue",tv.getText().toString());
        editor.commit();
     }

  /* public String getPreference(String key) {
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        String value = settings.getString(key, "000");
        return value;
    }*/


    @Override
    protected void onStop() {
        super.onStop();
        setCounter();
    }

}

here logcat...

Code:
06-29 01:56:02.574      880-880/com.example.haider885.counter D/AndroidRuntime﹕ Shutting down VM
06-29 01:56:02.574      880-880/com.example.haider885.counter W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x40015560)
06-29 01:56:02.584      880-880/com.example.haider885.counter E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.haider885.counter/com.example.haider885.counter.MainActivity}: java.lang.NumberFormatException: unable to parse 'null' as integer
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
            at android.app.ActivityThread.access$1500(ActivityThread.java:117)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:123)
            at android.app.ActivityThread.main(ActivityThread.java:3683)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:507)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NumberFormatException: unable to parse 'null' as integer
            at java.lang.Integer.parseInt(Integer.java:356)
            at java.lang.Integer.parseInt(Integer.java:332)
            at com.example.haider885.counter.MainActivity.<init>(MainActivity.java:17)
            at java.lang.Class.newInstanceImpl(Native Method)
            at java.lang.Class.newInstance(Class.java:1409)
            at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
            at android.app.ActivityThread.access$1500(ActivityThread.java:117)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:123)
            at android.app.ActivityThread.main(ActivityThread.java:3683)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:507)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
            at dalvik.system.NativeStart.main(Native Method)

this is Line 17...

public int counter = Integer.parseInt(value);


Please Help....I am beginner
 
it says in the log:
Caused by: java.lang.NumberFormatException: unable to parse 'null' as integer

looking at your code i think you should change the line to
public int counter = 0;
 
it says in the log:
Caused by: java.lang.NumberFormatException: unable to parse 'null' as integer

looking at your code i think you should change the line to
public int counter = 0;
I want to get "counter " value from preferences.....if I do that
public int counter = 0;
it will always start from 0 when app will restart...
 
Back
Top Bottom