我有一个活动,其中我有一个类扩展 AsyncTask 用于 HTTP GET 请求,然后解析我得到的数据。问题很简单——onclick 不起作用。
这是我的代码的一部分:
public class GetMethod extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... urls){
BufferedReader in = null;
for (String url : urls) {
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
try {
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String l = "";
String nl = System.getProperty("line.separator");
while ((l = in.readLine()) != null) {
sb.append(l + nl);
}
in.close();
data = sb.toString();
runOnUiThread(new Runnable() {
public void run() {
JSONObject jObject;
try {
jObject = new JSONObject(data);
//JSONObject object = jObject.getJSONObject("pharms");
JSONArray jArray = jObject.getJSONArray("pharms");
for (int i = 0; i < jArray.length(); i++) {
name = jArray.getJSONObject(i).getString("name").toString();
phone = jArray.getJSONObject(i).getString("phone").toString();
latitude = jArray.getJSONObject(i).getString("latitude").toString();
longitude = jArray.getJSONObject(i).getString("longitude").toString();
address = jArray.getJSONObject(i).getString("address").toString();
RelativeLayout ll2 = new RelativeLayout(context);
//ll2.setOrientation(Orientation.ho)
*** Create button named call ***
call.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:123456789"));
startActivity(callIntent); //// This function doesn't work
}
});
ll2.addView(call);
*** Create textview named tv ***
ll2.addView(tv);
ll.addView(ll2);
}
}