每次按下登录按钮调用AsyncTask时,应用程序都会崩溃。我无法弄清楚我是否错误地传递了字符串或是什么。任何想法将不胜感激。另外,如果我没有提供足够的信息,请告诉我。对不起,这是我的第一篇文章。谢谢
package com.carrcodes.getgathering;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import org.json.JSONException;
import org.json.JSONObject;
public class LoginActivity extends Activity {
EditText usernameField,passwordField;
Button login,register;
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
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
setTitle("Login");
usernameField = (EditText) findViewById(R.id.StudentEmailBox);
passwordField = (EditText) findViewById(R.id.PasswordBox);
login = (Button) findViewById(R.id.sign_in_button);
register = (Button) findViewById(R.id.register_button);
loginErrorMsg = (TextView) findViewById(R.id.login_error);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
/* Intent intent = new Intent(LoginActivity.this, GetGatheringActivity.class);
Bundle login_info = new Bundle();
String username = usernameField.getText().toString();
String password = passwordField.getText().toString();
login_info.putString("Username", username);
login_info.putString("Password", password);
intent.putExtras(login_info);
UserFunctions userFunction = new UserFunctions();
JSONObject json = userFunction.loginUser(username, password); */
Async task = new Async();
task.execute(KEY_SUCCESS);
}});
/*
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
// 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
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
JSONObject json_user = json.getJSONObject("user");
// Clear all previous data in database
userFunction.logoutUser(getApplicationContext());
db.addUser(json_user.getString(KEY_EMAIL)
, json.getString(KEY_UID), json_user.getString(KEY_CREATED_AT));
// Launch Dashboard Screen
Intent dashboard = new Intent(getApplicationContext(),
GetGatheringActivity.class);
// Close all views before launching Dashboard
dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(dashboard);
// Close Login Screen
finish();
} else {
// Error in login
loginErrorMsg.setText("Incorrect username/password");
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});*/
register.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent registerIntent = new Intent(getApplicationContext(),
RegisterActivity.class);
startActivity(registerIntent);
finish();
}
});
}
private class Async extends AsyncTask<String, Void, Void>{
private Context context;
@Override
protected Void doInBackground(String... sUrl) {
Intent intent = new Intent (LoginActivity.this, GetGatheringActivity.class);
Bundle login_info = new Bundle();
String username = usernameField.getText().toString();
String password = passwordField.getText().toString();
login_info.putString("Username", username);
login_info.putString("Password", password);
intent.putExtras(login_info);
UserFunctions userFunction = new UserFunctions();
JSONObject json = userFunction.loginUser(username, password);
// 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
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
JSONObject json_user = json.getJSONObject("user");
// Clear all previous data in database
userFunction.logoutUser(getApplicationContext());
db.addUser(json_user.getString(KEY_EMAIL)
, json.getString(KEY_UID), json_user.getString(KEY_CREATED_AT));
// Launch Dashboard Screen
Intent dashboard = new Intent(getApplicationContext(),
GetGatheringActivity.class);
// Close all views before launching Dashboard
dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(dashboard);
// Close Login Screen
finish();
}else{
// Error in login
loginErrorMsg.setText("Incorrect username/password");
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
这是我的Logcat
01-15 21:31:25.642 1777-1777/com.carrcodes.getgathering D/InputEventConsistencyVerifier﹕ KeyEvent: ACTION_UP but key was not down.
in android.widget.EditText{3cd680a VFED..CL .F...... 64,221-1376,378 #7f090004 app:id/PasswordBox}
0: sent at 2748595000000, KeyEvent { action=ACTION_UP, keyCode=KEYCODE_TAB, scanCode=15, metaState=0, flags=0x8, repeatCount=0, eventTime=2748595, downTime=2748451, deviceId=0, source=0x101 }
01-15 21:31:28.647 1777-1798/com.carrcodes.getgathering E/JSON﹕ [ 01-15 21:31:28.655 1777: 1798 E/JSON Parser ]
Error parsing data org.json.JSONException: End of input at character 0 of
01-15 21:31:28.665 1777-1798/com.carrcodes.getgathering E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
Process: com.carrcodes.getgathering, PID: 1777
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String org.json.JSONObject.getString(java.lang.String)' on a null object reference
at com.carrcodes.getgathering.LoginActivity$Async.doInBackground(LoginActivity.java:151)
at com.carrcodes.getgathering.LoginActivity$Async.doInBackground(LoginActivity.java:130)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
答案 0 :(得分:0)
JSONObject json = userFunction.loginUser(username, password);
我怀疑json
将返回null,因为在此之后您在其上调用了几个getString()
方法。你可以将它打印到日志或使用调试器一步一步吗?
这是引起问题的线路,我认为(没有行号,很难确定):
if (json.getString(KEY_SUCCESS) != null) {
答案 1 :(得分:0)
我认为问题是空指针异常。 问题发生在 JSONObject json = userFunction.loginUser(用户名,密码);
在调用任何方法之前进行if(json!= null)之类的检查。