Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.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 我无法创建共享首选项文件_Android_Android Fragments_Android Preferences_Android Radiogroup - Fatal编程技术网

Android 我无法创建共享首选项文件

Android 我无法创建共享首选项文件,android,android-fragments,android-preferences,android-radiogroup,Android,Android Fragments,Android Preferences,Android Radiogroup,我无法创建共享首选项文件我已经为此挣扎了2天请帮助,我是新来的。在我的应用程序中,我在不同的屏幕上每个屏幕上都有15个问题,我想存储所选选项的文本,以便将来使用。 我的代码 公共类QuestionPagerFragment扩展了片段{ protected View mView; String pageData[]; //Stores the text to swipe. String optionData[];//Stores the option data. static int rid

我无法创建共享首选项文件我已经为此挣扎了2天请帮助,我是新来的。在我的应用程序中,我在不同的屏幕上每个屏幕上都有15个问题,我想存储所选选项的文本,以便将来使用。 我的代码

公共类QuestionPagerFragment扩展了片段{

protected View mView;

String pageData[];  //Stores the text to swipe.
String optionData[];//Stores the option data.

static int rid;
RadioGroup group;
static ArrayList<Integer> list = new ArrayList();

SharedPreferences prefs;
public static final String MyPREFERENCES = "MyPrefs" ;


@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    mView = inflater.inflate(R.layout.page, container, false);
    return mView;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    prefs = getActivity().getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
    pageData=getResources().getStringArray(R.array.desserts);

    optionData=getResources().getStringArray(R.array.options);

    group = (RadioGroup)mView.findViewById(R.id.group);

    group.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // TODO Auto-generated method stub
            int radioButtonID = group.getCheckedRadioButtonId();
            View radioButton = group.findViewById(radioButtonID);
            rid = group.indexOfChild(radioButton);
            list.add(getArguments().getInt("pos"), rid);
            click();

        }
    });
   ((TextView)mView.findViewById(R.id.textMessage)).setText(pageData[getArguments().getInt("pos")]);
    if(getArguments().getInt("pos") == 14) {
        ((Button)mView.findViewById(R.id.submitbtn)).setVisibility(View.VISIBLE);
        ((Button)mView.findViewById(R.id.submitbtn)).setOnClickListener(new View.OnClickListener() {
               public void onClick(View v){

               }                   
            });
    }

    for(int i = 0; i < group.getChildCount(); i++){
        ((RadioButton) group.getChildAt(i)).setText(optionData[(getArguments().getInt("pos")*4)+i]);
    }      
}



public static void save() {
       if(rid==0){
           MainActivity.FLAG_A++;                  
       }
       else if(rid==1){
           MainActivity.FLAG_B++;    
       }
       else if(rid==2){
           MainActivity.FLAG_C++;    
       }
       else if(rid==3){
           MainActivity.FLAG_D++;    
       }            
}
public void click(){

       SharedPreferences prefs = getActivity().getSharedPreferences( "idValue", 0 );
       SharedPreferences.Editor editor = prefs.edit();
       editor.putString( "idValue", list.get(getArguments().getInt("pos")).toString());
       editor.commit();
}
受保护视图mView;
字符串pageData[];//存储要滑动的文本。
字符串optionData[];//存储选项数据。
静态int-rid;
放射组;
静态ArrayList=新建ArrayList();
共享引用优先权;
公共静态最终字符串MyPREFERENCES=“MyPrefs”;
@可空
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
mView=充气机充气(R.layout.page,容器,false);
返回mView;
}
@凌驾
ActivityCreated上的公共无效(@Nullable Bundle savedinStateCState){
super.onActivityCreated(savedInstanceState);
prefs=getActivity().getSharedReferences(MyPREFERENCES,Context.MODE\u PRIVATE);
pageData=getResources().getStringArray(R.array.desserts);
optionData=getResources().getStringArray(R.array.options);
组=(放射组)mView.findviewbyd(R.id.group);
setOnCheckedChangeListener(新的OnCheckedChangeListener(){
@凌驾
检查更改后的公共无效(RadioGroup组,int checkedId){
//TODO自动生成的方法存根
int radioButtonID=group.getCheckedRadioButtonId();
查看radioButton=group.findViewById(radioButtonID);
rid=group.indexOfChild(单选按钮);
add(getArguments().getInt(“pos”),rid);
单击();
}
});
((TextView)mView.findviewbyd(R.id.textMessage)).setText(pageData[getArguments().getInt(“pos”));
if(getArguments().getInt(“pos”)==14){
((按钮)mView.findviewbyd(R.id.submitbtn)).setVisibility(View.VISIBLE);
((按钮)mView.findviewbyd(R.id.submitbtn)).setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
}                   
});
}
对于(int i=0;i

}

此代码将帮助您存储值

SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE); 
Editor editor = pref.edit();
//将数据存储为键/值对

editor.putBoolean("key_name1", true);           // Saving boolean - true/false
editor.putInt("key_name2", "int value");        // Saving integer
editor.putFloat("key_name3", "float value");    // Saving float
editor.putLong("key_name4", "long value");      // Saving long
editor.putString("key_name5", "string value");  // Saving string

// Save the changes in SharedPreferences
editor.commit(); // commit changes
//获取SharedReferences数据

//如果键的值不存在,则返回第二个参数值-在本例中为null

pref.getBoolean("key_name1", null);         // getting boolean
pref.getInt("key_name2", null);             // getting Integer
pref.getFloat("key_name3", null);           // getting Float
pref.getLong("key_name4", null);            // getting Long
pref.getString("key_name5", null);          // getting String
//从SharedReferences中删除键值

editor.remove("key_name3"); // will delete key key_name3
editor.remove("key_name4"); // will delete key key_name4

// Save the changes in SharedPreferences
editor.commit(); // commit changes
//清除SharedReferences中的所有数据

 editor.clear();
 editor.commit(); // commit changes
请尝试删除此行: SharedReferences prefs=getActivity().GetSharedReferences(“idValue”,0)

尝试将commit()更改为apply(),如下所示:

public void click(){

   SharedPreferences prefs = getActivity().getSharedPreferences( "your.project.package.name", 0 ); // don't use short id because sharedPreferences is a global file
   SharedPreferences.Editor editor = prefs.edit();
   editor.putString( "idValue", list.get(getArguments().getInt("pos")).toString());

   editor.apply();  //Here is the change
}
要阅读您的首选项,请尝试:

 SharedPreferences prefs = this.getSharedPreferences("your.project.package.name", Context.MODE_PRIVATE);
 String idValue= prefs.getString("idValue", null); 

共享优先权不是文件这是一种将文本存储在内存中的方法如果您想将其用于其他活动,则可以通过文件访问它,我的意思是我在数据文件夹中找不到SP共享优先权不用于读取数据或从文件中获取数据这用于将值存储在内部应用程序中,仅存储在变量一侧,该变量将始终存储,如果清除它,则内存将关闭not@amitsharma共享优先权是一种存储文本的方式,例如在应用午餐之间,但其实际上是通过将此数据保存到文件来完成的。所以共享首选项是一个文件:)@user user3699550检查是否正确,如果您对我的答案满意,请回答第三链接以了解更多详细信息否,它为getApplicationContext()提供了错误(我使用的是片段)阅读完整帖子这是完整的appamitsharma谢谢buddy@user3699550如果您获得了关于链接或答案的帮助,请检查其是否正确…………这无助于再次定义SharedReferences prefs,因为它涵盖了全局变量,并且您正在处理本地变量,但这不是问题/data/data/YOUR_PACKAGE_NAME/shared_prefs/YOUR_prefs_NAME.xml,但仅限于此若你们有根手机,我相信你们打开它使用私人mod。试着像我建议的那样阅读它,我正在使用private_模式,但数据文件夹中没有任何内容,但我猜它没有保存在文件夹中。我正在获取上一个问题的int值,可能是因为您覆盖了字符串键“idValue”映射的变量。SharedReferences它不是一个列表,而是一个hashMap。如果你想存储另一个值,你需要放置另一个键,如“idValue2”,我不知道如何继续??如果你能在这方面帮助我,这将是非常可观的