我是App开发的新手,我正在尝试使用JSONParser
创建一个登录应用,但问题是每当我尝试login
时它都会崩溃。
06-30 15:06:32.415 16009-16052/com.zaid.sling.mysqltest E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
......
Caused by: java.lang.NullPointerException
代码如下
public class Login extends Activity implements OnClickListener{
private EditText user, pass;
private Button mSubmit, mRegister;
// Progress Dialog
private ProgressDialog pDialog;
// JSON parser class
JSONParser jsonParser = new JSONParser();
private static final String LOGIN_URL = "http://10.0.2.2:1234/webservice/login.php";
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
//setup input fields
user = (EditText)findViewById(R.id.username);
pass = (EditText)findViewById(R.id.password);
//setup buttons
mSubmit = (Button)findViewById(R.id.login);
mRegister = (Button)findViewById(R.id.register);
//register listeners
mSubmit.setOnClickListener(this);
mRegister.setOnClickListener(this);
}
@Override
public void onPause(){
super.onPause();
if(pDialog != null)
pDialog.dismiss();
}
@Override
protected void onStop() {
super.onStop();
if (pDialog != null) {
pDialog.dismiss();
pDialog = null;
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.login:
new AttemptLogin().execute("username", "password");
break;
case R.id.register:
Intent i = new Intent(this, Register.class);
startActivity(i);
break;
default:
break;
}
}
class AttemptLogin extends AsyncTask<String, String, String> {
boolean failure;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Login.this);
pDialog.setMessage("Attempting login...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... args) {
int success;
String username = args[0];
String password = args[1];
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("name", username));
params.add(new BasicNameValuePair("password", password));
Log.d("request!", "starting");
// getting product details by making HTTP request
JSONObject json = jsonParser.makeHttpRequest(
LOGIN_URL, params);
// check your log for json response
Log.d("Login attempt", json.toString());
// json success tag
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
failure = false;
Log.d("Login Successful!", json.toString());
}else{
failure = true;
Log.d("Login Failure!", json.getString(TAG_MESSAGE));
}
return json.getString(TAG_MESSAGE);
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
// dismiss the dialog once product deleted
pDialog.dismiss();
if (file_url != null){
Toast.makeText(Login.this, file_url, Toast.LENGTH_LONG).show();
}
if (failure == false) {
Intent i = new Intent(Login.this, ReadComment.class);
finish();
Login.this.startActivity(i);
}
}
}
}
用@Daniel更新我的代码后,我在logcat中获得了以下内容。
07-01 14:28:35.969 12758-12876/com.zaid.sling.mysqltest E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:299)
......
Caused by: java.lang.NullPointerException
at com.zaid.sling.mysqltest.Login$AttemptLogin.doInBackground(Login.java:143)
答案 0 :(得分:1)
这是错误
new AttemptLogin().execute();
这是错误的。您需要在此处传递用户名和密码。
new AttemptLogin().execute("user_name", "password");
您收到ArrayIndexOutOfBoundsException
错误。因为没有args传递给AsyncTask但是你试着像这样阅读
String username = args[0];
String password = args[1];
所以你需要将值传递给AsyncTask。
答案 1 :(得分:0)