我已經將OnclickListener設置爲ImageView,當點擊執行asyncTask時,它在運行於Api 5.0下的設備上運行良好,但在Android Api 5.0設備中完全不工作。AndroidView上的ImageView onClickListener無法正常工作
異步任務:
public class SendName extends AsyncTask<String, String, String> {
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... strings) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://example.com/web.php");
String user = "John";
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("user", user));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost);
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String lengthOfFile) {
}
}
ImageView的XML:
<ImageView
android:id="@+id/send"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/send" />
OnClickListener:
ImageView send_img = (ImageView) findViewById(R.id.send);
send_img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new sendName().execute()
}
});
請發表您的代碼。 –
@Mayuri code added –
它不應該是'new SendName()。execute()'? – aandis