0

我在第 80 行 (userFunction.logoutUser(getApplicationContext());) 收到错误,表示无法解析 userFunction。我已经定义了 userFunction 并且已经尝试使用完整的包名和东西,那么我该怎么做才能真正快速地摆脱这个错误呢?(问这个问题主要是为了将来参考,因为这种情况经常发生)谢谢!

package com.example.dashboardactivity;

import libary.UserFunctions;

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

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

public class LoginActivity extends Activity {
public final String TAG = "LoginActivity";
Button btnLogin;
Button btnLinkToRegister;
EditText inputEmail;
EditText inputPassword;
TextView loginErrorMsg;

// 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.login);

    // Importing all assets like buttons, text fields
    inputEmail = (EditText) findViewById(R.id.loginEmail);
    inputPassword = (EditText) findViewById(R.id.loginPassword);
    btnLogin = (Button) findViewById(R.id.btnLogin);
    btnLinkToRegister = (Button) findViewById(R.id.btnLinkToRegisterScreen);
    loginErrorMsg = (TextView) findViewById(R.id.login_error);

    // Login button Click Event
    Log.i(TAG, "LoginActivity Login button Click Event" );
    btnLogin.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            new AsyncTask<String, Void, JSONObject>(){
                @Override
                protected JSONObject doInBackground(String... args) { //This is run on a background thread
                    String email = args[0];
                    String password = args[1];
                    UserFunctions userFunction = new UserFunctions();
                    JSONObject json = userFunction.loginUser(email, password);
                    return json;
                }

                @Override
                protected void onPostExecute(JSONObject json) { //This is run on the UI thread
                    if(json != null){
                        //... update the user interface ...
                         Log.i(TAG, "checking for login response");
                         // check for login response
                         try {
                             if (json.getString(KEY_SUCCESS) != null) {
                                 loginErrorMsg.setText("");
                                 String res = json.getString(KEY_SUCCESS); 
                                 if(Integer.parseInt(res) == 1){
                                     // user successfully logged in
                                     // Store user details in SQLite Database
                                     libary.DatabaseHandler db = new libary.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 login
                                     loginErrorMsg.setText("Incorrect username/password");
                                 }
                             }
                         } catch (JSONException e) {
                             e.printStackTrace();
                         }
                     }


                    else{
                        Log.e("LoginTask", "No login response was received");
                    }
                    super.onPostExecute(json);
                }

            }.execute(inputEmail.getText().toString(), inputPassword.getText().toString());
        }
    });


    // Link to Register Screen
    Log.i(TAG, "LoginActivity btnLinkToRegister" );
    btnLinkToRegister.setOnClickListener(new View.OnClickListener() {

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

1 回答 1

0

您已在方法UserFunctions userFunction = new UserFunctions();内部定义,doInBackground但试图访问onPostExecute.

尝试在UserFunctions userFunction;外部声明,doInBackground以便其他函数可以访问它。

new AsyncTask<String, Void, JSONObject>(){

        UserFunctions userFunction;
        @Override
        protected JSONObject doInBackground(String... args) { //This is run on a background thread
            userFunction = new UserFunctions();
         }

        @Override
        protected void onPostExecute(JSONObject json) { 
            userFunction.logoutUser(getApplicationContext());
         }
}
于 2013-09-03T22:56:42.347 回答