Java 在我的AsyncTask中,哪个变量读取为null?
每次我点击登录按钮调用AsyncTask时,应用程序都会崩溃。我不知道我是否传递了错误的字符串或者什么。任何想法都将不胜感激。如果我没有提供足够的信息,请告诉我。对不起,这是我的第一篇文章。多谢各位Java 在我的AsyncTask中,哪个变量读取为null?,java,android,json,android-asynctask,Java,Android,Json,Android Asynctask,每次我点击登录按钮调用AsyncTask时,应用程序都会崩溃。我不知道我是否传递了错误的字符串或者什么。任何想法都将不胜感激。如果我没有提供足够的信息,请告诉我。对不起,这是我的第一篇文章。多谢各位 package com.carrcodes.getgathering; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.os.Asy
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;
}
}
我怀疑json返回为null,因为在这一点之后,您对它调用了几个getString方法。您可以将其打印到日志中,还是使用调试器逐步执行
这是导致问题的线路,我认为没有线路号,很难确定:
if (json.getString(KEY_SUCCESS) != null) {
我认为问题在于空指针异常。 问题发生在 JSONObject json=userFunction.loginuserName,密码
像ifjson一样进行检查!=在调用任何方法之前为空。那么,哪一行是LoginActivity.java行151?json_用户必须为空。从外观上看,这是您调用getString的唯一对象如果json.getStringKEY_成功!=无效的{@Carr then json为null.Hm。好的,谢谢。我会花一些时间回顾一下。调试器真正告诉我的就是我发布的logcat。有什么方法让我更深入地了解吗?是的,你发布的行是正确的@ToddI认为这一部分:输入结尾处的字符0表示你的json格式不好。如果我是你,我会深入研究userFunction是什么,看看你是否可以调试它或者修改它来记录更多关于它接收到的信息。祝你好运。
JSONObject json = userFunction.loginUser(username, password);
if (json.getString(KEY_SUCCESS) != null) {