从sharedPreferences获取保存的URL时,应用程序崩溃

时间:2013-12-03 13:03:15

标签: android

我的注册button通过register()调用我的Void doInBackground功能。有了这个,我的应用程序崩溃,但当使用 URL 参数作为静态,在代码中定义,一切正常。

我做错了什么?

static void register(final Context context, final String regId, final String appName) {
    Log.i(TAG, "registering device (regId = " + regId + ")");
    SharedPreferences SERVER_URL = context.getSharedPreferences("URL" , Context.MODE_PRIVATE);

    String serverUrl = SERVER_URL + "register.php";
    Map<String, String> params = new HashMap<String, String>();
    params.put("regId", regId);
    params.put("appName", appName); ...
}

2 个答案:

答案 0 :(得分:0)

此处SERVER_URLSharedPreferences实例,我们需要使用getString()方法获取指定值,试试这个,

static void register(final Context context, final String regId, final String appName) {
    Log.i(TAG, "registering device (regId = " + regId + ")");
    SharedPreferences SERVER_URL = context.getSharedPreferences("URL" , Context.MODE_PRIVATE);

    String serverUrl = SERVER_URL.getString("replace_your_key_value",null) + "/register.php";
    Map<String, String> params = new HashMap<String, String>();
    params.put("regId", regId);
    params.put("appName", appName); ...

答案 1 :(得分:0)

尝试:

 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());

    String serverUrl = prefs.getString("URL") + /"register.php";
Log.i("Server URL " + serverUrl );

而不是代码。

SharedPreferences SERVER_URL = context.getSharedPreferences("URL" , Context.MODE_PRIVATE);
String serverUrl = SERVER_URL + "register.php";