0

I want to store user data and a password, but I always get an error "Could not execute method of the activity".

I think the write actions works:

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();

I get an error with reading the preferences:

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 "";
    }
}

I get the following error:

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

Hope you can help I don't know why. Thank a lot!

EDIT:

public class CustomizedListView extends Activity {

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

I edited the getReponse() method too and added the

super.onCreate(savedInstanceState);

user2047688
  • 45
  • 1
  • 10

3 Answers3

1

You need to call super.onCreate() in your onCreate() method.

Derived classes must call through to the super class's implementation of this method. If they do not, an exception will be thrown. -Docs

The root of that exception indicates that your activity has not yet been initialized. It has no context, and a lot of its fields are null. One of the reasons is that you're not calling super.onCreate(), however this would have thrown a different exception as soon as onCreate() returned (as the line from the doc above indicates). I suspect that you're doing something unusual like instantiating an instance of the activity using the new keyword and attempting to call this method which won't work as expected. You need to go through the proper channels when creating an activity. Specifically you need to call startActivity().

Samuel
  • 16,923
  • 6
  • 62
  • 75
  • When I set the `String username = "mm";` manually the Activity works fine. The problem should be really the getPreferences – user2047688 Feb 22 '13 at 17:16
  • Yes, that's because setting the string doesn't require a context. Getting preferences requires a valid context, and your activity isn't a valid context unless you use the channels that Android expects. – Samuel Feb 22 '13 at 18:18
0

Read from below link why is necessary to call super.onCreate(savedInstanceState);

super.onCreate(savedInstanceState);

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

Read saved data from sharedPreference like this

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, "");

        ...
}

I think NPE might username or password have null value. Check it using Debug.

editor.putString(CustomizedListView.KEY_USER, username);
editor.putString(CustomizedListView.KEY_PASSWORD, password);
Community
  • 1
  • 1
Ajay S
  • 48,003
  • 27
  • 91
  • 111
  • @user2047688 what is written on kommunikation.RESTClient.getResponse(RESTClient.java:67) – Ajay S Feb 22 '13 at 17:13
  • Yes, I added the whole code and trials. `getPreferences("password", MODE_PRIVATE);` doesn't work with the first String statement – user2047688 Feb 22 '13 at 17:14
  • @user2047688 you have not initialize the uebergabeParam. – Ajay S Feb 22 '13 at 17:16
  • @user2047688 Debug you application some object have NPE and you are trying to access it If not solve let me know as soon as possible. – Ajay S Feb 22 '13 at 17:27
0

what i can see in your code is that you are mixing methods getPreferences() and getSharedPreferences(), the second one is when you have diferent files of preferences. im not sure if this will cause the aplication to crash.

if your are working with only 1 preferences file avoid using the method getSharedPreferences() if you do have more than 1 preferences files you must specify at compile time wich file you are refering to.

final edit:

you are calling SharedPreferences from a method that does not have a Context:

public String getResponse(String URLParameter, HttpMethod method){

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

}

as long as its on a Activity use this:

public String getResponse(String URLParameter, HttpMethod method){

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

    }
Eliud
  • 61
  • 5