Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/219.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
Java 编辑来自其他类的SharedReference值_Java_Android_Sharedpreferences_Android Context - Fatal编程技术网

Java 编辑来自其他类的SharedReference值

Java 编辑来自其他类的SharedReference值,java,android,sharedpreferences,android-context,Java,Android,Sharedpreferences,Android Context,我试图更改在适配器的main活动中设置的SharedReference。所以我想通过MainActivity的一个实例,我可以从那里访问并更改它。单击某个项目时,弹出一个对话框,确认后,变量应存储在共享参考文件中。不幸的是,我在应该编辑此变量的部分中遇到错误: android.content.SharedReferences$Editor 空对象引用上的android.content.SharedReferences.edit() main活动: public class MainActivit

我试图更改在适配器的
main活动中设置的
SharedReference
。所以我想通过
MainActivity
的一个实例,我可以从那里访问并更改它。单击某个项目时,弹出一个对话框,确认后,变量应存储在
共享参考文件中。不幸的是,我在应该编辑此变量的部分中遇到错误:

android.content.SharedReferences$Editor 空对象引用上的android.content.SharedReferences.edit()

main活动

public class MainActivity extends AppCompatActivity {

    SharedPreferences favE;
    int x;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        favE = MainActivity.this.getSharedPreferences("myFavEName",
                Context.MODE_PRIVATE);
        x = favE.getInt("favKey", 0);

    }

    public int getFavE(){
        return x;
    }
}
我的适配器:

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {

    Context context;

    List<GetDataAdapter> getDataAdapter;

    MainActivity ma = new MainActivity();

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {

        GetDataAdapter getDataAdapter1 =  getDataAdapter.get(position);

        final int a = getDataAdapter1.getId();

        int x = ma.getFavE();

        holder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 addDialog(a);       
        });
    }


    public void addDialog(final int a) {
        Context context = this.context;
        LayoutInflater inflater = LayoutInflater.from(this.context);

        AlertDialog.Builder builder = new AlertDialog.Builder(context);

        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {

            }
        });

        builder.setPositiveButton("Exit Group", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {          
                ma.favE.edit().putInt("favKey", 0)
                        .apply();
            }
        });
        builder.show();
    }
}
公共类RecycleServiceAdapter扩展了RecyclerView.Adapter{
语境;
列出getDataAdapter;
MainActivity ma=新的MainActivity();
@凌驾
公共无效onBindViewHolder(ViewHolder,最终int位置){
GetDataAdapter getDataAdapter1=GetDataAdapter.get(位置);
final int a=getDataAdapter1.getId();
int x=ma.getFavE();
holder.cardView.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
对话(a);
});
}
公共无效添加对话框(最终int a){
Context=this.Context;
LayoutInflater充气器=LayoutInflater.from(this.context);
AlertDialog.Builder=新建AlertDialog.Builder(上下文);
setNegativeButton(“取消”,新建DialogInterface.OnClickListener()){
public void onClick(DialogInterface对话框,int-id){
}
});
setPositiveButton(“退出组”,新建DialogInterface.OnClickListener()){
public void onClick(DialogInterface dialog,int-id){
ma.favE.edit().putInt(“favKey”,0)
.apply();
}
});
builder.show();
}
}

您不需要MainActivity的实例。您可以指定一个构造函数,该构造函数将仅从活动中获取上下文以使用共享首选项。因此,当您创建适配器时,您将为适配器提供MainActivity上下文。我认为,在您的情况下,由于您正在创建新实例,因此您使用的上下文先于MainActivity已正确初始化。因此它提供了错误的上下文。 您的构造函数将如下所示

public RecyclerViewAdapter(Context context) {
        this.context = context;
    }

适配器中的
MainActivity
实例不是实例化SharedReferences的同一个实例…@Shark您能给我解释一下吗?是的。您发布的第一个代码片段是在它的
onCreate()中实例化(初始化)SharedReferences成员
favE
method。在您发布的第二个代码片段中,您通过执行
MainActivity ma=new MainActivity();
创建了一个新的
MainActivity
实例-它没有运行它的
onCreate()
方法,因为操作系统尚未启动该活动。因此,它的
favE
成员仍然未初始化(null)如果你真的想这样做,只需在
MainActivity
中设置
favE
静态,但你也应该知道简单的
SharedPrefUtil
类可以在这两种情况下工作,如果你将
static
设置为简单的
MainActivity.get,就可以避免所有这些问题haredPref()
实现了这个技巧,您不需要从适配器实例化活动。最好只将
MainActivity
实例传递到适配器,而不是在那里实例化一个新实例。那么我如何访问变量?当我没有引用它时,我会得到一个
无法解析符号的错误?然后可以调用您的上下文实例上的GetSharedReferences是的,因为上下文将是RecylerViewAdapter类中的一个变量,您可以使用它通过GetSharedReferences(“myFavEName”,Context.MODE\u PRIVATE)访问SharedReferences;啊,好的!要编辑值,我只需使用
Context.favE.edit().putInt(“favKey”,0.apply())不,您可以直接使用favE.edit().putInt(“favKey”,0.apply();您不需要任何上下文