gpt4 book ai didi

java - Android Studio getApplicationContext()

转载 作者:行者123 更新时间:2023-12-02 03:27:35 25 4
gpt4 key购买 nike

如果用户没有输入用户名和密码,我试图弹出消息,但是我的问题是 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();

}
}
});
}

}

最佳答案

尝试 LoginActivity.this 而不是 getApplicationContext()

关于java - Android Studio getApplicationContext(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38605534/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com