gpt4 book ai didi

java - 空对象引用上的“android.text.Editable android.widget.EditText.getText()”,代码顶部带有警报对话框

转载 作者:行者123 更新时间:2023-12-02 11:50:03 24 4
gpt4 key购买 nike

为什么我会收到此错误?请帮忙。预先感谢您!

final Button mShowDialog =(Button)findViewById(R.id.btnShowDialog);
mShowDialog.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder mbuilder = new AlertDialog.Builder(LoginActivity.this);
View mView = getLayoutInflater().inflate(R.layout.dialog_login,null);
final EditText hehe= findViewById(R.id.etUsername);
final EditText hehe1= findViewById(R.id.etPassword);
final Button bLogin = mView.findViewById(R.id.bSignIn);

bLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String Username = hehe.getText().toString();
final String Password = hehe1.getText().toString();

// Response received from the server
Response.Listener<String> responseListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jsonResponse = new JSONObject(response);
boolean success = jsonResponse.getBoolean("success");

if (success) {
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
LoginActivity.this.startActivity(intent);
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
builder.setMessage("Login Failed")
.setNegativeButton("Retry", null)
.create()
.show();
}

} catch (JSONException e) {
e.printStackTrace();
}
}
};

LoginRequest loginRequest = new LoginRequest(Username, Password, responseListener);
RequestQueue queue = Volley.newRequestQueue(LoginActivity.this);
queue.add(loginRequest);
}
});
mbuilder.setView(mView);
AlertDialog dialog = mbuilder.create();
dialog.show();

}
});

最佳答案

使用

final EditText hehe  =  mView.findViewById(R.id.etUsername);
// ^^^^^
final EditText hehe1 = mView.findViewById(R.id.etPassword);
final Button bLogin = mView.findViewById(R.id.bSignIn);

因为 findViewById 单独会尝试从 Activity 的当前布局中查找 View ,因此您需要从 mView 对象中查找 View ,该对象是从 dialog_login.xml 膨胀的

关于java - 空对象引用上的“android.text.Editable android.widget.EditText.getText()”,代码顶部带有警报对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47954048/

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