SharedPreferences获取“无法执行活动的方法”

时间:2013-02-22 16:44:16

标签: android sharedpreferences

我想存储用户数据和密码,但我总是收到错误"无法执行活动的方法"。

我认为写动作有效:

Editor editor = getSharedPreferences("pref", 0).edit();
editor.putString(CustomizedListView.KEY_USER, username);
editor.putString(CustomizedListView.KEY_PASSWORD, password);
System.out.println("username: " + username);
System.out.println("password: " + password);
editor.commit();

我在阅读偏好时遇到错误:

public class RESTClient extends Activity {

    private ArrayList<NameValuePair> uebergabeParam = new ArrayList<NameValuePair>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
    }

    public String getResponse(String URLParameter, HttpMethod method) {
        System.out.println("RESTClient");

        SharedPreferences pref = getPreferences(MODE_PRIVATE);
            // or
            SharedPreferences pref = getSharedPreferences("pref", MODE_PRIVATE);
        String username = pref.getString(CustomizedListView.KEY_USER, "");
        String password = pref.getString(CustomizedListView.KEY_PASSWORD, "");

    try {
        Client client = new Client();
        client.addParameter(method, uebergabeParam, URLParameter, username , password);
        return client.execute().get();
    } catch (InterruptedException e) {
        System.out.println("RESTClient getResponse " + e);
        e.printStackTrace();
    } catch (ExecutionException e) {
        System.out.println("RESTClient getResponse " + e);
        e.printStackTrace();
    }
    return "";
    }
}

我收到以下错误:

02-22 17:37:58.903: I/System.out(1714): RESTClient
02-22 17:37:58.903: D/AndroidRuntime(1714): Shutting down VM
02-22 17:37:58.903: W/dalvikvm(1714): threadid=1: thread exiting with uncaught exception (group=0x412f42a0)
02-22 17:37:58.918: E/AndroidRuntime(1714): FATAL EXCEPTION: main
02-22 17:37:58.918: E/AndroidRuntime(1714): java.lang.IllegalStateException: Could not execute method of the activity
02-22 17:37:58.918: E/AndroidRuntime(1714):     at android.view.View$1.onClick(View.java:3691)
02-22 17:37:58.918: E/AndroidRuntime(1714):     at android.view.View.performClick(View.java:4211)
02-22 17:37:58.918: E/AndroidRuntime(1714):     at android.view.View$PerformClick.run(View.java:17267)
02-22 17:37:58.918: E/AndroidRuntime(1714):     at android.os.Handler.handleCallback(Handler.java:615)
02-22 17:37:58.918: E/AndroidRuntime(1714):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-22 17:37:58.918: E/AndroidRuntime(1714):     at android.os.Looper.loop(Looper.java:137)
02-22 17:37:58.918: E/AndroidRuntime(1714):     at android.app.ActivityThread.main(ActivityThread.java:4898)
02-22 17:37:58.918: E/AndroidRuntime(1714):     at java.lang.reflect.Method.invokeNative(Native Method)
02-22 17:37:58.918: E/AndroidRuntime(1714):     at java.lang.reflect.Method.invoke(Method.java:511)
02-22 17:37:58.918: E/AndroidRuntime(1714):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
02-22 17:37:58.918: E/AndroidRuntime(1714):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
02-22 17:37:58.918: E/AndroidRuntime(1714):     at dalvik.system.NativeStart.main(Native Method)
02-22 17:37:58.918: E/AndroidRuntime(1714): Caused by: java.lang.reflect.InvocationTargetException
02-22 17:37:58.918: E/AndroidRuntime(1714):     at java.lang.reflect.Method.invokeNative(Native Method)
02-22 17:37:58.918: E/AndroidRuntime(1714):     at java.lang.reflect.Method.invoke(Method.java:511)
02-22 17:37:58.918: E/AndroidRuntime(1714):     at android.view.View$1.onClick(View.java:3686)
02-22 17:37:58.918: E/AndroidRuntime(1714):     ... 11 more
02-22 17:37:58.918: E/AndroidRuntime(1714): Caused by: java.lang.NullPointerException
02-22 17:37:58.918: E/AndroidRuntime(1714):     at android.app.Activity.getLocalClassName(Activity.java:4556)
02-22 17:37:58.918: E/AndroidRuntime(1714):     at android.app.Activity.getPreferences(Activity.java:4589)
02-22 17:37:58.918: E/AndroidRuntime(1714):     at kommunikation.RESTClient.getResponse(RESTClient.java:67)
02-22 17:37:58.918: E/AndroidRuntime(1714):     at Login.onClick(Login.java:124)
02-22 17:37:58.918: E/AndroidRuntime(1714):     ... 14 more

希望你能帮助我不知道为什么。非常感谢!

修改

public class CustomizedListView extends Activity {

    // Login
    public static final String KEY_USER = "user";  
    public static final String KEY_PASSWORD = "password";
}

我也编辑了getReponse()方法并添加了

  

super.onCreate(savedInstanceState);

3 个答案:

答案 0 :(得分:1)

您需要使用super.onCreate()方法拨打onCreate()

  

派生类必须调用超类的此方法的实现。如果他们不这样做,将抛出异常。 - Docs

该异常的根表明您的活动尚未初始化。它没有上下文,很多字段都是null。其中一个原因是您没有调用super.onCreate(),但是只要onCreate()返回,就会抛出一个不同的异常(如上面文档中的行所示)。我怀疑你正在做一些不寻常的事情,比如使用new关键字实例化活动实例并尝试调用此方法,这将无法按预期工作。创建活动时,您需要通过适当的渠道。具体而言,您需要致电startActivity()

答案 1 :(得分:0)

请阅读以下链接,为什么需要拨打super.onCreate(savedInstanceState);

super.onCreate(savedInstanceState);

@Override
public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
}

sharedPreference这样读取保存的数据

public String getResponse(String URLParameter, HttpMethod method) {

    SharedPreferences pref = getPreferences("pref", MODE_PRIVATE);
    String username = pref.getString(CustomizedListView.KEY_USER, "");
    String password = pref.getString(CustomizedListView.KEY_PASSWORD, "");

        ...
}

我认为NPE可能是用户名或密码有空值。使用Debug检查它。

editor.putString(CustomizedListView.KEY_USER, username);
editor.putString(CustomizedListView.KEY_PASSWORD, password);

答案 2 :(得分:0)

我在你的代码中看到的是你正在混合方法getPreferences()getSharedPreferences(),第二个是当你有不同的偏好文件时。我不确定这是否会导致应用程序崩溃。

如果您只使用1个首选项文件,请避免使用方法getSharedPreferences()如果您有多个首选项文件,则必须在编译时指定您要引用的文件。

最终编辑:

您正在从没有Context的方法调用SharedPreferences:

public String getResponse(String URLParameter, HttpMethod method){

   SharedPreferences pref = getSharedPreferences("pref", MODE_PRIVATE);

}

只要它在Activity上使用它:

public String getResponse(String URLParameter, HttpMethod method){

       SharedPreferences pref = getAplicationContext().getSharedPreferences("pref", MODE_PRIVATE);

    }