Question

So I set up a main_layout.xml shown below with 3 textviews which I then "setContentView" to the main_layout.xml from MainActivity.java as shown below. I then have methods which will be in Java.class (a seperate class in a seperate package) that won't be an activity but will have methods that will be called from MainActivity. The problem is I'm having trouble accessing any of these resources, each of the three findViewById's in Java.class get highlighted under in red and error states: The method findViewById(int) is undefined for the type Json.SnagInfo

What am I doing wrong?

main_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
    android:id="@+id/title"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/title" />
<TextView
    android:id="@+id/author"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/author" />
<TextView
    android:id="@+id/release"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/release"/>
</LinearLayout>

MainActivity.java

public class MainActivity extends Activity {
// Local Variables
Context context;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_layout);
    }
}

Json.java

public class Json {
TextView title;
TextView author;
TextView release;
ListView published;
ArrayList<HashMap<String, String>> bookList = new ArrayList<HashMap<String, String>>();

    public class SnagInfo extends AsyncTask<URL, Void, String> {
          protected void onPreExecute() {
                super.onPreExecute();

                bookList.clear();

                title = (TextView) findViewById(R.id.title);
                author = (TextView) findViewById(R.id.author);
                release = (TextView) findViewById(R.id.release);
            }
      }
}
Was it helpful?

Solution

findViewById is a method of Activity class. SnagInfo extends AsyncTask.

If you want to the result of background computation in the activity class you can use interface as a Callback.

Now initialize all your views in activity and set the data there.

Check this link

How do I return a boolean from AsyncTask?

You can also make asynctask an inner class of activity and update ui in onPostExecute

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top