注册和登录

时间:2015-04-01 20:33:29

标签: android android-volley

我正在使用http://www.androidhive.info/2012/01/android-login-and-registration-with-php-mysql-and-sqlite/

中的教程

当我单击“登录”或“注册”按钮时,应用程序会立即崩溃,并显示空的ponter异常。 LogCat指向代码中的3行(Login.java)。关于如何解决这个问题的任何想法?

loginCheck(email, password);


private void loginCheck(final String email, final String password) {

AppController.getInstance().addToRequestQueue(strReq, tag_string_req);

logcat的

E/AndroidRuntime(6911): java.lang.NullPointerException
E/AndroidRuntime(6911):     at com.smwale.zicaresults1.Login.loginCheck(Login.java:176)
E/AndroidRuntime(6911):     at com.smwale.zicaresults1.Login.access$2(Login.java:108)
E/AndroidRuntime(6911):     at com.smwale.zicaresults1.Login$1.onClick(Login.java:85)
E/AndroidRuntime(6911):     at android.view.View.performClick(View.java:3511)
E/AndroidRuntime(6911):     at android.view.View$PerformClick.run(View.java:14105)
E/AndroidRuntime(6911):     at android.os.Handler.handleCallback(Handler.java:605)
E/AndroidRuntime(6911):     at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(6911):     at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(6911):     at android.app.ActivityThread.main(ActivityThread.java:4424)
E/AndroidRuntime(6911):     at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(6911):     at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(6911):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
E/AndroidRuntime(6911):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
E/AndroidRuntime(6911):     at dalvik.system.NativeStart.main(Native Method)

登录,JAVA

 public class Login extends Activity{

// LogCat tag
    private static final String TAG = Register.class.getSimpleName();
    private Button buttonLogin;
    private Button btnLinkToRegister;
    private EditText emailInput;
    private EditText passwordInput;
    private ProgressDialog pDialog;
    private SessionManager session;

 @Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView (R.layout.activity_login);

    emailInput = (EditText)findViewById(R.id.etLoginEmail);
    passwordInput = (EditText)findViewById(R.id.etLoginPass);
    buttonLogin = (Button)findViewById(R.id.buttonLogin);
    btnLinkToRegister = (Button)findViewById(R.id.buttonToReg);

    //p dialog
    pDialog = new ProgressDialog(this);
    pDialog.setCancelable(false);

    //sessionMan
    session = new SessionManager(getApplicationContext());

    // Check if user is already logged in or not
            if (session.isLoggedIn()) {
                // User is already logged in. Take him to main activity
                Intent intent = new Intent(Login.this, HomeActivity.class);
                startActivity(intent);
                finish();
            }


   buttonLogin.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {

        String email = emailInput.getText().toString();
        String password = passwordInput.getText().toString();

        // Check for empty data in the form
        if (email.trim().length() > 0 && password.trim().length() > 0) {
            // login user
            loginCheck(email, password);
        } else {
            // Prompt user to enter credentials
            Toast.makeText(getApplicationContext(),
                    "Please enter the credentials!", Toast.LENGTH_LONG)
                    .show();
        }
    }
});

// Link to Register Screen
        btnLinkToRegister.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                Intent i = new Intent(getApplicationContext(),
                        Register.class);
                startActivity(i);
                finish();
            }
        });


 }
        private void loginCheck(final String email, final String password) {
            // Tag used to cancel the request
            String tag_string_req = "req_login";

            pDialog.setMessage("Logging in ...");
            showDialog();

            StringRequest strReq = new StringRequest(Method.POST,
                    AppConfig.URL_REGISTER, new Response.Listener<String>() {

                        @Override
                        public void onResponse(String response) {
                            Log.d(TAG, "Login Response: " + response.toString());
                            hideDialog();

                            try {
                                JSONObject jObj = new JSONObject(response);
                                boolean error = jObj.getBoolean("error");

                                // Check for error node in json
                                if (!error) {
                                    // user successfully logged in
                                    // Create login session
                                    session.setLogin(true);

                                    // Launch main activity
                                    Intent intent = new Intent(Login.this,
                                            HomeActivity.class);
                                    startActivity(intent);
                                    finish();
                                } else {
                                    // Error in login. Get the error message
                                    String errorMsg = jObj.getString("error_msg");
                                    Toast.makeText(getApplicationContext(),
                                            errorMsg, Toast.LENGTH_LONG).show();
                                }
                            } catch (JSONException e) {
                                // JSON error
                                e.printStackTrace();
                            }

                        }
                    }, new Response.ErrorListener() {

                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Log.e(TAG, "Login Error: " + error.getMessage());
                            Toast.makeText(getApplicationContext(),
                                    error.getMessage(), Toast.LENGTH_LONG).show();
                            hideDialog();
                        }
                    }) {

                @Override
                protected Map<String, String> getParams() {
                    // Posting parameters to login url
                    Map<String, String> params = new HashMap<String, String>();
                    params.put("tag", "login");
                    params.put("email", email);
                    params.put("password", password);

                    return params;
                }

            };


            // Adding request to request queue
            AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
        }

        private void showDialog() {
            if (!pDialog.isShowing())
                pDialog.show();
        }

        private void hideDialog() {
            if (pDialog.isShowing())
                pDialog.dismiss();
        }

        private void actionBarSetup() {
              if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
                ActionBar ab = getActionBar();
                ab.setTitle("My Results");
                ab.setSubtitle("Login screen"); 
              }
            }}

1 个答案:

答案 0 :(得分:0)

约书亚 我遇到了同样的问题。发现&#34; AppController.getInstance()&#34;正在返回NULL。 可能你错过了 机器人:名称=&#34; info.androidhive.loginandregistration.app.AppController&#34;在清单文件中