In my android application I used sharedpreference to store values in two activites like in first activity to store the values of login. So once user logs in, his username and password is saved and he will be routed to calculation activity, where user needs to fill his data inside several edittexts here I used second shared preference to store the edittext value. On pressing the show report button, user is directed to the next page that is daily prediction. So next time when user want to see the prediction, if he has logged in then he needs to skip the above said two activities and display the result. In my case I am able to achieve the login process correctly, but I used the second sharedpreference to store the edittext value the app is crashing. I am giving my code below.
first activity .. login activity
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.directcalc_xm);
/*
* Check if we successfully logged in before.
* If we did, redirect to calculation page
*/
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
if (settings.getString("logged", "").toString().equals("logged") && settings.getString("log", "").toString().equals("log"))
{
Intent intent = new Intent(DirectCalculation.this, FullExplanationEntry.class);
startActivity(intent);
}
else
{
btn1 = (Button) findViewById ( R.id.button1);
btn2 = (Button) findViewById ( R.id.button2);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(DirectCalculation.this, SignInActivity.class);
startActivity(intent);
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent in = new Intent(DirectCalculation.this, SignUpActivity.class);
startActivity(in);
}
});
}
second Activity .. calculation page
if(firstName.equals(""))
{
Toast.makeText(getApplicationContext(), "First Name should not be left blank.. Please enter your First Name and try once again.", Toast.LENGTH_LONG).show();
return;
}
else if(lastName.equals(""))
{
Toast.makeText(getApplicationContext(), "Last Name should not be left blank.. Please enter your Last Name and try once again.", Toast.LENGTH_LONG).show();
return;
}
else if(callFirstName.equals(""))
{
Toast.makeText(getApplicationContext(), "Please enter your First Name that is currently used and try once again.", Toast.LENGTH_LONG).show();
return;
}
else
{
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("log", "log");
editor.commit();
Intent i = new Intent(this, TabLayoutActivity.class);
//Personal Year
i.putExtra("name18",sum18 + "");
//Personal Month
i.putExtra("name19",sum19 + "");
//Personal Dya
i.putExtra("name20",sum20 + "");
//Current Pinnacle
i.putExtra("pin", pin + "");
//Current Challenge
i.putExtra("ch", ch + "");
i.putExtra("yearstr", yearstr);
i.putExtra("monthstr", monthstr);
i.putExtra("daystr", daystr);
startActivity(i);
}
}
logcat
01-17 08:37:30.800: D/Single Product Details(1267): {"product":[{"updated_at":"0000-00-00 00:00:00","pyno":"4","pdyno":"4","pmnno":"4","pdaynumber":"This is the prediction for Personal Day Number 4.","pmonthnumber":"This is the prediction for Personal Month Number 4.","pyearnumber":"This is the prediction for Personal Year Number 4.","created_at":"2013-11-28 01:16:49","rthoughtnumber":"This is the prediction for Relational Thought Number 4.","pid":"4","rthno":"4"}],"success":1}
01-17 08:37:31.380: D/Single Product Details(1267): {"product":[{"pinnacle":"This is the prediction for Pinnacle Number 6.","created_at":"2013-11-23 03:08:57","pid":"6","updated_at":"0000-00-00 00:00:00","challenge":"This is the prediction for Challenge Number 6.","pinnum":"6","chnum":"6"}],"success":1}
01-17 08:37:31.461: I/Choreographer(1267): Skipped 37 frames! The application may be doing too much work on its main thread.
01-17 08:37:31.769: I/Choreographer(1267): Skipped 32 frames! The application may be doing too much work on its main thread.
01-17 08:37:32.130: I/Choreographer(1267): Skipped 69 frames! The application may be doing too much work on its main thread.
01-17 08:38:00.560: D/AndroidRuntime(1313): Shutting down VM
01-17 08:38:00.560: W/dalvikvm(1313): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
01-17 08:38:00.699: E/AndroidRuntime(1313): FATAL EXCEPTION: main
01-17 08:38:00.699: E/AndroidRuntime(1313): java.lang.RuntimeException: Unable to instantiate application android.app.Application: java.lang.NullPointerException
01-17 08:38:00.699: E/AndroidRuntime(1313): at android.app.LoadedApk.makeApplication(LoadedApk.java:504)
01-17 08:38:00.699: E/AndroidRuntime(1313): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4364)
01-17 08:38:00.699: E/AndroidRuntime(1313): at android.app.ActivityThread.access$1300(ActivityThread.java:141)
01-17 08:38:00.699: E/AndroidRuntime(1313): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1294)
01-17 08:38:00.699: E/AndroidRuntime(1313): at android.os.Handler.dispatchMessage(Handler.java:99)
01-17 08:38:00.699: E/AndroidRuntime(1313): at android.os.Looper.loop(Looper.java:137)
01-17 08:38:00.699: E/AndroidRuntime(1313): at android.app.ActivityThread.main(ActivityThread.java:5041)
01-17 08:38:00.699: E/AndroidRuntime(1313): at java.lang.reflect.Method.invokeNative(Native Method)
01-17 08:38:00.699: E/AndroidRuntime(1313): at java.lang.reflect.Method.invoke(Method.java:511)
01-17 08:38:00.699: E/AndroidRuntime(1313): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-17 08:38:00.699: E/AndroidRuntime(1313): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-17 08:38:00.699: E/AndroidRuntime(1313): at dalvik.system.NativeStart.main(Native Method)
01-17 08:38:00.699: E/AndroidRuntime(1313): Caused by: java.lang.NullPointerException
01-17 08:38:00.699: E/AndroidRuntime(1313): at android.app.LoadedApk.initializeJavaContextClassLoader(LoadedApk.java:379)
01-17 08:38:00.699: E/AndroidRuntime(1313): at android.app.LoadedApk.getClassLoader(LoadedApk.java:322)
01-17 08:38:00.699: E/AndroidRuntime(1313): at android.app.LoadedApk.makeApplication(LoadedApk.java:496)
01-17 08:38:00.699: E/AndroidRuntime(1313): ... 11 more
SignIn Activity
if(password.equals(storedPassword))
{
Toast.makeText(SignInActivity.this, "Login Successfull", Toast.LENGTH_LONG).show();
/*
* So login information is correct,
* we will save the Preference data
* and redirect to the next activity.
*/
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString("logged", "logged");
editor.commit();
// the data is verified correct and the activity id redirecting to the user details entry page.
Intent i = new Intent(SignInActivity.this,FullExplanationEntry.class);
startActivity(i);
}
and when pressing button I need to go to third activity that is TabLayoutActivity