我正在开发一个应用程序,因为我必须将数据发送到服务器。我得到异常,同时获取用户详细信息nullpointer异常请告诉我我在哪里犯了错误
myActivity代码
userDetails = new DetailsDTO();
userName = userNameEdit.getText().toString().trim();
password = passwordEdit.getText().toString().trim();
confirmPwd = confirmPasswordEdit.getText().toString();
mobileNo = mobileNoEdit.getText().toString();
userDetails.setUserName(userName);
userDetails.setPassword(password);
userDetails.setConfirmPwd(confirmPwd);
userDetails.setMobileNumber(mobileNo);
(new UserDetailsAsync(mContext)).execute();
我的AsyncTask代码:
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
httpClient = new DefaultHttpClient();
httpPost = new HttpPost(TrafficConstants.USER_REG_URL);
Log.e("userName", details.getUserName());
Log.e("pass", details.getPassword());
Log.e("MOb",details.getMobileNumber());
try{
arrayList = new ArrayList<NameValuePair>(3);
arrayList.add(new
BasicNameValuePair("usr",details.getUserName()));
arrayList.add(new
BasicNameValuePair("pass",details.getPassword()));
arrayList.add(new
BasicNameValuePair("mob",details.getMobileNumber()));
httpPost.setEntity(new UrlEncodedFormEntity(arrayList));
HttpResponse response = httpClient.execute(httpPost);
}
catch(Exception e){
e.printStackTrace();
}
return null;
}
LogCat错误
02-20 12:16:37.535: E/AndroidRuntime(2605): java.lang.RuntimeException: An error occured while executing doInBackground()
02-20 12:16:37.535: E/AndroidRuntime(2605): at android.os.AsyncTask$3.done(AsyncTask.java:299)
02-20 12:16:37.535: E/AndroidRuntime(2605): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
02-20 12:16:37.535: E/AndroidRuntime(2605): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
02-20 12:16:37.535: E/AndroidRuntime(2605): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
02-20 12:16:37.535: E/AndroidRuntime(2605): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
02-20 12:16:37.535: E/AndroidRuntime(2605): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
02-20 12:16:37.535: E/AndroidRuntime(2605): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
02-20 12:16:37.535: E/AndroidRuntime(2605): at java.lang.Thread.run(Thread.java:856)
02-20 12:16:37.535: E/AndroidRuntime(2605): Caused by: java.lang.NullPointerException: println needs a message
02-20 12:16:37.535: E/AndroidRuntime(2605): at android.util.Log.println_native(Native Method)
02-20 12:16:37.535: E/AndroidRuntime(2605): at android.util.Log.e(Log.java:231)
02-20 12:16:37.535: E/AndroidRuntime(2605): at com.technowellServices.locationfind.UserRegistratrionActivity$UserDetailsAsync.doInBackground(UserRegistratrionActivity.java:150)
02-20 12:16:37.535: E/AndroidRuntime(2605): at com.technowellServices.locationfind.UserRegistratrionActivity$UserDetailsAsync.doInBackground(UserRegistratrionActivity.java:1)
02-20 12:16:37.535: E/AndroidRuntime(2605): at android.os.AsyncTask$2.call(AsyncTask.java:287)
答案 0 :(得分:5)
在您的类中声明AsyncTask时,请将其声明为:
class TestAsyncTask extends AsyncTask<Context, Void, Void>{
@Override
protected Void doInBackground(Context... params) {
// TODO Auto-generated method stub
Context context = params[0];
return null;
}
}
接受您想要传递给您创建的Asynctask的参数的第一个参数。
另外,请将其命名为:
new TestAsyncTask().execute(MyActivity.this);
答案 1 :(得分:3)
最好尝试将您的Object传递给AsyncTask doInBackground:
@Override
protected Void doInBackground(DetailsDTO... params) {
// TODO Auto-generated method stub
httpClient = new DefaultHttpClient();
httpPost = new HttpPost(TrafficConstants.USER_REG_URL);
DetailsDTO userDetails = params[0];
Log.e("userName", userDetails.getUserName());
Log.e("pass", userDetails.getPassword());
Log.e("MOb",userDetails.getMobileNumber());
try{
arrayList = new ArrayList<NameValuePair>(3);
arrayList.add(new
BasicNameValuePair("usr",userDetails.getUserName()));
arrayList.add(new
BasicNameValuePair("pass",userDetails.getPassword()));
arrayList.add(new
BasicNameValuePair("mob",userDetails.getMobileNumber()));
httpPost.setEntity(new UrlEncodedFormEntity(arrayList));
HttpResponse response = httpClient.execute(httpPost);
}
catch(Exception e){
e.printStackTrace();
}
return null;
}
然后在你的Activity
课程中:
new UserDetailsAsync().execute(userDetails);
如果这样做无效,请准确撰写您的活动类150 line
,这会使您的应用与NPE崩溃。
答案 2 :(得分:0)
试试这个..
new UserDetailsAsync(userName,password,confirmPwd,mobileNo,mContext)).execute();
在 AsyncTask 添加构造函数
中 String userName,password,confirmPwd,mobileNo;
Context context;
// constructor
public UserDetailsAsync(String userName,String password,String confirmPwd,String mobileNo, Context context) {
this.userName = userName;
this.password = password;
this.confirmPwd = confirmPwd;
this.mobileNo = mobileNo;
this.context = context;
}
或者
在您的活动中使用 userDetails 作为public static userDetails
并在 AsyncTask
中 Log.e("userName", YourActivity.userDetails.getUserName());
Log.e("pass", YourActivity.userDetails.getPassword());
Log.e("MOb", YourActivity.userDetails.getMobileNumber());
答案 3 :(得分:0)
尝试在AsyncTask中创建userDetails = new DetailsDTO();
的实例,并将details.getUserName()
更改为userDetails .getUserName()
答案 4 :(得分:0)
我认为您的doinbackground
方法返回null。因此它可能会导致onpostexecute