2014-03-13 78 views
-2

我有第二個查詢在線數據庫的活動,我想設置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); 
+0

氬訪問它在靜態send()方法你從查詢中得到什麼? –

+0

試試這個:TextView text =(TextView)findViewById(R.id.textView10); –

+0

如果我寫的話,不能從Activity類型中對非靜態方法findViewById(int)進行靜態引用。 – user3313188

回答

0
String s = "test"; 
TextView text = (TextView) findViewById(R.id.textView10); 
text.setText(s); 
+0

不能從類型Activity中對非靜態方法findViewById(int)進行靜態引用,爲什麼? – user3313188

+0

刪除靜態,因爲你不能在主要活動中使用非靜態 – 2014-03-14 06:33:31

+0

中的靜態方法,那麼我有這個錯誤:無法對類型爲sendQuery的非靜態方法send(String)進行靜態引用 – user3313188

0

根本的問題是,你要訪問類實例成員通過靜態方法。靜態方法可以在任何類實例上調用,甚至可以在任何實例存在之前調用。因此,通過刪除靜態關鍵字將send()方法更改爲非靜態,或執行以下操作。

更改發送方法,以接受混淆的命名第二個參數,而不是一個Activty類型sendQuery類類型:

public static String send(String query, sendQuery sendQuery) { 

定義文本實例作爲sendQuery活動類的公共成員與

public TextView text = (TextView) findViewById(R.id.textView10); 

然後用

sendQuery.text.setText(provola); 
+0

有關一些有用的信息靜態與實例,請查看http://programmers.stackexchange.com/questions/211137/why-can-static-methods-only-use-static-data上的長答案。 – scottt

+0

我在公共TextView text =(TextView)上發生錯誤findViewById(R.id.textView10):此行有多個標記 \t - 無法對類型爲 的非靜態方法findViewById(int)進行靜態引用\t活動 \t - 變量文本的非法修飾符;只有最後一個被允許 – user3313188

+0

該行應該在靜態send()方法之外。換句話說,正確的'公共類sendQuery擴展main {'行。 – scottt