0

I am trying the apply this example to my project but I am getting the following error:

Error:(26, 33) error: cannot find symbol method findViewById(int)

This is how my code looks like:

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;
import android.os.AsyncTask;

import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView display = (TextView) findViewById(R.id.textView1);
        display.setText("Hello Android!");
        Connect myConnect = new Connect();
        myConnect.execute();
    }
}


class Connect extends AsyncTask<Void, Void, Integer> {

    TextView Text1 = (TextView) findViewById(R.id.textView1);

    @Override
    protected void onPreExecute(){
    }

    @Override
    protected Integer doInBackground(Void... params) {
        return 0;
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onPostExecute(0);
        Text1.setText("Text Changed");
    }
}
  • What am I missing here?
  • How can I reference the TextView through the AsyncTask?
Community
  • 1
  • 1
Kaguei Nakueka
  • 993
  • 2
  • 13
  • 34
  • make Text1 global and use settext in postExecute method – SaravInfern Jan 20 '17 at 10:28
  • setContentView(R.layout.activity_main); is part of MainActivity so you can create a reference of textview in MainActivity only for working code check readyandroid answer. – Ready Android Jan 20 '17 at 10:33
  • The question is not clear ... Is `Connect` an inner non static class of `MainActivity`? no, then obviously `AsyncTask` has no `findViewById` method – Selvin Jan 20 '17 at 10:47

4 Answers4

1

This is incorrect :

class Connect extends AsyncTask<Void, Void, Integer> {

    TextView Text1 = (TextView) findViewById(R.id.textView1);

findViewById is a method of Activity class. not AsyncTask

to fix it, use this:

Connect myConnect = new Connect(display);

and create constructor:

class Connect extends AsyncTask<Void, Void, Integer> {
   TextView Text1;
public Connect(TextView tv) {
Text1 = tv;
}

and you have got the other error:

 @Override
    protected void onProgressUpdate(Void... values) {
        super.onPostExecute(0);
        Text1.setText("Text Changed");
    }

change it into this:

 @Override
    protected void onProgressUpdate(Integer... values) {
      super.onProgressUpdate(values);
      Text1.setText("Text Changed");
    }

SUMMARISE //one file public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView display = (TextView) findViewById(R.id.textView1);
        display.setText("Hello Android!");
       Connect myConnect = new Connect(display);
        myConnect.execute();
    }
}

//possible the other file. but you can put the class below as nested - just move previous paranthesis to the end of the code class Connect extends AsyncTask { TextView Text1; public Connect(TextView tv) { Text1 = tv; } @Override protected void onPreExecute(){ }

        @Override
        protected Integer doInBackground(Void... params) {

        /*publish your progress here
          publishProgress(...);
        */
            return 0;
        }

        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
            Text1.setText("Text Changed");
        }
    }
Vyacheslav
  • 26,359
  • 19
  • 112
  • 194
  • It's a nested class. You don't need to pass it through the constructor. – Murat Karagöz Jan 20 '17 at 10:34
  • @MuratK. nope. You are not right. check his paranthesis:) Bu if it nested - you are right:) – Vyacheslav Jan 20 '17 at 10:35
  • If it would then he would not get compile time error – Selvin Jan 20 '17 at 10:55
  • @Selvin , hi can put this class in the other file. In practice it doesn't matter. Yes, it is possible to use this textview as 'shared' variable. But it is possible to pass this variable to another class. This is just and example. It really doesn't matter – Vyacheslav Jan 20 '17 at 10:58
  • I know that ... see my comments under other answer and the question ... I just confirm what you said ... that is seems to be not an inner non static class(because if it would his code would compile and working without problems - until recreation of Activity when AsyncTask would be running - but your code is not immune for this issue either - but the question is not about this and your answer would fix compile time problem) – Selvin Jan 20 '17 at 11:00
1

Your text view is related to your MainActivity so create the global reference in activity class only and use it for sub class also.

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;
import android.os.AsyncTask;

import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

TextView Text1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Text1 = (TextView) findViewById(R.id.textView1);
        Text1.setText("Hello Android!");
        Connect myConnect = new Connect();
        myConnect.execute();
    }



class Connect extends AsyncTask<Void, Void, Integer> {

    @Override
    protected void onPreExecute(){
    }

    @Override
    protected Integer doInBackground(Void... params) {
        return 0;
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onPostExecute(0);
        Text1.setText("Text Changed");
    }
}


}
Ready Android
  • 3,529
  • 2
  • 26
  • 40
-2

findViewById() is not accessible inside AsyncTask, the method belongs to activity, so you cannot initialize view using id inside Asynctask. Initialize your view in activity inside onCreate and use the instance in async task

public class MainActivity extends AppCompatActivity {
TextView display;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        display = (TextView) findViewById(R.id.textView1);
        display.setText("Hello Android!");
        Connect myConnect = new Connect();
        myConnect.execute();
    }


class Connect extends AsyncTask<Void, Void, Integer> {


    @Override
    protected void onPreExecute(){
    }

    @Override
    protected Integer doInBackground(Void... params) {
        return 0;
    }

    @Override
    protected void onProgressUpdate(Void... values) {


    }
    protected void onPostExecute(Long result) {
    display.setText("Text Changed");
 }
}}
SaravInfern
  • 3,338
  • 1
  • 20
  • 44
  • Downvoted as your answer doesn't explain what's wrong with the code snippet in question and how your version solves the problem. – Egor Jan 20 '17 at 10:31
-2

Use your view inside onCreate(). onProgressUpdate() will be called on the main application thread (a.k.a., UI thread). As said by CommansWare

Global

private TextView Text1;

use as:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TextView display = (TextView) findViewById(R.id.textView1); 
    Text1 = (TextView) findViewById(R.id.textView1); //here
       .........

Set as it is

@Override
protected void onProgressUpdate(Void... values) {
    super.onPostExecute(0);
    Text1.setText("Text Changed");
}
Community
  • 1
  • 1
W4R10CK
  • 5,502
  • 2
  • 19
  • 30
  • obviously field initializer would be called on Main thread ... it is the compile time error not connected with UI thread – Selvin Jan 20 '17 at 10:33
  • @Selvin no dude, its a working example that i implemented and said here – W4R10CK Jan 20 '17 at 10:35
  • Dude, it is compile time error: `Error:(26, 33) error: cannot find symbol method findViewById(int)` It has nothing to do with UI thread (if `Connect` would be inner not static class of activity it would work) – Selvin Jan 20 '17 at 10:43
  • also `private TextView Text1;` doesn't means global ... moving findViewById to onCreate doesn't make sens as it seems like **Conect is not an inner non static class** , so it would not fix this error(but if it would inner class then his code would [compile](http://ideone.com/H8yQz9)) – Selvin Jan 20 '17 at 10:50