doudouchan5830
2015-04-03 09:28
浏览 161

使用volley库登录页面Android

Im trying to set up an android login page using PHP and MySQL. I came across this Google 'Volley.jar' Library and i am trying to implement it but my LoginActivity has errors when getting the details and using the AppController to make a request. I have attached the LoginACtivity and AppController code. Login Activity

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);

    inputUsername = (EditText) findViewById(R.id.username);
    inputPassword = (EditText) findViewById(R.id.password);
    btnLogin = (Button) findViewById(R.id.btnLogin);

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

    // Session manager
    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(LoginActivity.this, MainActivity.class);
        startActivity(intent);
        finish();
    }

    // Login button Click Event
    btnLogin.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            String username = inputUsername.getText().toString();
            String password = inputPassword.getText().toString();

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

    });
}
private void checkLogin(final String username, 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(LoginActivity.this,
                            MainActivity.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("username", username);
            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();
}

AppController

private RequestQueue mRequestQueue;

private static AppController mInstance;

@Override
public void onCreate() {
    super.onCreate();
    mInstance = this;
}

public static synchronized AppController getInstance() {
    return mInstance;
}

public RequestQueue getRequestQueue() {
    if (mRequestQueue == null) {
        mRequestQueue = Volley.newRequestQueue(getApplicationContext());
    }

    return mRequestQueue;
}

public <T> void addToRequestQueue(Request<T> req, String tag) {
    req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
    getRequestQueue().add(req);
}

public <T> void addToRequestQueue(Request<T> req) {
    req.setTag(TAG);
    getRequestQueue().add(req);
}

public void cancelPendingRequests(Object tag) {
    if (mRequestQueue != null) {
        mRequestQueue.cancelAll(tag);
    }
}

And here is my logcat: 04-03 04:16:40.190: E/AndroidRuntime(1099): FATAL EXCEPTION: main 04-03 04:16:40.190: E/AndroidRuntime(1099): Process: com.example.zcasm_learningplatform, PID: 1099 04-03 04:16:40.190: E/AndroidRuntime(1099): java.lang.NullPointerException 04-03 04:16:40.190: E/AndroidRuntime(1099): at com.example.zcasm_learningplatform.LoginActivity.checkLogin(LoginActivity.java:154) 04-03 04:16:40.190: E/AndroidRuntime(1099): at com.example.zcasm_learningplatform.LoginActivity.access$2(LoginActivity.java:87) 04-03 04:16:40.190: E/AndroidRuntime(1099): at com.example.zcasm_learningplatform.LoginActivity$1.onClick(LoginActivity.java:76) 04-03 04:16:40.190: E/AndroidRuntime(1099): at android.view.View.performClick(View.java:4424) 04-03 04:16:40.190: E/AndroidRuntime(1099): at android.view.View$PerformClick.run(View.java:18383) 04-03 04:16:40.190: E/AndroidRuntime(1099): at android.os.Handler.handleCallback(Handler.java:733) 04-03 04:16:40.190: E/AndroidRuntime(1099): at android.os.Handler.dispatchMessage(Handler.java:95) 04-03 04:16:40.190: E/AndroidRuntime(1099): at android.os.Looper.loop(Looper.java:137) 04-03 04:16:40.190: E/AndroidRuntime(1099): at android.app.ActivityThread.main(ActivityThread.java:4998) 04-03 04:16:40.190: E/AndroidRuntime(1099): at java.lang.reflect.Method.invokeNative(Native Method) 04-03 04:16:40.190: E/AndroidRuntime(1099): at java.lang.reflect.Method.invoke(Method.java:515) 04-03 04:16:40.190: E/AndroidRuntime(1099): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777) 04-03 04:16:40.190: E/AndroidRuntime(1099): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593) 04-03 04:16:40.190: E/AndroidRuntime(1099): at dalvik.system.NativeStart.main(Native Method)

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doudeng9425 2015-04-03 09:35
    已采纳

    Same error occured in my case also, i fixed the error by adding AppControler class to manifest. but there may be other objects which are causing this error. I cant see Line 154.

    In AndroidManifest.xml

     <application
            android:name="xxx.xxx.xxx.xxx.AppController"
            ....
      >
    

    here xxx refers your package name and ... refers your existing code

    打赏 评论

相关推荐 更多相似问题