Saturday, December 8, 2012

[android-developers] NullPointerException in SharedPreferences Android

package laxman.example.androcloud;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;

public class CloudHome extends TabActivity
{

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
final TabHost tabHost = getTabHost();
tabHost.addTab(tabHost.newTabSpec("tab1")
.setIndicator("",getResources().getDrawable(R.drawable.cloud1))
.setContent(new Intent(this, FileUloadDialog.class)));
tabHost.addTab(tabHost.newTabSpec("tab2")
.setIndicator("",getResources().getDrawable(R.drawable.sharing))
.setContent(new Intent(this, AndroCloud.class)));
tabHost.addTab(tabHost.newTabSpec("tab2")
.setIndicator("",getResources().getDrawable(R.drawable.activity))
.setContent(new Intent(this, AndroCloud.class)));
tabHost.addTab(tabHost.newTabSpec("tab2")
.setIndicator("",getResources().getDrawable(R.drawable.search))
.setContent(new Intent(this, AndroCloud.class)));
}

}
package laxman.example.androcloud;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class Session {

Context context;

public boolean start(String UserName)
{
SharedPreferences pref= context.getSharedPreferences("MyPref",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean("SessionState", true);
editor.putString("SessionName", UserName);
editor.commit();
return true;
}
public String GetSessionName()
{ SharedPreferences pref= context.getSharedPreferences("MyPref", Context.MODE_PRIVATE);
String sessionN=pref.getString("SessionName", null);
return sessionN;
}
public String GetSessionState()
{
SharedPreferences pref= context.getSharedPreferences("MyPref", Context.MODE_PRIVATE);
String SessionState=pref.getString("SessionState", null);

return SessionState;
}
public boolean destroy()
{
SharedPreferences pref= context.getSharedPreferences("MyPref", Context.MODE_PRIVATE);
Editor editor = pref.edit();
editor.clear();
editor.commit();
return true;
}


}
if want full code.see in attachments


--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment