Go Back   Android Forums > Android Development > Application Development
Application Development Dev lounge for our application developers.

New Forums: Nexus 6 | Nexus Player | Nexus 9
test: Reply
 
LinkBack Thread Tools
Old February 2nd, 2010, 07:38 AM   #1 (permalink)
New Member
Thread Author (OP)
 
Join Date: Feb 2010
Posts: 2
 
Device(s): HTC Magic
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts
Default Shared Preferences Help Needed

Okay, I've been charged with creating an SMS application for my College Project, however, I've run into a few difficulties.

I'm able to store text in a shared preferences in my Settings.java, and the information recalls perfectly when the program is paused and then restarted - so I know that code works.

My problem is that when I want to display this information AGAIN in a different file (Send.java) it's not working. Any ideas what I'm doing wrong?

Quote:
Originally Posted by Settings.java
package sms.project;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Settings extends Activity implements OnClickListener {

private Button settingsBtnBack;

public EditText txtInitial;
public EditText txtSurname;
public EditText txtEmail;

public SharedPreferences prefsInitial;
public SharedPreferences prefsSurname;
public SharedPreferences prefsEmail;


public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
settingsBtnBack = (Button)findViewById(R.id.settingsBtnBack);
settingsBtnBack.setOnClickListener(this);

prefsInitial = getPreferences(MODE_WORLD_READABLE);
txtInitial = (EditText)findViewById(R.id.shinitial);
String initial = prefsInitial.getString("initial","");
txtInitial.setText(initial);

txtSurname = (EditText)findViewById(R.id.shsurname);
String surname = prefsInitial.getString("initial","");
txtSurname.setText(surname);

prefsEmail = getPreferences(MODE_WORLD_READABLE);
txtEmail = (EditText)findViewById(R.id.shemail);
String email = prefsEmail.getString("email","");
txtEmail.setText(email);

}

public void onClick(View v) {
if (v==this.settingsBtnBack) {
Intent i = new Intent(this, Main.class);
startActivityForResult(i, 1);
Toast.makeText(getApplicationContext(), "Settings Saved", Toast.LENGTH_SHORT).show();
}
}

public void onPause() {
super.onPause();
SharedPreferences.Editor i = prefsInitial.edit();
String initial = String.valueOf( txtInitial.getText() );
i.putString("initial", initial);
i.commit();


SharedPreferences.Editor e = prefsEmail.edit();
String email = String.valueOf( txtEmail.getText() );
e.putString("email", email);
e.commit();

}
}
Quote:
Originally Posted by Send.java (My problem area)
package sms.project;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Send extends Activity implements OnClickListener {

private Button sendBtnBack;

public EditText sendcheckmessage;

public SharedPreferences prefsInitial;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.send);
sendBtnBack = (Button)findViewById(R.id.sendBtnBack);
sendBtnBack.setOnClickListener(this);


sendcheckmessage = (EditText)findViewById(R.id.sendcheckmessage);
String check = prefsInitial.getString("initial","");
sendcheckmessage.setText(check);

}

public void onClick(View v) {
if (v==this.sendBtnBack) {
Intent i = new Intent(this, Main.class);
startActivityForResult(i, 1);
Toast.makeText(getApplicationContext(), "To Be Coded", Toast.LENGTH_SHORT).show();
}
}
}
Any help would really be appreciated! ^_^

Advertisements
TheAngelsCry is offline  
Reply With Quote
sponsored links
Old February 2nd, 2010, 08:00 AM   #2 (permalink)
Member
 
Join Date: Sep 2009
Location: Birmingham, UK
Posts: 148
 
Device(s): G1 (Developer model), HTC Tattoo, Nexus One.
Carrier: Not Provided

Thanks: 1
Thanked 26 Times in 22 Posts
Default

Hi,

Hopefully I'm not missing something obvious, but in Send.java you don't seem to be assigning anything to your prefsInitial variable.

Unless there's more code that you haven't posted, I'd guess that could be your problem.

This line in Send.java should be giving you a null pointer exception
Code:
String check = prefsInitial.getString("initial","");

Mark
markb is offline  
Reply With Quote
Old February 2nd, 2010, 08:10 AM   #3 (permalink)
New Member
Thread Author (OP)
 
Join Date: Feb 2010
Posts: 2
 
Device(s): HTC Magic
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'm not receiving any error within Eclipse itself. The only error I get is when I try and run Send.java: The application Fault Reporter (process sms.project) has stopped unexpectedly. Please try again.

Plus, that line of code works when it's placed within Settings.java , but when I try and call the same information in a different class, it's not working.

Or am I missing your point completely?



--

Okay, I've realised I'm using an Activity Preference and not a Shared Preference... but these online tutorials are really quite confusing. Can anyone simplify what needs to be done for a Shared Preference to work?
TheAngelsCry is offline  
Last edited by TheAngelsCry; February 2nd, 2010 at 09:30 AM.
Reply With Quote
Old February 2nd, 2010, 11:16 AM   #4 (permalink)
Member
 
Join Date: Sep 2009
Location: Birmingham, UK
Posts: 148
 
Device(s): G1 (Developer model), HTC Tattoo, Nexus One.
Carrier: Not Provided

Thanks: 1
Thanked 26 Times in 22 Posts
Default

This line will cause a crash in Send.java because prefsInitial hasn't been given a value yet, so it will be null.

Code:
String check = prefsInitial.getString("initial","");
If you copy that line somewhere else, where prefsInitial HAS been set and is not null, then it won't crash. But that's pretty irrelevant, as it's the crash in Send.java you need to fix. In Send.java you need to set up the prefsInitial variable before you use it.


You said:
Code:
The only error I get is when I try and run Send.java: 
The application Fault Reporter (process sms.project) has stopped unexpectedly
That will be because of the null pointer exception, caused by using prefsInitial when it has a null value.

I suggest looking at the logs to see error messages and the exception traceback, and then you'll see where it is crashing.

Mark
markb is offline  
Reply With Quote
Old February 12th, 2010, 12:10 AM   #5 (permalink)
New Member
 
SFLeBrun's Avatar
 
Join Date: Feb 2010
Location: USA
Posts: 9
 
Device(s): Motorola Cliq
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts
Default Re: Shared Preferences Help Needed

The first thing that I noticed with the code in your post is that you are not using the getSharedPreferences() API that I know in the first code and never call it in the second Send class. The second part has been mentioned in previous replies so I will not go into that.

The API that I have used is:
Code:
Context.getSharedPreferences( String name, int mode );
Your call is missing the Preference (a.k.a. properties file) name. I am assuming that the call you are using is using a default Preference file. Preferences that are shared within a single application are named (file name). Check to see if there are other classes that implement the getSharedPreferences() interface.

Check out Data Storage | Android Developers for more details.

In one of your replies, you mentioned that the two Activiy classes run as separate applications. The SharedPreferences are not visible between two different applications unless both applications use the same "Shared User ID". This could also be your problem if you are running the two Activity classes in different applications.

You may also want to consider a ContentProvider to handle persistent data shared between your multiple SMS applications. This is more complex that using SharedPreferences so which one is better for your needs depend on how much and what type of data you want to share.
SFLeBrun is offline  
Reply With Quote
Old February 12th, 2010, 12:20 AM   #6 (permalink)
New Member
 
SFLeBrun's Avatar
 
Join Date: Feb 2010
Location: USA
Posts: 9
 
Device(s): Motorola Cliq
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts
Default

The code from your Setting Class uses the private to an Activity Preferences.
Code:
...
public SharedPreferences prefsInitial;
    ...
        prefsInitial = getPreferences(MODE_WORLD_READABLE);
        ...
If you want to share preferences between components of the same application the SharedPreference interface:

Code:
prefsInitial = getContext().getSharedPreferences("MySettings", MODE_WORLD_READABLE);
Even though the getPreferences() returns a SharedPreferences object, the data stored in it is only accessible to that one Activity in that one Application.
SFLeBrun is offline  
Reply With Quote
Old August 28th, 2010, 03:26 PM   #7 (permalink)
New Member
 
Join Date: Aug 2010
Posts: 1
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts
Default

How to use shared preference between two applications
Deepa is offline  
Reply With Quote
Old April 27th, 2011, 02:09 PM   #8 (permalink)
New Member
 
Join Date: Aug 2010
Posts: 6
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello,

Refer following link to have complete understanding about SharedPreference

KP Bird: Android InDepth : SharedPreference

Enjoy....
kpbird is offline  
Reply With Quote
Old August 22nd, 2011, 10:06 AM   #9 (permalink)
New Member
 
Join Date: Aug 2011
Posts: 1
 
Device(s):
Carrier: Not Provided

Thanks: 0
Thanked 0 Times in 0 Posts
Default

KP Bird has a nice artical and lot of detailed explanation. I like it.

To answer Deepa's question :

Use MODE_WORLD_READBLE in the MODE property while you are creating the Shared Preference Vairable. If you want a to the point artical you can check here.
KrishRud is offline  
Reply With Quote
Reply


Go Back   Android Forums > Android Development > Application Development
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -5. The time now is 11:03 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.