Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Create方法,该方法返回在applucation开始时获得的值_Android_Integer_Return_Call_Oncreate - Fatal编程技术网

Android Create方法,该方法返回在applucation开始时获得的值

Android Create方法,该方法返回在applucation开始时获得的值,android,integer,return,call,oncreate,Android,Integer,Return,Call,Oncreate,我正在开发一个应用程序,我想知道当用户从一个非onCreate()的方法启动我的应用程序时,他拥有多少卷。我已经基于onCreate中的当前卷创建了一个int,但是由于它不能返回任何内容,我不知道如何从那里获取int。使用在onCreate()中生成的int非常重要 如何做到这一点?如果我完全理解您的意思,您应该使用:来保存该值。@pawegio right,如果您想使用用户设置的相同音量级别,请使用首选项。 这是如何: 写入首选项 SharedPreferences pref =

我正在开发一个应用程序,我想知道当用户从一个非onCreate()的方法启动我的应用程序时,他拥有多少卷。我已经基于onCreate中的当前卷创建了一个int,但是由于它不能返回任何内容,我不知道如何从那里获取int。使用在onCreate()中生成的int非常重要


如何做到这一点?

如果我完全理解您的意思,您应该使用:来保存该值。

@pawegio right,如果您想使用用户设置的相同音量级别,请使用首选项。 这是如何:

写入首选项

SharedPreferences pref =
            context.getSharedPreferences("MyAppPreferences", Context.MODE_PRIVATE);

    /*
     * Get the editor for this object. The editor interface abstracts the implementation of
     * updating the SharedPreferences object.
     */

    SharedPreferences.Editor editor = pref.edit();

    /*
     * Write the keys and values to the Editor
     */

    editor.putInt("VolumLevel", 60);
    /*
     * Commit the changes. Return the result of the commit.
     */

    e.commit();
 SharedPreferences pref = context.getSharedPreferences("MyAppPreferences", MODE_PRIVATE);
 int volLevel = pref.getInt("VolumLevel", 50 /*Default if value wasn't setup yet*/);

 return volLevel;
从首选项中读取

SharedPreferences pref =
            context.getSharedPreferences("MyAppPreferences", Context.MODE_PRIVATE);

    /*
     * Get the editor for this object. The editor interface abstracts the implementation of
     * updating the SharedPreferences object.
     */

    SharedPreferences.Editor editor = pref.edit();

    /*
     * Write the keys and values to the Editor
     */

    editor.putInt("VolumLevel", 60);
    /*
     * Commit the changes. Return the result of the commit.
     */

    e.commit();
 SharedPreferences pref = context.getSharedPreferences("MyAppPreferences", MODE_PRIVATE);
 int volLevel = pref.getInt("VolumLevel", 50 /*Default if value wasn't setup yet*/);

 return volLevel;

张贴你已经完成的代码。这将为问题提供背景