findViewById 在不同类的静态方法中 [英] findViewById in static method in different class
问题描述
在我的 android 片段内部,我对我的应用程序是否在平板电脑上运行进行了以下简单检查(在平板电脑上我打开了 3 个视图,在手机上看不到视图 2 和 3,只有第一个一个看到了)
Inside on of my android fragments I have the following simple check as to whether my app runs on a tablet or not (on a tablet I have 3 views open, view 2 and 3 are not seen on a phone, only the first one is seen)
boolean mDualPane;
View detailsFrame = getActivity().findViewById(R.id.content1);
mDualPane = detailsFrame != null && detailsFrame.getVisibility() == View.VISIBLE;
这在片段本身中工作正常,但我需要在许多片段中进行完全相同的检查,因此我希望将它放在我用于多个类中常用方法的MiscMethods"类中.现在我班上同样的鳕鱼看起来像这样:
This works fine in the fragment itself, but I need the exact same check in many fragments and so I wanted to have it in my "MiscMethods" class that I use for frequently used methods in multiple classes. Now the same cod in my class looks like this:
public class MiscMethods{
public static boolean landscapeChecker(){
boolean mDualPane;
View detailsFrame = getActivity().findViewById(R.id.content1);
mDualPane = detailsFrame != null && detailsFrame.getVisibility() == View.VISIBLE;
return mDualPane;
}
}
getActivity(如果我删除了 findViewById)对于 MiscMethods 类型是未定义的.
getActivity (and if I delete that findViewById) is undefined for the type MiscMethods.
现在我有一个像这里这样的上下文的应用程序类
Now I have an Application class for context like here
public static void ErrorToast(int errorCode) {
String errorString = null;
switch (errorCode) {
case 1:
errorString = App.getContext().getString(R.string.error_tooManyFieldsEmpty);
break;
case 2:
errorString = App.getContext().getString(R.string.error_featureComingSoon);
break;
case 3:
errorString = App.getContext().getString(R.string.error_SwitchBreak);
break;
default:
errorString="Wrong Error Code";
break;
}
Toast errormsg = Toast.makeText(App.getContext(), errorString, Toast.LENGTH_SHORT);
errormsg.setGravity(Gravity.CENTER, 0, 0);
errormsg.show();
}
但是这个 App.getContext() 也没有帮助.
but this App.getContext() also does not help.
我该如何解决这个问题?
How can I fix this?
推荐答案
看起来您正在寻找一个快速而肮脏的解决方案,所以这里有两个:
It looks like you're searching for a quick-and-dirty solution, so here are two:
更改方法签名以将活动作为参数:
Change the method signature's to take the activity as a parameter:
public static boolean LandscapeChecker(Activity activity)
.
在方法中,使用传入的activity
代替getActivity()
.从您的各种活动中调用它,例如 boolean mDualPane = LandscapeChecker(this)
.
In the method, use the passed-in activity
in place of getActivity()
. Call it from your various activities like boolean mDualPane = landscapeChecker(this)
.
子类 Activity
并将方法放在那里.对于此解决方案,您将创建一个类 MyActivity extends Activity
,然后让您的各种活动 extend MyActivity
而不是 extend Activity
.在方法中,使用this
代替getActivity()
.
Subclass Activity
and put the method there instead. For this solution, you would create a class MyActivity extends Activity
, then make your various activities extend MyActivity
instead of extend Activity
. In the method, use this
in place of getActivity()
.
这篇关于findViewById 在不同类的静态方法中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!