0

当单击注册或登录按钮时,我已经获得了 android 应用程序的登录和注册屏幕以及与之相关的活动。保存时我没有收到任何错误。但是当我运行它时,屏幕会在模拟器中加载,允许我输入数据,但是一旦我单击注册按钮或登录按钮,我就会收到错误“应用程序意外停止,请重试”。

以下是我在 LogCat 中遇到的错误:

03-01 13:01:49.649: W/dalvikvm(583): threadid=1: thread exiting with uncaught exception (group=0x40015560)
03-01 13:01:49.669: E/AndroidRuntime(583): FATAL EXCEPTION: main
03-01 13:01:49.669: E/AndroidRuntime(583): java.lang.NullPointerException
03-01 13:01:49.669: E/AndroidRuntime(583): at com.example.attendance2.RegisterActivity$1.onClick(RegisterActivity.java:59)
03-01 13:01:49.669: E/AndroidRuntime(583):     at android.view.View.performClick(View.java:2485)
03-01 13:01:49.669: E/AndroidRuntime(583):     at android.view.View$PerformClick.run(View.java:9080)
03-01 13:01:49.669: E/AndroidRuntime(583):     at android.os.Handler.handleCallback(Handler.java:587)
03-01 13:01:49.669: E/AndroidRuntime(583):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-01 13:01:49.669: E/AndroidRuntime(583):     at android.os.Looper.loop(Looper.java:123)
03-01 13:01:49.669: E/AndroidRuntime(583):     at android.app.ActivityThread.main(ActivityThread.java:3683)
03-01 13:01:49.669: E/AndroidRuntime(583):     at java.lang.reflect.Method.invokeNative(Native Method)
03-01 13:01:49.669: E/AndroidRuntime(583):     at java.lang.reflect.Method.invoke(Method.java:507)
03-01 13:01:49.669: E/AndroidRuntime(583):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-01 13:01:49.669: E/AndroidRuntime(583):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-01 13:01:49.669: E/AndroidRuntime(583):     at dalvik.system.NativeStart.main(Native Method)
03-01 13:01:56.080: I/Process(583): Sending signal. PID: 583 SIG: 9

我已经检查了 RegisterActivity.java,看起来一切都很好......我还添加了 RegisterActivity.java 和 UserFunctions.java 如果您发现问题所在以及是否需要更多编码,请告诉我!当我尝试 LoginActivity.java 登录时,也会发生同样的错误。

注册活动.java

package com.example.attendance2;

import org.json.JSONException;
import org.json.JSONObject;

import library.DatabaseHandler;
import library.UserFunctions;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class RegisterActivity extends Activity {
Button btnRegister;
Button btnLinkToLogin;
EditText inputFullName;
EditText inputEmail;
EditText inputPassword;
TextView registerErrorMsg;

// JSON Response node names
private static String KEY_SUCCESS = "success";
private static String KEY_ERROR = "error";
private static String KEY_ERROR_MSG = "error_msg";
private static String KEY_UID = "uid";
private static String KEY_NAME = "name";
private static String KEY_EMAIL = "email";
private static String KEY_CREATED_AT = "created_at";

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

    // Importing all assets like buttons, text fields
    inputFullName = (EditText) findViewById(R.id.registerName);
    inputEmail = (EditText) findViewById(R.id.registerEmail);
    inputPassword = (EditText) findViewById(R.id.registerPassword);
    btnRegister = (Button) findViewById(R.id.btnRegister);
    btnLinkToLogin = (Button) findViewById(R.id.btnLinkToLoginScreen);
    registerErrorMsg = (TextView) findViewById(R.id.register_error);

    // Register Button Click event
    btnRegister.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            String name = inputFullName.getText().toString();
            String email = inputEmail.getText().toString();
            String password = inputPassword.getText().toString();
            UserFunctions userFunction = new UserFunctions();
            JSONObject json = userFunction.registerUser(name, email, password);

            // check for login response
            try {
                if (json.getString(KEY_SUCCESS) != null) {
                    registerErrorMsg.setText("");
                    String res = json.getString(KEY_SUCCESS);
                    if(Integer.parseInt(res) == 1){
                        // user successfully registered
                        // Store user details in SQLite Database
                        DatabaseHandler db = new DatabaseHandler(getApplicationContext());
                        JSONObject json_user = json.getJSONObject("user");

                        // Clear all previous data in database
                        userFunction.logoutUser(getApplicationContext());
                        db.addUser(json_user.getString(KEY_NAME), json_user.getString(KEY_EMAIL), json.getString(KEY_UID), json_user.getString(KEY_CREATED_AT));
                        // Launch Dashboard Screen
                        Intent dashboard = new Intent(getApplicationContext(),   DashboardActivity.class);
                        // Close all views before launching Dashboard
                        dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(dashboard);
                        // Close Registration Screen
                        finish();
                    }else{
                        // Error in registration
                        registerErrorMsg.setText("Error occured in registration");
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    });

    // Link to Login Screen
    btnLinkToLogin.setOnClickListener(new View.OnClickListener() {

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

用户函数.java

package library;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.jsn.JSONObject;

import android.content.Context;

public class UserFunctions {

private JSONParser jsonParser;

// Testing in localhost using wamp or xampp
// use http://10.0.2.2/ to connect to your localhost ie http://localhost/
private static String loginURL = "http://10.0.2.2:82/android_api/";
private static String registerURL = "http://10.0.2.2:82/android_api/";

private static String login_tag = "login";
private static String register_tag = "register";

// constructor
public UserFunctions(){
    jsonParser = new JSONParser();
}

/**
 * function make Login Request
 * @param email
 * @param password
 * */
public JSONObject loginUser(String email, String password){
    // Building Parameters
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("tag", login_tag));
    params.add(new BasicNameValuePair("email", email));
    params.add(new BasicNameValuePair("password", password));
    JSONObject json = jsonParser.getJSONFromUrl(loginURL, params);
    // return json
    // Log.e("JSON", json.toString());
    return json;
}

/**
 * function make Login Request
 * @param name
 * @param email
 * @param password
 * */
public JSONObject registerUser(String name, String email, String password){
    // Building Parameters
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("tag", register_tag));
    params.add(new BasicNameValuePair("name", name));
    params.add(new BasicNameValuePair("email", email));
    params.add(new BasicNameValuePair("password", password));

    // getting JSON Object
    JSONObject json = jsonParser.getJSONFromUrl(registerURL, params);
    // return json
    return json;
}

/**
 * Function get Login status
 * */
public boolean isUserLoggedIn(Context context){
    DatabaseHandler db = new DatabaseHandler(context);
    int count = db.getRowCount();
    if(count > 0){
        // user logged in
        return true;
    }
    return false;
}

/**
 * Function to logout user
 * Reset Database
 * */
public boolean logoutUser(Context context){
    DatabaseHandler db = new DatabaseHandler(context);
    db.resetTables();
    return true;
}

}
4

1 回答 1

0

在我看来,就像 registerErrorMsg 为 null - 例如,在布局中找不到您的文本视图。您使用的 id (R.id.register_error) 有可能在另一个元素中声明。

于 2013-03-01T18:26:29.963 回答