如何从另一个活动调用非静态函数?

huangapple go评论65阅读模式
英文:

How to call non static function from another activity?

问题

以下是您要翻译的内容:

我在代码中有一个函数,我想在其他活动中使用这个函数,但我不能将这个函数设为静态,因为每次我添加static关键字时,就会出现错误(错误与“this”有关):

'com.example.memorableplacecs.MainActivity.this' 无法在静态上下文中引用

这是我想要使用的代码:

protected boolean saveArray() {
    SharedPreferences sp = this.getSharedPreferences(SHARED_PREFS_NAME, Activity.MODE_PRIVATE);
    SharedPreferences.Editor mEdit1 = sp.edit();
    Set<String> set = new HashSet<String>();
    set.addAll(mArrayList);
    mEdit1.putStringSet("list", set);
    return mEdit1.commit();
}

谢谢您的帮助。

英文:

I have a function in my code that I want to use in the other activity but I can't make the function static because every time I add static it gives me an error(the error refer to "this") :

&#39;com.example.memorableplacecs.MainActivity.this&#39; cannot be referenced from a static context

this is my code I want to use:

protected boolean saveArray() {
    SharedPreferences sp = this.getSharedPreferences(SHARED_PREFS_NAME, Activity.MODE_PRIVATE);
    SharedPreferences.Editor mEdit1 = sp.edit();
    Set&lt;String&gt; set = new HashSet&lt;String&gt;();
    set.addAll(mArrayList);
    mEdit1.putStringSet(&quot;list&quot;, set);
    return mEdit1.commit();
}

Thank you for your help

答案1

得分: 1

你可以这样将其设置为静态:

private final static String SHARED_PREFS_NAME = "your_name";

public static boolean saveArray(Context context, List<Needed_Class> list) {
    SharedPreferences sp = context.getSharedPreferences(SHARED_PREFS_NAME, Context.MODE_PRIVATE);
    SharedPreferences.Editor mEdit1 = sp.edit();
    Set<String> set = new HashSet<String>();
    set.addAll(list);
    mEdit1.putStringSet("list", set);
    return mEdit1.commit();
}

并从活动中调用:

PrefsHelper.saveArray(this, list);
英文:

You can make it static like this:

private final static String SHARED_PREFS_NAME = &quot;your_name&quot;;

public static boolean saveArray(Context context, List&lt;Needed_Class&gt; list) {
    SharedPreferences sp = context.getSharedPreferences(SHARED_PREFS_NAME, Context.MODE_PRIVATE);
    SharedPreferences.Editor mEdit1 = sp.edit();
    Set&lt;String&gt; set = new HashSet&lt;String&gt;();
    set.addAll(list);
    mEdit1.putStringSet(&quot;list&quot;, set);
    return mEdit1.commit();
}

and call from activity:

PrefsHelper.saveArray(this, list)

答案2

得分: 1

因为一次只能在一个活动中,所以没有必要从另一个活动中调用方法。

您可以创建一个Application,在清单文件中注册它。

然后在其中添加您的静态方法。

然后在一个活动中,您可以像这样调用它:

MyApplication application = ((MyApplication)getApplicationContext());
application.saveArray(params..)
英文:

There is no point to call a method from another activity, because you're on one activity at a time.

You can create an Application, register it in the Manifest.

And add your static method in it.

Then in an activity, you can call it like this:

    MyApplication application = ((MyApplication)getApplicationContext());
    application.saveArray(params..)

答案3

得分: 0

一个静态方法存在于类实例之外,是类定义本身的一部分,而不是实例的一部分。
这意味着当一个方法被声明为静态时,它的执行与Activity或类实例无关,因此它无法使用"this"关键字访问任何局部变量,也不能访问实例引用。

解决方案是将上下文(context)和任何所需数据一起传递给方法:

public static boolean saveArray(final Context context, final List<Needed_Class> data) {
    final SharedPreferences sp = context.getSharedPreferences(SHARED_PREFS_NAME, Activity.MODE_PRIVATE);
    final SharedPreferences.Editor editor = sp.edit();
    final Set<String> set = new HashSet<String>();
    set.addAll(data);
    editor.putStringSet("list", set);
    return editor.commit();
}

注意,由于它被声明为静态,你不再需要在Activity类中拥有这个方法。最好将它移到一个专用的辅助类中。

英文:

A static method exists outside the life of a class instance, is part of the class definition itself, and not the instance.
This means that when a method is declared as static, its execution is not related to an Activity or a class instance, therefore it can't access any local variables, neither instance references using the "this" keyword.

The solution is to pass a context into the method along with any required data:

public static boolean saveArray(final Context context, final List&lt;Needed_Class&gt; data) {
	final SharedPreferences sp = context.getSharedPreferences(SHARED_PREFS_NAME, Activity.MODE_PRIVATE);
	final SharedPreferences.Editor editor = sp.edit();
	final Set&lt;String&gt; set = new HashSet&lt;String&gt;();
	set.addAll(data);
	editor.putStringSet(&quot;list&quot;, set);
	return editor.commit();
}

Notice that as it is declared as static, you no longer need to have this method in your Activity class. It would be better to move it into a dedicated helper class.

huangapple
  • 本文由 发表于 2020年9月25日 23:16:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/64066916.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定