我有第二個查詢在線數據庫的活動,我想設置TextView的文本但我不能。當我啓動應用程序時,TextView是空的。textView中的文本
這是第二次活動的代碼:
public class sendQuery extends main {
/////////// Public method to send Query ///////////
public static String send(String query, Activity sendQuery) {
String result = "0";
InputStream is = null;
String weekDayVal=null;
String provola=null;
//the query to send
ArrayList<NameValuePair> querySend = new ArrayList<NameValuePair>();
querySend.add(new BasicNameValuePair("querySend",query));
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://locali.altervista.org/php/locali.php");
httppost.setEntity(new UrlEncodedFormEntity(querySend));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(
new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
try{
TextView text = (TextView) sendQuery.findViewById(R.id.textView10);
JSONArray weekDetails = new JSONArray (result); // Your response string
for(int index=0;index < 1/*weekDetails.length()*/;index++)
{
JSONObject tempWeekDetail = weekDetails.getJSONObject(index);
weekDayVal = tempWeekDetail.getString("Lunedi");// Value for Monday
//added this Log which you can view from LogCat. also changed above variable name
Log.i("Resp Value","Moday Value "+weekDayVal);
JSONObject provino = weekDetails.getJSONObject(index);
provola = provino.getString("Martedi");// Value for Monday
//added this Log which you can view from LogCat. also changed above variable name
Log.i("Resp Value","Moday Value "+provola);
text.setText(provola);
}
}
catch(Exception e)
{
}
}catch(Exception e){
Log.e("log_tag", "Error converting result: "+e.toString());
}
Log.i("SendQUERY", result);
return result;
}
}
的問題是在第二個活動,這裏
TextView text = (TextView) sendQuery.findViewById(R.id.textView10);
text.setText(provola);
氬訪問它在靜態send()方法你從查詢中得到什麼? –
試試這個:TextView text =(TextView)findViewById(R.id.textView10); –
如果我寫的話,不能從Activity類型中對非靜態方法findViewById(int)進行靜態引用。 – user3313188