-1

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);
            }
      }
}
Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97
user3117785
  • 247
  • 5
  • 16
  • You need to extend the Activity in java class,also you cannot able to set the text in textview without specifying respective layout – Kalai.G Dec 19 '13 at 05:25
  • I'm not sure what you mean by extend the activity in java class, which java class? the Json class? And why can't I set the TextView? How would I specify the respective layout? Sorry I'm new. – user3117785 Dec 19 '13 at 05:27
  • Raghunandan answered you clearly. Extending the Activity is just like UI for user. You need to learn the basics. use java class and write some functions.call those functions from your activity and use those return values for setting the text in your activity. Don't use any views inside java class. Learn from here http://developer.android.com/reference/android/app/Activity.html – Kalai.G Dec 19 '13 at 05:33

1 Answers1

1

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

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • Okay but if I were to put that public class SnagInfo and textview variables into my MainActivity, everything would work fine. I'm trying to organize my code into seperate classes, so how would I go about doing this? – user3117785 Dec 19 '13 at 05:15
  • @user3117785 check this http://stackoverflow.com/questions/16752073/how-do-i-return-a-boolean-from-asynctask – Raghunandan Dec 19 '13 at 05:18
  • Not really sure that's the same situation. I'm just trying to access the TextViews on my MainActivity Class (that's initially in my xml) from my Json.java class. – user3117785 Dec 19 '13 at 05:28
  • @user3117785 it is not the same situation. but you can't initialize textview in asynctask like. I believe you want to do some computation is asynctask get json. now you wish to set the json parsed values to textview right? For that i suggested using interface as a callback. The same as returning boolean value from asynctask in the link – Raghunandan Dec 19 '13 at 05:31