0
我試圖創建一個連接到本地主機上的數據庫的活動,在用戶填寫表單後,表單在後臺方法中使用asyntask.do提交給本地主機。我得到一個錯誤,我不太確定什麼是錯誤的,請我需要幫助。Android異步任務在後臺執行錯誤
這是我的logcat
`01-30 06:03:10.451: E/AndroidRuntime(1332): FATAL EXCEPTION: AsyncTask #1
01-30 06:03:10.451: E/AndroidRuntime(1332): java.lang.RuntimeException: An error occured while executing doInBackground()
01-30 06:03:10.451: E/AndroidRuntime(1332): at android.os.AsyncTask$3.done(AsyncTask.java:278)
01-30 06:03:10.451: E/AndroidRuntime(1332): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
01-30 06:03:10.451: E/AndroidRuntime(1332): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
01-30 06:03:10.451: E/AndroidRuntime(1332): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
01-30 06:03:10.451: E/AndroidRuntime(1332): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
01-30 06:03:10.451: E/AndroidRuntime(1332): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
01-30 06:03:10.451: E/AndroidRuntime(1332): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
01-30 06:03:10.451: E/AndroidRuntime(1332): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
01-30 06:03:10.451: E/AndroidRuntime(1332): at java.lang.Thread.run(Thread.java:856)
01-30 06:03:10.451: E/AndroidRuntime(1332): Caused by: java.lang.NullPointerException
01-30 06:03:10.451: E/AndroidRuntime(1332): at com.oose.buddiev2.RegisterActivity$CreateNewUser.doInBackground(RegisterActivity.java:101)
01-30 06:03:10.451: E/AndroidRuntime(1332): at com.oose.buddiev2.RegisterActivity$CreateNewUser.doInBackground(RegisterActivity.java:1)
01-30 06:03:10.451: E/AndroidRuntime(1332): at android.os.AsyncTask$2.call(AsyncTask.java:264)
01-30 06:03:10.451: E/AndroidRuntime(1332): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
01-30 06:03:10.451: E/AndroidRuntime(1332): ... 5 more`
這是在活動
class CreateNewUser extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(RegisterActivity.this);
pDialog.setMessage("Creating User..");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Creating User
* */
protected String doInBackground(String... args) {
String usernameString = usernameEditText.getText().toString();
String emailString = emailEditText.getText().toString();
String passwordString = passwordEditText.getText().toString();
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", usernameString));
params.add(new BasicNameValuePair("email", emailString));
params.add(new BasicNameValuePair("password", passwordString));
// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(url_create_user,
"POST", params);
// check logcat for response
Log.d("Create Response", json.toString());
// check for success tag
try {
int success = json.getInt(TAG_SUCCESS);
// int success = 1;
if (success == 1) {
// successfully created product
Intent i = new Intent(getApplicationContext(),
LoginActivity.class);
startActivity(i);
// closing this screen
finish();
} else {
// failed to create product
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
pDialog.dismiss();
}
什麼是你RegisterActivity.java線101? – nKn
您在RegisterActivity.java的第101行發生了Nullpointer異常。只要檢查它是什麼,你可以解決這個問題。 – Padma
這是第101行: String passwordString = passwordEditText.getText()。toString(); 我評論了它,並刪除了在後臺做的'startactivity',它啓動得很好,但我該如何傳遞密碼字符串來切斷。因爲它看起來像我在做錯了 – bubunyo