Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7727833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:33:45+00:00 2026-06-01T05:33:45+00:00

I would like to know how to get data from preference activity when my

  • 0

I would like to know how to get data from preference activity when my app starts / resume.
i have a preference activity where the user is able to select the app’s locale, the problem seems that when the app starts it sometimes fails to read the preferences from the main activity which causes a forceclose. here is some code that i have.
Please help, Thanks

 import java.util.Locale;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.graphics.PixelFormat;
import android.graphics.Typeface;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;

import android.widget.ImageButton;
import android.widget.TextView;

public class Main extends Activity {
    /** Called when the activity is first created. */

    String ListPreference;

    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    //                          Locale
    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    private void getPrefs() {  
        // Get the xml/preferences.xml preferences  
        SharedPreferences prefs = PreferenceManager  
                        .getDefaultSharedPreferences(getBaseContext());  
        ListPreference = prefs.getString("listPref", "French");  


        if (ListPreference.equals("1")) {//do something  
            String languageToLoad = "fr";
            Locale locale = new Locale(languageToLoad);
            Locale.setDefault(locale);
            Configuration config = new Configuration();
            config.locale = locale;
            getBaseContext().getResources().updateConfiguration(config,
                    getBaseContext().getResources().getDisplayMetrics());


        }


    if (ListPreference.equals("2")) {//do something  
        String languageToLoad = "en";
        Locale locale = new Locale(languageToLoad);
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        getBaseContext().getResources().updateConfiguration(config,
                getBaseContext().getResources().getDisplayMetrics());

    }
    }
    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
    //                              END locale Code
    //XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        screenOn();
        setup();
        getPrefs();





    PreferenceManager.setDefaultValues(this, R.xml.preference, false);


            this.overridePendingTransition(R.anim.fade_in, R.anim.fade_out);

        }

        private void setup() {

            text1= (TextView) findViewById(R.id.Main_Action_Bar_Text);
            Button1= (ImageButton) findViewById(R.id.sample);
            Button2 = (ImageButton) findViewById(R.id.sample);
            Button3 = (ImageButton) findViewById(R.id.sample);
            Button4 = (ImageButton) findViewById(R.id.sample);
            text2= (TextView) findViewById(R.id.sample);
            text3= (TextView) findViewById(R.id.sample);
            text4= (TextView) findViewById(R.id.sample);
            text5= (TextView) findViewById(R.id.sample);


            Typeface font = Typeface.createFromAsset(getAssets(), "font/kb.ttf");

            }

        @Override
        protected void onResume() {
            super.onResume();
            getPrefs();
            setup();
        }

}

this is the preferences.xml in the res/xml folder

   <?xml version="1.0" encoding="utf-8"?>
    <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
            <PreferenceCategory
                    android:title="Language">
                    <ListPreference
                            android:title="Select Language"

                            android:key="listPref"
                            android:defaultValue="1"
                            android:entries="@array/Language"
                            android:entryValues="@array/listValues" />
            </PreferenceCategory>

            </PreferenceScreen>

error log

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp.information.Main}: 
android.view.InflateException: Binary XML file line #5: Error inflating class java.lang.reflect.Constructor
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
at android.app.ActivityThread.access$1500(ActivityThread.java:117)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
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:842)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #5: Error inflating class java.lang.reflect.Constructor
at android.preference.GenericInflater.createItem(GenericInflater.java:397)
at android.preference.GenericInflater.onCreateItem(GenericInflater.java:417)
at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:428)
at android.preference.GenericInflater.rInflate(GenericInflater.java:481)
at android.preference.GenericInflater.rInflate(GenericInflater.java:493)
at android.preference.GenericInflater.inflate(GenericInflater.java:326)
at android.preference.GenericInflater.inflate(GenericInflater.java:263)
at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:251)
at android.preference.PreferenceManager.setDefaultValues(PreferenceManager.java:444)
at android.preference.PreferenceManager.setDefaultValues(PreferenceManager.java:422)
at com.myapp.information.Main.onCreate(Main.java:168)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
... 11 more
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:415)
at android.preference.GenericInflater.createItem(GenericInflater.java:383)
... 23 more
Caused by: java.lang.NullPointerException
at android.content.res.AssetManager.getResourceTextArray(AssetManager.java:214)
at android.content.res.Resources.getTextArray(Resources.java:361)
at android.content.res.TypedArray.getTextArray(TypedArray.java:628)
at android.preference.ListPreference.<init>(ListPreference.java:49)
... 26 more









    // this is the line that throws exception (Main.java:168) 
// I assume this line is to read the preferences when app starts 

PreferenceManager.setDefaultValues(this, R.xml.preference, false);

thanks again for taking the time.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T05:33:46+00:00Added an answer on June 1, 2026 at 5:33 am

    you can check the preference in onResume, you don’t need to put that in onCreate. You should look at the android life cycle again. It is very fundamental. http://developer.android.com/reference/android/app/Activity.html#onResume()

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to know what events get executed (if any) after a user
I would like to know how to get the name of the property that
I would like to know how to get my UIWebView to show these characters
With the XML below, I would like to know how to get the value
I'm using SQL Server 2005 and would like to know how I can get
I would like to have a class that handles population of lists of data
I would like to make a data manager class that retrieves data from plist,
i would like know some reference. I know i can googling it. but prefer
Would like to know what a programmer should know to become a good at
Would like to know the c# code to actually retrieve the IP type: Static

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.