Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.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 无法在static方法内的类中获取保存的SharedReferences值_Java_Android - Fatal编程技术网

Java 无法在static方法内的类中获取保存的SharedReferences值

Java 无法在static方法内的类中获取保存的SharedReferences值,java,android,Java,Android,在我的项目中,我想与app link共享我的推荐代码。我已将推荐代码保存在SharedReferences中,但我无法在单独的类中设置该消息 public static SharedPreferences sharedPreferences; public static void shareMyApp(Context context) { String link = "https://play.google.com/store/apps/details?id="+context.ge

在我的项目中,我想与app link共享我的推荐代码。我已将推荐代码保存在SharedReferences中,但我无法在单独的类中设置该消息

public static SharedPreferences sharedPreferences;

public static void shareMyApp(Context context) {

    String link = "https://play.google.com/store/apps/details?id="+context.getPackageName()+"Signup with my referral code.My Code is "+sharedPreferences.getString("member_id",null);

    Intent sharingIntent = new Intent(Intent.ACTION_SEND);
    sharingIntent.setType("text/*");
    sharingIntent.putExtra(Intent.EXTRA_TEXT, link);
    context.startActivity(Intent.createChooser(sharingIntent, "Share Via"));
}
此方法存在于java类中。当调用此方法时,会出现错误

java.lang.NullPointerException:尝试对空对象引用调用接口方法“java.lang.String android.content.SharedReferences.getStringjava.lang.String,java.lang.String”


如何使用app link打印该代码?

您是否初始化了SharedReferences

差不多

sharedPreferences = getSharedPreferences("", Context.MODE_PRIVATE);
在这里,我:

将SharedReferences移动到局部变量 初始化它,使用您在注释中概述的方法,您需要为PREF_NAME添加静态导入,或者修复该位以引用现有的PREF_NAME常量 在发送内容时,请使用text/plain而不是text/*以指定MIME类型
您不初始化SharedReferences,因此它为null。但获得转介代码为null@ArpanSarkar:那么,您可能正在使用不同的SharedReferences。在应用程序的其他地方,您正在使用SharedReferences来保存数据。有很多SharedReferences对象,我不知道您使用的是哪一个。在我的示例中,我展示了一种获取SharedReferences对象的常用方法。正如我所写的,您应该将我的示例中的PreferenceManager.GetDefaultSharedReferencesContext替换为当前用于获取SharedReferences对象的任何代码。什么SharedReferences适合您的应用程序answer@ArpanSarkar:欢迎您将数据保存到PreferenceManager.GetDefaultSharedReferencesContext。这是整个应用程序的常见SharedReferences,也是首选项屏幕使用的内容。public MyAppPrefersManagerContext上下文{SharedReferences=context.GetSharedReferencesPref_名称,PRIVATE_模式;Prefeditor=SharedReferences.edit;}
public static void shareMyApp(Context context) {
    SharedPreferences sharedPreferences = context.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE);
    String link = "https://play.google.com/store/apps/details?id="+context.getPackageName()+"Signup with my referral code.My Code is "+sharedPreferences.getString("member_id",null);

    Intent sharingIntent = new Intent(Intent.ACTION_SEND);
    sharingIntent.setType("text/plain");
    sharingIntent.putExtra(Intent.EXTRA_TEXT, link);
    context.startActivity(Intent.createChooser(sharingIntent, "Share Via"));
}