Android SharedPreferences多个活动中的多个值

时间:2014-01-17 08:57:45

标签: java android sharedpreferences

在我的Android应用程序中,我使用sharedpreference将值存储在两个活动中,例如在第一个活动中存储登录的值。因此,一旦用户登录,他的用户名和密码将被保存,并且他将被路由到计算活动,其中用户需要在几个edittexts中填充他的数据,我使用第二个共享首选项来存储edittext值。在按下显示报告按钮时,用户将被定向到每日预测的下一页。因此,下次当用户想要查看预测时,如果他已经登录,那么他需要跳过上述两个活动并显示结果。在我的情况下,我能够正确地实现登录过程,但我使用第二个共享首选项来存储应用程序崩溃的edittext值。我在下面给出了我的代码。

第一项活动..登录活动

 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);
                            }
                        });
                    }

第二个活动..计算页面

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活动

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);




            }

按下按钮时我需要进入TabLayoutActivity

的第三个活动

1 个答案:

答案 0 :(得分:0)

你在4.0上对吗?没有错误 - 请参阅此issue并获得非常好的解释RuntimeException: Unable to instantiate application