登录页面Android与排球库

时间:2022-10-22 20:32:26

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

我试图使用PHP和MySQL建立一个Android登录页面。我遇到了这个Google'Volley.jar'库,我正在尝试实现它,但是我的LoginActivity在获取详细信息并使用AppController发出请求时出错。我已经附加了LoginACtivity和AppController代码。登录活动

@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)

这是我的logcat:04-03 04:16:40.190:E / AndroidRuntime(1099):致命异常:主04-03 04:16:40.190:E / AndroidRuntime(1099):进程: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):在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):在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 个解决方案

#1


0  

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.

在我的情况下也发生了同样的错误,我通过将AppControler类添加到清单来修复错误。但可能有其他对象导致此错误。我看不到154号线。

In AndroidManifest.xml

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

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

这里xxx是指你的包名,而...是指你现有的代码

#1


0  

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.

在我的情况下也发生了同样的错误,我通过将AppControler类添加到清单来修复错误。但可能有其他对象导致此错误。我看不到154号线。

In AndroidManifest.xml

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

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

这里xxx是指你的包名,而...是指你现有的代码