0

I want language change feature in my app which can provide user feature to change application language from settings but at start i want default application to open so i created dummy project to test it where i am accessing Langugage preference from a class which is defining factor for language Here its code(Clearly I know I am not using sharedPreference the right way and i know the error was because of that so please help in setting shared preference in this app):

public class LanguagePreferences extends Activity { public int language; int check; SharedPreferences settings = getSharedPreferences("mysettings", Context.MODE_PRIVATE); public LanguagePreferences() { language=settings.getInt("language", 0); } public void changeLang() { check=settings.getInt("language",0); if(check==0) { settings.edit().putInt("language", 1); settings.edit().comment(); } else { settings.edit().putInt("language", 1); settings.edit().comment(); } } } 

and a button to change application Language in settings Here its Code:

protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.change); btnLang=(Button)findViewById(R.id.change); btnLang.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub LanguagePreferences l= new LanguagePreferences(); l.changeLang(); } }); } 

and main Activity where i am changing text value if language is different code:

protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TextView tv = (TextView) findViewById(R.id.text); Button btnLang = (Button) findViewById(R.id.buttonlang); SharedPreferences settings = getSharedPreferences("mysettings", Context.MODE_PRIVATE); btnLang.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent i = new Intent(MainActivity.this, Change.class); startActivity(i); } }); LanguagePreferences l = new LanguagePreferences(); if (l.language == 0) { tv.setText("Germany"); } else { tv.setText("English"); } } 

I am using shared preferences to save data but at start activity closes and not responding error shows. Any Help is appreciated.

Error Log:

09-11 17:44:26.200: D/AndroidRuntime(13952): Shutting down VM 09-11 17:44:26.200: W/dalvikvm(13952): threadid=1: thread exiting with uncaught exception (group=0x414112a0) 09-11 17:44:26.208: E/AndroidRuntime(13952): FATAL EXCEPTION: main 09-11 17:44:26.208: E/AndroidRuntime(13952): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dummy/com.example.dummy.MainActivity}: java.lang.NullPointerException 09-11 17:44:26.208: E/AndroidRuntime(13952): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097) 09-11 17:44:26.208: E/AndroidRuntime(13952): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2122) 09-11 17:44:26.208: E/AndroidRuntime(13952): at android.app.ActivityThread.access$600(ActivityThread.java:140) 09-11 17:44:26.208: E/AndroidRuntime(13952): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1228) 09-11 17:44:26.208: E/AndroidRuntime(13952): at android.os.Handler.dispatchMessage(Handler.java:99) 09-11 17:44:26.208: E/AndroidRuntime(13952): at android.os.Looper.loop(Looper.java:137) 09-11 17:44:26.208: E/AndroidRuntime(13952): at android.app.ActivityThread.main(ActivityThread.java:4895) 09-11 17:44:26.208: E/AndroidRuntime(13952): at java.lang.reflect.Method.invoke(Method.java:511) 09-11 17:44:26.208: E/AndroidRuntime(13952): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994) 09-11 17:44:26.208: E/AndroidRuntime(13952): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761) 09-11 17:44:26.208: E/AndroidRuntime(13952): at dalvik.system.NativeStart.main(Native Method) 09-11 17:44:26.208: E/AndroidRuntime(13952): Caused by: java.lang.NullPointerException 09-11 17:44:26.208: E/AndroidRuntime(13952): at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:101) 09-11 17:44:26.208: E/AndroidRuntime(13952): at com.example.dummy.LanguagePreferences.<init>(LanguagePreferences.java:18) 09-11 17:44:26.208: E/AndroidRuntime(13952): at com.example.dummy.MainActivity.onCreate(MainActivity.java:36) 09-11 17:44:26.208: E/AndroidRuntime(13952): at android.app.Activity.performCreate(Activity.java:5163) 09-11 17:44:26.208: E/AndroidRuntime(13952): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094) 09-11 17:44:26.208: E/AndroidRuntime(13952): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2061) 09-11 17:44:26.208: E/AndroidRuntime(13952): ... 11 more 09-11 17:44:43.325: I/Process(13952): Sending signal. PID: 13952 SIG: 9 
5
  • sorry added edit().commit on code but still closing Commented Sep 11, 2015 at 11:18
  • If i remove LanguagePreferences l = new LanguagePreferences(); from main activity and remove its dependency error gone i know its something related with java but i am beginner in java please any suggestion Commented Sep 11, 2015 at 11:19
  • It is pretty clear you have a NullPointerException. See the 4th line of your error log at the end. This means that you perform an operation on an object with the null value. Usually this is because you call a method on it. Check your LanguagePreference line 18. Commented Sep 11, 2015 at 12:30
  • About in the middle of your log you see Caused by: java.lang.NullPointerException. From there go down each line until you see a class of your own. There you see it says LanguagePreferences.java:18. That means you need to check that file on line 18 ;) Commented Sep 11, 2015 at 12:34
  • this line is causing error and i know it by debugging the also you confirm that SharedPreferences settings=getSharedPreferences("myfile", Context.MODE_PRIVATE); but can you please tell me is this not the right way use shared preference Commented Sep 11, 2015 at 12:40

2 Answers 2

1

As mentioned in the comments, most likely the settings variable is the problem.

Try initializing it in your onCreate() method.

@Override protected void onCreate(Bundle state){ super.onCreate(state); SharedPreferences settings = getSharedPreferences("mysettings", Context.MODE_PRIVATE); } 

You can find more information on how to use these shared preferences here.


From your example:

 LanguagePreferences l = new LanguagePreferences(); 

You are creating a new Activity object here. You're not supposed to do that. Activities are managed by android. You should use the lifecycle methods and callbacks to interact with them. Store the language only in the shared preferences, not in a member variable of the activity.

Sign up to request clarification or add additional context in comments.

Comments

0

You are Trying to create an instance of an Activity with at the line below, which is wrong.

 LanguagePreferences l = new LanguagePreferences(); 

You cannot reach the activity's method like this. You can Add an Application Class and write a public static method like:

public class ApplicationClass extends Application { public static SharedPreferences mSharedPrefs; public static SharedPreferences.Editor mPrefsEditor; private static ApplicationClass instance; public ApplicationClass() { instance = this; } @Override public void onCreate() { super.onCreate(); mSharedPrefs = getSharedPreferences("mysettings", MODE_PRIVATE); mPrefsEditor = mSharedPrefs.edit(); } public static SharedPreferences getmSharedPrefs() { if (mSharedPrefs == null) { mSharedPrefs = getContext().getSharedPreferences("mysettings", MODE_PRIVATE); } return mSharedPrefs; } public static SharedPreferences.Editor getmPrefsEditor() { if (mPrefsEditor == null || mSharedPrefs == null) { mSharedPrefs = getContext().getSharedPreferences("mysettings", MODE_PRIVATE); mPrefsEditor = mSharedPrefs.edit(); } return mPrefsEditor; } } 

And you can use this like:

ApplicationClass.getmPrefsEditor().putString("key","value").commit(); 

And this:

ApplicationClass.getmSharedPrefs().getString("key"); 

in your Activities and Fragments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.