来自内部处理程序的类之间的SharedPreferences问题

时间:2013-11-21 14:21:05

标签: android sharedpreferences

我的目标是将restpost变量放入第二个类,因此可以通过httppost发送。 已经在使用的SharedPreferences对象用于用户设置。我用谷歌搜索了至少100次,并在这里检查了每个相关的帖子。

主要活动包含:

        private final Handler mHandler = new Handler() {
    @SuppressWarnings("deprecation")
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
        case MESSAGE_STATE_CHANGE:
            if(D) Log.i(TAG, "MESSAGE_STATE_CHANGE: " + msg.arg1);
            switch (msg.arg1) {
            case BTService.STATE_CONNECTED:
                setStatus(getString(R.string.title_connected_to,mConnectedDeviceName));
                mConversationArrayAdapter.clear();
                break;
    ... //then...eventually this..

                        Editor editor = sharedPrefs.edit();
                editor.putString("restpost", restpost);
                editor.commit();
            HttpService sendNow = new HttpService();
            sendNow.sendData();

第二节课在下面。

    public class HttpService extends PreferenceActivity{


public void onCreate(Bundle savedInstanceState) {
    //  super.onCreate(savedInstanceState);

}

public void sendData(){

    SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
    String host = sharedPrefs.getString("host_text", "NULL");
    String muser = sharedPrefs.getString("user_text", "NULL");
    String mpw = sharedPrefs.getString("pw_text", "NULL");
    String maxurl = sharedPrefs.getString("restpost", "NULL");

    HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(maxurl);
    HttpContext localContext = new BasicHttpContext();
    HttpResponse response;
    try {
        response = httpClient.execute(httpPost);
        Log.d("Http Response:", response.toString());
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
        }
    }

最终目标是在主动BT连接期间执行http事件。这是基于BluetoothChat示例,如果这有帮助。 我也有一个处理程序&定时任务运行以重复执行BT消息 - 每分钟不超过1次。

这是错误:

11-21 09:08:30.395:E / AndroidRuntime(5012):java.lang.NullPointerException 11-21 09:08:30.395:E / AndroidRuntime(5012):在android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:160) 11-21 09:08:30.395:E / AndroidRuntime(5012):在android.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:366) 11-21 09:08:30.395:E / AndroidRuntime(5012):at com.clicsys.motomon.HttpService.sendData(HttpService.java:29) 11-21 09:08:30.395:E / AndroidRuntime(5012):at com.clicsys.motomon.MainActivity $ 3.handleMessage(MainActivity.java:467) 11-21 09:08:30.395:E / AndroidRuntime(5012):在android.os.Handler.dispatchMessage(Handler.java:99) 11-21 09:08:30.395:E / AndroidRuntime(5012):在android.os.Looper.loop(Looper.java:137) 11-21 09:08:30.395:E / AndroidRuntime(5012):在android.app.ActivityThread.main(ActivityThread.java:4950) 11-21 09:08:30.395:E / AndroidRuntime(5012):at java.lang.reflect.Method.invokeNative(Native Method) 11-21 09:08:30.395:E / AndroidRuntime(5012):at java.lang.reflect.Method.invoke(Method.java:511) 11-21 09:08:30.395:E / AndroidRuntime(5012):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1004) 11-21 09:08:30.395:E / AndroidRuntime(5012):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771) 11-21 09:08:30.395:E / AndroidRuntime(5012):at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

HttpService sendNow = new HttpService();

// ...

public class HttpService extends PreferenceActivity

您无法使用new实例化活动(您可以但它们未正确初始化)。通过Intent实例化活动,并在Context或生命周期的后期将其用作onCreate()

在您的情况下,似乎HttpService根本不应该是一项活动。将有效的Context对象作为参数传递给sendData(),然后使用它代替this