Reputation: 25
I have an app that builds a bundle and passes it to a second activity, for the data within the bundle to be used later.
So far I just want to display one of the elements in the bundle in a TextView
, to be sure that I can handle the data in activity two. I'm calling getView().findViewByID(R.id.theTextViewIWant)
, but it is always returning null
, and the IDE says it cannot resolve getView()
. I believe it has got to be something I don't fully understand about the View
that holds the second activity, so I'd appreciate any help.
public class MyClass extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mark_student);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
@Override
protected void onStart(){
super.onStart();
Bundle receivedInfo = getIntent().getExtras();
String unitSelected = receivedInfo.getString("key");
TextView textViewBox = (TextView) getView().findViewById(R.id.textView2);
textViewBox.setText(unitSelected);
}
I've tried these two other ways of getting the view object, and they haven't worked either:
ViewGroup rootView = (ViewGroup) ((ViewGroup) this.findViewById(android.R.id.content)).getChildAt(0); //nope
View fragmentView = getView(); //neither
Upvotes: 2
Views: 719
Reputation: 9
The getView() is a fragment method. You need to use findViewById() in the activity to initialize the view. Also move the initialization code to onCreate method and process the intent there as well.
Upvotes: 0
Reputation: 20626
I'm calling getView().findViewByID(R.id.theTextViewIWant), but it is always returning null, and the IDE says it cannot resolve getView()
I recommend to you to read getView()
method documentation, but there's a small explanation
Get a View that displays the data at the specified position in the data set.
So if you want to find a view that's not on a data set, for instance in your example TextView
then you want to use findViewById(int)
Finds a view that was identified by the android:id XML attribute that was processed in onCreate(Bundle).
As it says in the documentation I recommend you to put it on onCreate()
method, instead of onStart()
.
Then you have to remove the getView()
and do it like :
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourLayoutFromActivity2);
TextView textViewBox = (TextView) findViewById(R.id.textView2);
}
Note : if you put your stuff in onStart()
it means that every-time the app come from foreground will execute all of you have inside of it. I also recommend to you to take a look at Android life cycle
Upvotes: 1
Reputation: 15155
The IDE is telling you getView()
is not an Activity
method (it is however a Fragment
method). In an Activity
you can simply call findViewById()
.
TextView textViewBox = (TextView) findViewById(R.id.textView2);
Upvotes: 1