我正在尝试创建一个与Web服务器api连接的登录应用程序。当我在android studio中构建文件时,一切都很顺利。但是在模拟器/真实Android设备中运行应用程序后,当我单击登录按钮时,应用程序已停止。
MainActivity在这里:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
TextInputLayout username, password;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView myTextView = (TextView) findViewById(R.id.textView5);
Typeface typeface=Typeface.createFromAsset(getAssets(), "fonts/solaimanlipi.ttf");
myTextView.setTypeface(typeface);
initializtion();
}
private void initializtion() {
final TextInputLayout username = (TextInputLayout) findViewById(R.id.username);
final TextInputLayout password = (TextInputLayout) findViewById(R.id.password);
username.setHint("Username");
password.setHint("Password");
button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
}
@Override
public void onClick(View view) {
int id = view.getId();
if(id == R.id.button){
if (!CommonTask.isOnline(this)) {
CommonTask.goSettingPage(this);
return;
}
validation_check();
}
}
private void validation_check() {
if(username.getEditText().getText().toString().trim().equals("")){
username.setError("Please enter user name");
return;
}
if(password.getEditText().getText().toString().trim().equals("")){
password.setError("Please enter your password");
return;
}
doLogin();
}
private void doLogin() {
try{
String url=String.format(CommonURL.getInstance().login,username.getEditText().getText().toString().trim(),
password.getEditText().getText().toString().trim());
final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Login , Please wait");
progressDialog.setCancelable(false);
progressDialog.show();
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, "",
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject obj) {
progressDialog.dismiss();
if(obj != null){
LoginResponse response = new Gson().fromJson(obj.toString(), LoginResponse.class);
if(response.result.equals("valid")){
CommonTask.savePreferences(getApplicationContext(), CommonContents.USERNAME,username.getEditText().getText().toString().trim());
CommonTask.savePreferences(getApplicationContext(), CommonContents.PASSWORD,password.getEditText().getText().toString().trim());
CommonTask.savePreferences(getApplicationContext(), CommonContents.isLoggedIn,"1");
// Toast.makeText(getApplicationContext(), "Login Succesfull", Toast.LENGTH_SHORT).show();
Intent intent=new Intent(getApplicationContext(),HomePage.class);
startActivity(intent);
finish();
}else{
Toast.makeText(getApplicationContext(), "Wrong UserID or Password.", Toast.LENGTH_SHORT).show();
}
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
progressDialog.dismiss();
Toast.makeText(getApplicationContext(), "Unexpected error.", Toast.LENGTH_SHORT).show();
}
});
request.setRetryPolicy(new DefaultRetryPolicy(30000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
MainApp.getInstance().addToRequestQueue(request, "Login");
}catch (Exception ex){
CommonTask.showLog(ex.getMessage());
}
}
}
但是当我点击按钮时,Android Studio会给我这个logcat:
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at com.mateors.mastererp.activity.MainActivity.validation_check(MainActivity.java:68)
at com.mateors.mastererp.activity.MainActivity.onClick(MainActivity.java:63)
at android.view.View.performClick(View.java:4084)
at android.view.View$PerformClick.run(View.java:16966)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
在initialization
功能中,您不必再次声明username
和password
字段。这里应该使用全局变量。您在代码中声明了两个局部变量,这些变量是错误的,并且无法在此范围之外找到。
这是修改后的initialization
功能。
private void initializtion() {
username = (TextInputLayout) findViewById(R.id.username);
password = (TextInputLayout) findViewById(R.id.password);
username.setHint("Username");
password.setHint("Password");
button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
}
现在在validation_check
函数中,您需要从这些EditText
获取文本。
private void validation_check() {
if(username.getText().toString().trim().equals("")){
username.setError("Please enter user name");
return;
}
if(password.getText().toString().trim().equals("")){
password.setError("Please enter your password");
return;
}
doLogin();
}
这样做。