Reputation: 21
i have this error
Cannot make a static reference to the non-static method findViewById(int) from the type Activity" in a second activity
Code of second activity:
public class sendQuery extends main {
/////////// Public method to send Query ///////////
public static String send(String query,Activity main) {
String result = "0";
InputStream is = null;
String weekDayVal=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{
JSONArray weekDetails = new JSONArray ( result); // Your response string
for(int index=0;index < 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);
}
}catch(Exception e)
{
TextView text = (TextView) main.findViewById(R.id.textView10);
text.setText(weekDayVal);
}
}catch(Exception e){
Log.e("log_tag", "Error converting result: "+e.toString());
}
Log.i("SendQUERY", result);
return result;
}
}
Have I first to pass the value to main activity?
and why I get this error?
Please help me!
Thanks
EDIT
I have modify code, is now correct? because I do not see the writing in the TextView
Upvotes: 1
Views: 3608
Reputation: 677
If you have static method, you cann't call not static method. Static method "exists" without instance of class. Non static method is "part" of instance of class.
Pass View, in which you want to find View as parameter of the static method.
public static String send(String query, View view) {
....
view.findViewById(R.id.some_id);
....
}
Upvotes: 0
Reputation: 18873
You have declared this method to be static.
Your line of code
TextView text = (TextView) findViewById(R.id.textView10);
And this
findViewById(int id)
Is from the Activity class, which requires a instance of this class in order to use.
Solution:
What you can do is adding a parameter to your method like:
public static String send(String query,Activity yourActivity)
then use
TextView text = (TextView) yourActivity.findViewById(R.id.textView10);
Upvotes: 1