Reputation: 191
I have a main activity with TextViews
and class that want's modify the main activity's TextViews
.
I keep generating NullPointerException
errors in my findViewbyID
method in my class. I'm assuming because I'm not passing the View to the class method. How do I got about doing this?
Upvotes: 0
Views: 639
Reputation: 837
In your Activity , if you use setcontentView(R.layout.yourlayout)
then all the subsequent findViewById()
correspond to the views in yourlayout.xml
.
In the second activity your are trying to find views of the layout of the first activity, that is why you are getting the NullPointerException
.
Why do you want to manipulate views of this activity in another activity ? I suggest you manipulate the views in the first activity.
Upvotes: 0
Reputation: 132992
you will need to to pass Activity instance to your non Activity class using constructor to access all UI elements from non Activity Class as :
public class NonActivity{
Activity activity;
Context context;
public NonActivity(Activity activity,Context context) {
this.activity=activity;
this.context=context;
}
}
now you can access UI elements from Activity in NonActivity class as:
TextView textview=(TextView)activity.findViewbyID(R.id.textview);
Upvotes: 4