textView中的文本

时间:2014-03-13 11:01:24

标签: android

我有第二个查询在线数据库的活动,我想设置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);

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

根本问题是您尝试通过静态方法访问类实例成员。静态方法可以在任何类实例上调用,甚至可以在任何实例存在之前调用。因此,要么通过删除static关键字将send()方法更改为非静态方法,请执行以下操作。

更改send方法以接受容易混淆的第二个参数而不是Activty类型的sendQuery类类型:

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

使用

将您的文本实例定义为sendQuery活动类的公共成员
public TextView text = (TextView) findViewById(R.id.textView10);

然后使用

在静态send()方法中访问它
sendQuery.text.setText(provola);