简体   繁体   中英

Quick Unresolved Class Issue

I get an error on line 80 (userFunction.logoutUser(getApplicationContext());) That says userFunction cannot be resolved. I have defined userFunction and have already tried to use the full package name and things, so what can I do to get rid of this error really quick? (Asked this question mostly for future reference since this happens a lot) Thanks!

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();
        }
    });
}
}

You've defined UserFunctions userFunction = new UserFunctions(); inside the doInBackground method but are trying to access the variable in onPostExecute .

Try to declare UserFunctions userFunction; outside the doInBackground so that it is accessible to other functions.

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());
         }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM