1

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10dp"
android:background="@drawable/gradientbg" >

<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/viewPager"/>

</LinearLayout>

home.xml

 <TextView
 android:id="@+id/gpsStatus"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_marginTop="10dp"
 android:layout_marginRight="10dp"
 android:layout_marginBottom="10dp"
 android:layout_marginLeft="2dp" />

my main activity

TextView gpsStatus = (TextView)findViewById(R.id.gpsStatus); // gpsStatus = null
gpsStatus.setText("foo");

This will result in a nullpointerexception.

LayoutInflater inflater = getLayoutInflater();
View homeView = inflater.inflate(R.layout.home, null);
TextView gpsStatus = (TextView)homeView.findViewById(R.id.gpsStatus);
gpsStatus.setText("foo");

This wont crash the code, but it wont change my text either.

So how do i find and manipulate controls that arent located in my main.xml?

Thanks

Johan
  • 35,120
  • 54
  • 178
  • 293
  • @user370305 `.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView` – Johan Jul 07 '12 at 17:04
  • Is your Home.xml file contains more views or only TextView? – user370305 Jul 07 '12 at 17:05
  • @user370305 A linearlayout with the textview in it. – Johan Jul 07 '12 at 17:06
  • Ok, so now which Activity displayed when you are trying to change the text of TextView? Is TextView Visible on screen? – user370305 Jul 07 '12 at 17:09
  • @user370305 Yes its visible through the `ViewPager`. But its the `main.xml` which is showed through, and i think thats the problem. http://mobile.tutsplus.com/tutorials/android/android-user-interface-design-horizontal-view-paging/ – Johan Jul 07 '12 at 17:13
  • @user370305 i included my other view in my main view and it worked. Thanks though :) – Johan Jul 07 '12 at 17:36

2 Answers2

1

This is because your home.xml does not exist in your main activity when you are calling findViewById. Once your home.xml layout file is inflated into your main activity, findViewById should work.

findViewById only works for IDs that are under the current view hierarchy. By calling findViewById on your inflated view, you are checking the view hierarchy specifically on the layout object you created.

If you add your home.xml layout to a view inside your main activity, it will be added to your activity's view hierarchy, and then your findViewById and setText calls will work.

WindyB
  • 786
  • 3
  • 9
  • Ok, and how would i add it to a view inside my main activity? – Johan Jul 07 '12 at 17:26
  • There are many different ways. You can directly include home.xml into your main.xml layout (see http://developer.android.com/training/improving-layouts/reusing-layouts.html or http://stackoverflow.com/questions/8834898/use-of-android-xml-merge-tag). Or, you can use your layout inflater to inflate your view, then add it to a view group. – WindyB Jul 07 '12 at 17:30
  • Alternatively, you can also use a PagerAdapter to load your views into your ViewPager. See http://developer.android.com/reference/android/support/v4/view/PagerAdapter.html for details. – WindyB Jul 07 '12 at 17:31
  • Additional question: How would i know in my main activity when the home.xml has been inflated? Checking it just after creating an instance of `MyPagerAdapter` doesnt seems to be enough. This is the code im using: http://mobile.tutsplus.com/tutorials/android/android-user-interface-design-horizontal-view-paging/ – Johan Jul 07 '12 at 18:14
1

So how do i find and manipulate controls that arent located in my main.xml?

well in main.xml

<LinearLayout 
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/aLyoutWillbeAddedIfruqired"
 >
</LinearLayout>

and in you activity do something like...

LinearLayout statusLayout= (LinearLayout )findViewById(R.id.aLyoutWillbeAddedIfruqired);

and

LayoutInflater inflater = getLayoutInflater();
View homeView = inflater.inflate(R.layout.home, null);
TextView gpsStatus = (TextView)homeView.findViewById(R.id.gpsStatus);
gpsStatus.setText("foo");
statusLayout.addView(homeView);

I hope it will help you...

Mohsin Naeem
  • 12,542
  • 3
  • 39
  • 53