Android 自动登录总是返回Null

Android 自动登录总是返回Null,android,service,nullpointerexception,sharedpreferences,Android,Service,Nullpointerexception,Sharedpreferences,目前,我的应用程序登录时会提示用户输入用户名和密码,然后提交按钮将信息发送到服务和我的服务器: private ServiceConnection mConnection = new ServiceConnection() { public void onServiceConnected(ComponentName className, IBinder service) { imService = ((MessagingService.IMBin

目前,我的应用程序登录时会提示用户输入用户名和密码,然后提交按钮将信息发送到服务和我的服务器:

    private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {

            imService = ((MessagingService.IMBinder) service).getService();

            if (imService.isUserAuthenticated() == true) {

                Intent i = new Intent(LoggingIn.this, MainActivity.class);
                startActivity(i);
                LoggingIn.this.finish();
            }
        }

        public void onServiceDisconnected(ComponentName className) {

            imService = null;
            Toast.makeText(LoggingIn.this, R.string.local_service_stopped,
                    Toast.LENGTH_SHORT).show();
        }
    };

        /*
         * Start and bind the imService
         */
        startService(new Intent(LoggingIn.this, MessagingService.class)); 

        usernameText = (EditText) findViewById(R.id.username);
        passwordText = (EditText) findViewById(R.id.password);

        loginButton.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {
                if (imService == null) {
                    Toast.makeText(getApplicationContext(),
                            R.string.not_connected_to_service,
                            Toast.LENGTH_LONG).show();
                    // showDialog(NOT_CONNECTED_TO_SERVICE);
                    return;
                } else if (imService.isNetworkConnected() == false) {
                    Toast.makeText(getApplicationContext(),
                            R.string.not_connected_to_network,
                            Toast.LENGTH_LONG).show();
                    // showDialog(NOT_CONNECTED_TO_NETWORK);

                } else if (usernameText.length() > 0
                        && passwordText.length() > 0) {

                    Thread loginThread = new Thread() {
                        private Handler handler = new Handler();

                        @Override
                        public void run() {
                            String result = null;

                            try {
                                result = imService.authenticateUser(
                                        usernameText.getText().toString().trim(),
                                        passwordText.getText().toString().trim());
                            } catch (UnsupportedEncodingException e) {

                                e.printStackTrace();
                            }
                            if (result == null
                                    || result.equals(AUTHENTICATION_FAILED)) {
                                /*
                                 * Authenticatin failed, inform the user
                                 */
                                handler.post(new Runnable() {
                                    public void run() {
                                        Toast.makeText(
                                                getApplicationContext(),
                                                R.string.make_sure_username_and_password_correct,
                                                Toast.LENGTH_LONG).show();

                                        // showDialog(MAKE_SURE_USERNAME_AND_PASSWORD_CORRECT);
                                    }
                                });

                            } else {

                                /*
                                 * if result not equal to authentication failed,
                                 * result is equal to friend and group list of
                                 * the user 0: is for friends, 1: is for groups
                                 */
                                handler.post(new Runnable() {
                                    public void run() {

                                        // If log in successful, then save
                                        // username and password to shared
                                        // preferences:

                                        SaveSharedPreference.setUserName(
                                                getApplicationContext(),
                                                usernameText.getText()
                                                        .toString());

                                        SaveSharedPreference.setPassword(
                                                getApplicationContext(),
                                                passwordText.getText()
                                                        .toString());

                                        Intent i = new Intent(LoggingIn.this,
                                                MainActivity.class);
                                        startActivity(i);
                                        LoggingIn.this.finish();

                                    }
                                });

                            }

                        }
                    };
                    loginThread.start();

                } else {
                    /*
                     * Username or Password is not filled, alert the user
                     */
                    Toast.makeText(getApplicationContext(),
                            R.string.fill_both_username_and_password,
                            Toast.LENGTH_LONG).show();
                    // showDialog(FILL_BOTH_USERNAME_AND_PASSWORD);
                }   
            }
        });
这很好用。我希望实现的是用户的自动登录,因为他们已经登录,因此用户名和密码存储在共享首选项中

因此,当他们再次访问应用程序时,应用程序应自动将他们登录为:

  private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {
        imService = ((MessagingService.IMBinder) service).getService();

        if (imService.isUserAuthenticated() == true) {

            Intent i = new Intent(LoggingIn.this, MainActivity.class);
            startActivity(i);
            LoggingIn.this.finish();
        }
    }

    public void onServiceDisconnected(ComponentName className) {

        imService = null;
        Toast.makeText(LoggingIn.this, R.string.local_service_stopped,
                Toast.LENGTH_SHORT).show();
    }
};

        startService(new Intent(LoggingIn.this, MessagingService.class));

        final String userName = SaveSharedPreference.getUserName(getApplicationContext());

        final String password = SaveSharedPreference.getPassword(getApplicationContext());

        Thread loginThread = new Thread() {
            private Handler handler = new Handler();

            @Override
            public void run() {
                String result = null;

                try {
                    result = imService.authenticateUser(
                            userName.trim(),
                            password.trim());
                } catch (UnsupportedEncodingException e) {

                    e.printStackTrace();
                }
                if (result == null
                        || result.equals(AUTHENTICATION_FAILED)) {
                            /*
                             * Authenticatin failed, inform the user
                             */
                    handler.post(new Runnable() {
                        public void run() {
                            Toast.makeText(
                                    getApplicationContext(),
                                    R.string.make_sure_username_and_password_correct,
                                    Toast.LENGTH_LONG).show();

                            // showDialog(MAKE_SURE_USERNAME_AND_PASSWORD_CORRECT);
                        }
                    });

                } else {

                    handler.post(new Runnable() {
                        public void run() {
                            Intent i = new Intent(LoggingIn.this,
                                    MainActivity.class);
                            startActivity(i);
                            LoggingIn.this.finish();

                        }
                    });

                }

            }
        };
        loginThread.start();
用户信息已成功存储在共享首选项中,但在执行此代码块时,它始终在第行返回NullPointerException:

                    result = imService.authenticateUser(
                            userName.trim(),
                            password.trim());
在try{}catch块中

我尝试在应用程序中放置一个计时器,该计时器可能没有时间启动服务,但即使在超过20秒后,该计时器仍然不起作用

我怎样才能获得自动登录工作而不返回

更新说明:私人经理服务;是一个接口,如下所示:

public interface Manager {

    public boolean isNetworkConnected();

    public boolean isUserAuthenticated();

}

@DanielNugent它在onCreate外部声明,但在类定义内部声明:private ServiceConnection mConnection=new ServiceConnection{好的。关于imService,这是一个实例变量吗?@DanielNugent private Manager imService;是一个接口,并更新了代码您知道它是imService、用户名还是密码是空的吗?@DanielNugent用户名和密码都不是空的,但我不知道如何让它工作,因为我添加的第一个版本是用户启动的单击按钮后,工作正常,