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

  • Home
  • SEARCH
  • 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 8090231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T19:39:24+00:00 2026-06-05T19:39:24+00:00

i am working on android and trying to read a value from my preference

  • 0

i am working on android and trying to read a value from my preference file.
but when i run.a message appears in emulator.the
the application has stopped unexpectedly
here is my preference file name pref.and it is inres/xml

<?xml version="1.0" encoding="utf-8"?>
    <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
        <CheckBoxPreference android:defaultValue="false"
             android:key="@string/first"/>
    </PreferenceScreen>

this is main activity where i am reding the value.

package code.finalwork;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.EditText;

public class FinalWorkActivity extends Activity {
    private String pref_file="pref.xml";
EditText text=(EditText) findViewById(R.id.editText1);
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        SharedPreferences pref=getSharedPreferences(pref_file,Context.MODE_PRIVATE);
       Boolean val= pref.getBoolean("firstuse", true);
       if(val)
       {
            text.setText("first use");
       }
       else
       {
           text.setText("Not first use");
       }


    }
}

please someone figure out the problem.

  • 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-05T19:39:25+00:00Added an answer on June 5, 2026 at 7:39 pm

    Try this:

    public class FinalWorkActivity extends Activity {
        private String pref_file="pref.xml";
        EditText text;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            text = (EditText) findViewById(R.id.editText1);
            SharedPreferences pref=getSharedPreferences(pref_file,Context.MODE_PRIVATE);
            Boolean val= pref.getBoolean("firstuse", true);
            if(val) {
            text.setText("first use");
            } else {
            text.setText("Not first use");
            }
        }
    }
    

    If it doesn’t fix please post the error log.

    EDIT: remember that so save preferences you need to do:

    SharedPreferences pref = getSharedPreferences(pref_file,Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = pref.edit();          
    editor.putBoolean("firstuse", authToken);
    editor.commit();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to connecting SQL Azure from my android application. The code is working
I'm working on a client-server Android application and trying to figure out how to
I am working on my first Android Application and I am trying to implement
Im trying to read in image file from a server , with the code
hi friends iam trying to upload a file from the android device to the
I'm working on android app and trying to get created_time from facebook JSON .
I am working with the Android Bluetooth Chat sample application. I am trying to
I am trying to add AppWidget into my application, but it seems like android
I am working on my first Android Application. What I am trying to do
I am currently working on an Android app and I am trying to get

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.