我尝试使用AsyncTask注册用户。运行应用程序时,它会创建一个用户并抛出这样的异常。
01-20 11:57:42.461: D/User Created!(2977): {"message":"Username Successfully Added!","success":1}
01-20 11:57:42.571: W/dalvikvm(2977): threadid=13: thread exiting with uncaught exception (group=0x40e19438)
01-20 11:57:42.591: E/AndroidRuntime(2977): FATAL EXCEPTION: AsyncTask #1
01-20 11:57:42.591: E/AndroidRuntime(2977): java.lang.RuntimeException: An error occured while executing doInBackground()
01-20 11:57:42.591: E/AndroidRuntime(2977): at android.os.AsyncTask$3.done(AsyncTask.java:299)
01-20 11:57:42.591: E/AndroidRuntime(2977): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
01-20 11:57:42.591: E/AndroidRuntime(2977): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
01-20 11:57:42.591: E/AndroidRuntime(2977): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
01-20 11:57:42.591: E/AndroidRuntime(2977): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
01-20 11:57:42.591: E/AndroidRuntime(2977): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
01-20 11:57:42.591: E/AndroidRuntime(2977): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
01-20 11:57:42.591: E/AndroidRuntime(2977): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
01-20 11:57:42.591: E/AndroidRuntime(2977): at java.lang.Thread.run(Thread.java:856)
01-20 11:57:42.591: E/AndroidRuntime(2977): Caused by: java.lang.NullPointerException
01-20 11:57:42.591: E/AndroidRuntime(2977): at com.example.rp.Register$CreateUser.doInBackground(Register.java:329)
01-20 11:57:42.591: E/AndroidRuntime(2977): at com.example.rp.Register$CreateUser.doInBackground(Register.java:1)
01-20 11:57:42.591: E/AndroidRuntime(2977): at android.os.AsyncTask$2.call(AsyncTask.java:287)
01-20 11:57:42.591: E/AndroidRuntime(2977): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
01-20 11:57:42.591: E/AndroidRuntime(2977): ... 5 more
这是我的活动asynctask
class CreateUser extends AsyncTask<String, String, String>{
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Register.this);
pDialog.setMessage("Creating User...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... arg0) {
int success;
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("name", name));
params.add(new BasicNameValuePair("email", email));
params.add(new BasicNameValuePair("password", password));
params.add(new BasicNameValuePair("dob", dob));
params.add(new BasicNameValuePair("gender", gender));
params.add(new BasicNameValuePair("mobile", mobile));
Log.d("request!", "starting");
JSONObject json = jsonParser.makeHttpRequest(url_reg, "POST", params);
Log.d("Login attempt", json.toString());
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Log.d("User Created!", json.toString());
Intent i = new Intent(Register.this, TestTab.class);
finish();
startActivity(i);
session.createLoginSession("RandB", email);
return json.getString(TAG_MESSAGE);
} else {
showAlertDialog(Register.this, "Registration Error...", "Please register again...", false);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
pDialog.dismiss();
}
}
用户创建。我想创建会话并开始新的活动。任何人都可以帮助我吗?
答案 0 :(得分:0)
在onPostExecute()方法
中执行此操作if (success == 1) {
Log.d("User Created!", json.toString());
Intent i = new Intent(Register.this, TestTab.class);
finish();
startActivity(i);
session.createLoginSession("RandB", email);
return json.getString(TAG_MESSAGE);
} else {
showAlertDialog(Register.this, "Registration Error...", "Please register again...", false);
}
答案 1 :(得分:0)
将任务更改为:
class CreateUser extends AsyncTask<String, String, String>{
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Register.this);
pDialog.setMessage("Creating User...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... arg0) {
int success;
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("name", name));
params.add(new BasicNameValuePair("email", email));
params.add(new BasicNameValuePair("password", password));
params.add(new BasicNameValuePair("dob", dob));
params.add(new BasicNameValuePair("gender", gender));
params.add(new BasicNameValuePair("mobile", mobile));
Log.d("request!", "starting");
JSONObject json = jsonParser.makeHttpRequest(url_reg, "POST", params);
Log.d("Login attempt", json.toString());
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
session.createLoginSession("RandB", email);
return json.getString(TAG_MESSAGE);
} else {
return null;
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
if(result!=null){
Intent i = new Intent(Register.this, TestTab.class);
startActivity(i);
session.createLoginSession("RandB", email);
finish();
}else{
showAlertDialog(Register.this, "Registration Error...", "Please register again...", false);
}
pDialog.dismiss();
}
}