我正在尝试在Async
执行后更新主线程 UI。我试图通过更新它onPostExecute
并获取NullPointerException
.
这是我的课
public class UncheckGuestArrayAdapter extends ArrayAdapter<UncheckGuestObj> {
private final Activity thisContext;
private ArrayList<UncheckGuestObj> guest = new ArrayList<UncheckGuestObj>();
// String event_id;
private String guest_id;
// TODO to ask about the checkin
static class ViewHolder {
public TextView guestName;
public TextView guestEmail;
public Button btnCheckIn;
}
public ViewHolder holder;
private ViewHolder getHolder(View v) {
ViewHolder holder = new ViewHolder();
holder.guestName = (TextView) v.findViewById(R.id.tx_guest_name);
holder.guestEmail = (TextView) v.findViewById(R.id.tx_guest_email);
holder.btnCheckIn = (Button) v.findViewById(R.id.btnCheckIn);
return holder;
}
public UncheckGuestArrayAdapter(Activity context,
ArrayList<UncheckGuestObj> objects) {
super(context, R.layout.row_guest_list, objects);
this.thisContext = context;
this.guest = objects;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
View vi = convertView;
if (vi == null || vi.getTag() == null) {
LayoutInflater inflater = (LayoutInflater) thisContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
vi = inflater.inflate(R.layout.row_guest_list, null);
holder = getHolder(vi);
vi.setTag(holder);
} else {
holder = (ViewHolder) vi.getTag();
}
holder.guestName.setText(guest.get(position).guestName);
holder.guestEmail.setText(guest.get(position).guestEmail);
holder.btnCheckIn.setText("Check In");
// holder.btnCheckIn.setBackgroundColor(Color.GREEN);
holder.btnCheckIn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.d("you clicked", "the item at [" + position + "] position");
guest_id = guest.get(position).guestId;
new CheckInAsync().execute();
}
});
return vi;
}
// Check In Async Task for executing the POST Request
public class CheckInAsync extends AsyncTask<Void, Void, Void> {
ProgressDialog mProgressDialog;
@Override
protected Void doInBackground(Void... params) {
try {
Thread.sleep(3000);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
try {
URI uri = new URI(Constants.BASE_URL + "/events/"
+ GuestListActivity.event_id + "/guests/" + guest_id);
Log.e(getClass().getSimpleName(), uri.toString());
HttpPost httppost = new HttpPost(uri);
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
2);
// nameValuePairs.add(new BasicNameValuePair("sk", SharePref
// .getInstance(GetEventList.this).getSessionKey()));
Context mContext = null;
nameValuePairs.add(new BasicNameValuePair("sk", SharePref
.getInstance(mContext).getSessionKey()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
int response_code = response.getStatusLine().getStatusCode();
// HttpEntity entity = response.getEntity();
Log.i("response code", "" + response_code);
if (response_code == 201) {
Log.e("checkin status", "Checkin done");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void result) {
mProgressDialog.dismiss();
holder.btnCheckIn.setText(R.string.heavy_check_mark);
holder.btnCheckIn.setBackgroundColor(Color.MAGENTA);
Toast.makeText(getContext(), "Checked In!", Toast.LENGTH_SHORT)
.show();
}
}
}
例外是
08-23 18:56:16.192: E/AndroidRuntime(11077): FATAL EXCEPTION: main
08-23 18:56:16.192: E/AndroidRuntime(11077): java.lang.NullPointerException
08-23 18:56:16.192: E/AndroidRuntime(11077): at com.example.example.adaptor.UncheckGuestArrayAdapter$CheckInAsync.onPostExecute(UncheckGuestArrayAdapter.java:166)
08-23 18:56:16.192: E/AndroidRuntime(11077): at com.example.example.UncheckGuestArrayAdapter$CheckInAsync.onPostExecute(UncheckGuestArrayAdapter.java:1)
08-23 18:56:16.192: E/AndroidRuntime(11077): at android.os.AsyncTask.finish(AsyncTask.java:631)
08-23 18:56:16.192: E/AndroidRuntime(11077): at android.os.AsyncTask.access$600(AsyncTask.java:177)
行号 166 是线路输入onPostExecute
holder.btnCheckIn.setText(R.string.heavy_check_mark);
holder.btnCheckIn.setBackgroundColor(Color.MAGENTA);
我做错了什么?