Android Studio getApplicationcontext()

时间:2016-07-27 06:43:00

标签: java android android-studio

如果用户没有输入用户名和密码,我会尝试弹出消息,但我的问题是getApplicationcontext(),它说“无法解析方法”。我如何解决它? - - - - - - - - - - - - - - - - - - - - - - -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -----------------------------------------------

package com.example.rojean.prelim_project;

import android.support.v7.app.AppCompatActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends AppCompatActivity {

    Button loginBtn;
    EditText txtUsername, txtPassword;
    AccountsManagement session;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        Object getApplication;
        session = new AccountsManagement(getApplicationcontext());

        txtUsername = (EditText) findViewById(R.id.nameField);
        txtPassword = (EditText) findViewById(R.id.passwordField);

        Toast.makeText(getApplicationContext(),
                "User Login Status: " + session.isUserLoggedIn(),
                Toast.LENGTH_LONG).show();

        loginBtn = (Button)findViewById(R.id.loginBtn);
        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String username = txtUsername.getText().toString();
                String password = txtPassword.getText().toString();

                //Validation method
                if(username.trim().length() > 0 && password.trim().length() > 0){
                    // For testing puspose username, password is checked with static data
                    // username = admin
                    // password = admin

                    if(username.equals("admin") && password.equals("admin")){

                        // Creating user login session
                        // Statically storing name="Android Example"
                        // and email="androidexample84@gmail.com"
                        session.createUserLoginSession("Android Example",
                                "androidexample84@gmail.com");

                        // Starting MainActivity
                        Intent i = new Intent(getApplicationContext(), MainActivity.class);
                        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

                        // Add new Flag to start new Activity
                        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(i);

                        finish();
                    }else{

                        // username / password doesn't match&
                        Toast.makeText(getApplicationContext(),
                                "Username/Password is incorrect",
                                Toast.LENGTH_LONG).show();
                    }
                }else{

                    // user didn't entered username or password
                    Toast.makeText(getApplicationContext(),
                            "Please enter username and password",
                            Toast.LENGTH_LONG).show();

                }
            }
        });
    }

}

3 个答案:

答案 0 :(得分:2)

尝试使用LoginActivity.this代替getApplicationContext()

答案 1 :(得分:0)

使用this代替getApplicationContext()

Toast.makeText(this, "User Login Status: " + session.isUserLoggedIn(), Toast.LENGTH_LONG).show;

通过这种方式,您可以获得活动的上下文。

答案 2 :(得分:0)

请勿使用getApplicationContext()。获取活动上下文的最佳方法是使用LoginActivity.this,其中this指的是上下文本身。