Programmatically accessing a view that is defined in an XML snippet in Android

I am making an Android 3 app with tab-based navigation. Being completely new to Android, I was looking for tutorials for tabs and found several different approaches, of which the implementation based on Fragments + ActionBar seemed to be the best. I followed these tutorials.

http://www.abelski.com/courses/android3ui/actionbar.pdf (last slides) and http://www.youtube.com/watch?v=gMu8XhxUBl8 (same lesson in video format)

... and got to the point where the tabs are displayed, I can switch between them in order, and simple text elements inside them display well.

Now there are problems when I want one of the tabs to have a spinner widget that is defined in the fragment XML file and fills it with elements that are defined in the strings.xml file. When I try to access the counter using getViewById () from the onCreate () method, I always get a null value.

As I understand it, to fix this I need to somehow declare the XML fragment (similar to the way the main layout was done using setContentView (R.layout.main)), so getViewById () knows where to look for the spinner (Eclipse autocomplete finds it is already though). However, I’m fully on how to do this, since none of the tutorials have looked at a scenario where you want to change the contents of XML-based snippets inside the code.

The main.xml file is as follows:

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

tab1_fragment.xml like this:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Spinner android:id="@+id/spinner1" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </LinearLayout> 

and the onCreate method as follows:

 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ActionBar ab = getActionBar(); ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); ActionBar.Tab newAuditTab = ab.newTab().setText(R.string.tabheader_new_audit); Fragment newAuditFrag = new FragmentTabNewAudit(); newAuditTab.setTabListener(new TabListener(newAuditFrag)); ab.addTab(newAuditTab); //and the same for other tabs Spinner spinner = (Spinner) findViewById(R.id.spinner1); // this returns null ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource( this, R.array.cell_array, android.R.layout.simple_spinner_item); //cell_array is defined in strings.xml adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); spinner.setOnItemSelectedListener(new MyOnItemSelectedListener()); } 

Accessing the views defined in main.xml via R.id works fine, so in this case the problem is not related to Eclipse auto-class of class R, I think.

I suppose that I could define a spinner programmatically to get around the problem, but this probably will be a more elegant solution. I don’t see the large Android picture well enough to know exactly where to look.

+6
source share
1 answer

Since the counter is not a child of the Activity itself, findViewById cannot find it. Since the spinner is a child of the fragment, you can try:

 Spinner spinner = (Spinner)newAuditFrag.getView().findViewById(R.id.spinner1); 

However, I'm not sure when a view is created for the fragment, so getView() can also return null, which points to your code. Then you can try moving the code to onResume , which is called after onCreate .

+8
source

Source: https://habr.com/ru/post/898283/


All Articles