我有一个类,它的所有方法和变量都是静态的。类是这样的:
public class LinkManager {
private static final String TAG = "LinkManager";
private static final String UAT = "http://uat.MY-Domain.com/";
private static final String PRODUCTION = "https://www.MY-Domain.com/api/";
private static String DOMAIN;
private static final String FACEBOOK_PROFILE_IMAGE_URL = "http://graph.facebook.com/#@#/picture?type=large";
private static final String FACEBOOK_WALL_URL = "https://graph.facebook.com/#@#/feed";
private static final String URL_LOGIN = DOMAIN + "login/";
private static final String URL_USER_PROFILE = DOMAIN + "user/";
private static final String URL_VENUE_LIST = DOMAIN + "venues/?centre_lat=#@#¢re_lon=#@@#&radius=#@@@#";
.
.
.
public static void setBackendMode(int mode) {
switch(mode) {
case 0:
DOMAIN = PRODUCTION;
Log.i(TAG, "Backend mode: Production");
break;
case 1:
DOMAIN = UAT;
Log.i(TAG, "Backend mode: UAT");
break;
default:
Log.e(TAG, "Fatal Error!!! Check you backend url. Selected mode is: " + mode);
}
}
public static String getFacebookProfileImageUrl(String userId) {
String str = FACEBOOK_PROFILE_IMAGE_URL;
str = str.replaceAll("#@#", userId);
return str;
}
public static String getFacebookWallUrl(String userId) {
String str = FACEBOOK_WALL_URL;
str = str.replaceAll("#@#", userId);
return str;
}
public static String getLoginUrl() {
return URL_LOGIN;
}
public static String getUserProfileUrl() {
return URL_USER_PROFILE;
}
public static String getVenueListUrl(String lat, String lng, String radius) {
String str = URL_VENUE_LIST;
str = str.replaceAll("#@#", lat);
str = str.replaceAll("#@@#", lng);
str = str.replaceAll("#@@@#", radius);
return str;
}
.
.
.
}
我还有另一门课,这是我的主课。我想DOMAIN
通过这个类初始化变量。因为,我正在为 Android 开发应用程序,onCreate()
所以是第一个调用的方法。这是我的代码:
public class MainScreen extends FragmentActivity {
private static final String TAG = "MainScreen";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG, "*********************************");
Log.i(TAG, "Try to run ------- application...");
Log.i(TAG, "*********************************");
// Set Backend mode (Production or UAT)
LinkManager.setBackendMode(1);
...
}
}
现在,当我运行应用程序日志时会显示以下输出:
06-28 10:00:56.973: I/MainScreen(9014): *********************************
06-28 10:00:56.973: I/MainScreen(9014): Try to run ------- application...
06-28 10:00:56.973: I/MainScreen(9014): *********************************
06-28 10:00:56.973: I/LinkManager(9014): Backend mode: UAT
06-28 10:00:57.023: I/ContentDownloader(9014): Try to open=> nulluser/
第 4 行显示 DOMAIN 变量已使用 UAT 初始化,但之后其他类调用 LinkManager 类 DOMAIN 的方法时包含 null。由于 DOMAIN 是静态变量,我希望不应更改此变量的内容。
什么/我的错误在哪里?任何建议将不胜感激。谢谢