简体   繁体   English

Android SharedPreferences多个活动中的多个值

[英]Android SharedPreferences Multiple values in Multiple Activities

In my android application I used sharedpreference to store values in two activites like in first activity to store the values of login. 在我的android应用程序中,我使用了sharedpreference来将值存储在两个活动中,例如在第一个活动中,以存储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. 因此,一旦用户登录,他的用户名和密码将被保存,并且将被引导至计算活动,在此用户需要将其数据填充到多个edittext中,这里我使用了第二个共享首选项来存储edittext值。 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. 就我而言,我能够正确实现登录过程,但是我使用了第二个sharedpreference来存储应用程序崩溃时的edittext值。 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 当按下按钮时,我需要转到第三个活动,即TabLayoutActivity

You are on 4.0 right ? 您使用的是4.0版本吗? No fault of your part - see this issue and for a very nice explanation RuntimeException: Unable to instantiate application 您没有错-请参阅此问题,并获得非常好的解释RuntimeException:无法实例化应用程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM