Вопрос

i am initializing loader in insideOnclick method of button but failed to do so and getting error becuase of third last parameter when passing "null" it gives me error while passing "this" also failed to do work.

    loginBtn.setOnClickListener(new View.OnClickListener() {
                @Override
            public void onClick(View arg0) {
                    if(userName.getText().length() == 0 || password.getText().length() == 0)
                {
            Toast.makeText(context,"UserName Or Password Should be Filled",Toast.LENGTH_SHORT).show();
            }
        else{
    userName.setText("");
   password.setText("");
   **getLoaderManager().initLoader(1,null,null);** //here is the error 

                    }
                }
                });

            }
             @Override
                public  Loader<User> onCreateLoader(int id,Bundle args) {
                 loginLoader = new LoginLoader(context,userName.getText().toString(),password.getText().toString(),"2013-07-10 01:18:26");
                Log.i("login loader",""+loginLoader);
                 return loginLoader;
                 }

                @Override
                public void onLoadFinished(Loader<User> arg0,User proposals) {
                    Log.i("User",""+proposals.getUsername());
                    }

Here is the error List:

01-27 23:27:33.480: E/AndroidRuntime(7777): FATAL EXCEPTION: main 01-27 23:27:33.480: E/AndroidRuntime(7777): java.lang.NullPointerException 01-27 23:27:33.480: E/AndroidRuntime(7777): at android.app.LoaderManagerImpl.createLoader(LoaderManager.java:544) 01-27 23:27:33.480: E/AndroidRuntime(7777): at android.app.LoaderManagerImpl.createAndInstallLoader(LoaderManager.java:553) 01-27 23:27:33.480: E/AndroidRuntime(7777): at android.app.LoaderManagerImpl.initLoader(LoaderManager.java:607) 01-27 23:27:33.480: E/AndroidRuntime(7777): at com.mrfs.android.surveyapp.activities.LoginActivityService$1.onClick(LoginActivityService.java:58) 01-27 23:27:33.480: E/AndroidRuntime(7777): at android.view.View.performClick(View.java:4211) 01-27 23:27:33.480: E/AndroidRuntime(7777): at android.view.View$PerformClick.run(View.java:17267) 01-27 23:27:33.480: E/AndroidRuntime(7777): at android.os.Handler.handleCallback(Handler.java:615) 01-27 23:27:33.480: E/AndroidRuntime(7777): at android.os.Handler.dispatchMessage(Handler.java:92) 01-27 23:27:33.480: E/AndroidRuntime(7777): at android.os.Looper.loop(Looper.java:137) 01-27 23:27:33.480: E/AndroidRuntime(7777): at android.app.ActivityThread.main(ActivityThread.java:4898) 01-27 23:27:33.480: E/AndroidRuntime(7777): at java.lang.reflect.Method.invokeNative(Native Method) 01-27 23:27:33.480: E/AndroidRuntime(7777): at java.lang.reflect.Method.invoke(Method.java:511) 01-27 23:27:33.480: E/AndroidRuntime(7777): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006) 01-27 23:27:33.480: E/AndroidRuntime(7777): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773) 01-27 23:27:33.480: E/AndroidRuntime(7777): at dalvik.system.NativeStart.main(Native Method) *EDIT CODE: LOGIN LIST SERVICE.JAVA*

public class LoginListService 
{
SurveyDBHelper surveyDBHelper;
private final  static  LoginListService INSTANCE = new LoginListService();
public LoginListService()
{}
public static LoginListService getLoginInstance()
{return INSTANCE;
}
public User getLoginResult(String userName, String password,
            String apkVersion) {
        MultiValueMap<String, Object> formData = new LinkedMultiValueMap<String, Object>();
        formData.add("username", "kong");
        formData.add("password", "kongkong");
        formData.add("apkStatusDate", "2013-07-10 01:18:26"); // **HERE IS THE ERROR** 
        System.out.print("form data values:" + formData);
        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<MultiValueMap<String, Object>>(
                formData, WorkflowRestService.getInstance().getRequestHeaders());
        ResponseEntity<UserListItemHolder> responseEntity = WorkflowRestService
                .getInstance()
                .getRestTemplate()
                .exchange(WorkflowApp.getServicesURL() + "user/logIn",
                        HttpMethod.POST, requestEntity,
                        UserListItemHolder.class);

        Log.i("response Entity Login", "" + responseEntity);
        UserListItemHolder userListItemInstance = responseEntity.getBody();
        Log.i("response Entity Body Location Function",
                "" + responseEntity.getBody());
        if ("true".equals(userListItemInstance.getApkStatus())) {
Log.i("locationInstance.getLocationListItems if", ""+ userListItemInstance.getUserListItems());
            return userListItemInstance.getUserListItems();


        } else {
Log.i("locationInstance.getLocationListItems else", ""+ userListItemInstance.getUserListItems());
            userListItemInstance = null;
            return userListItemInstance.getUserListItems();
        }

    }
Это было полезно?

Решение

Looking at your code and your marker, the NullPointerException occurs because you're passing in a null in the place of a required parameter.

getLoaderManager().initLoader(1,null,null); //here is the error

According to the LoaderManager Docs the callback is required:

Parameters

  • id A unique identifier for this loader. Can be whatever you want. Identifiers re scoped to a particular LoaderManager instance.
  • args Optional arguments to supply to the loader at construction. If a loader already exists (a new one does not need to be created), this parameter will be ignored and the last arguments continue to be used.
  • callback Interface the LoaderManager will call to report about changes in the state of the loader. Required.

It looks like you're already implementing LoaderCallbacks, you just have to pass your implementation to the initLoader method:

getLoaderManager().initLoader(1,null,this);

Другие советы

Your date is in the wrong format. Use this instead:

2013-02-03T06:41:41.000Z

You have to put a T between date and time, a Z to the end, and add milliseconds.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top