Search code examples
androidandroid-fragmentsnullpointerexceptionandroid-viewpagersettext

How could I setText in my activity of a textView in common layout for a ViewPager in Android?


Update of the code: my problem is a nullPointerExeption on the line :

test.setText("try");

because the line

test=(TextView)myPager.findViewById(R.id.txtPagerDate);

return a null view

My code:

MainMeteo.java :

public class MainMeteo extends FragmentActivity {

    ViewPager myPager;
    ViewPagerAdapterBis myPagerAdapter;
    TextView test;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_meteo);
        myPagerAdapter = new ViewPagerAdapterBis(getSupportFragmentManager());
        myPager=(ViewPager)findViewById(R.id.panelpager);
        myPager.setAdapter(myPagerAdapter);


        test=(TextView)myPager.findViewById(R.id.txtPagerDate);
        test.setText("try");

    }

}

ViewAdapterBis.java:

    public class ViewPagerAdapterBis extends FragmentStatePagerAdapter {
    public ViewPagerAdapterBis(FragmentManager fm) {
        super(fm);
        // TODO Auto-generated constructor stub
    }
    @Override
    public Fragment getItem(int i) {
        Fragment fragment = new ViewPagerAdapterObject();
        Bundle args = new Bundle();
        // Our object is just an integer :-P
        args.putInt(ViewPagerAdapterObject.ARG_OBJECT, i + 1);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public int getCount() {
        return 15;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return "OBJECT " + (position + 1);
    }


}

ViewPagerAdapterObject.java

public class ViewPagerAdapterObject extends Fragment {

    public static final String ARG_OBJECT = "object";

    @Override
    public View onCreateView(LayoutInflater inflater,
            ViewGroup container, Bundle savedInstanceState) {
        // The last two arguments ensure LayoutParams are inflated
        // properly.
        View rootView = inflater.inflate(R.layout.meteo_pager, container, false);
        return rootView;
    }
}

And my two xml layout:

meteo_pager.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/txtPagerDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="4dp"
        android:text="Date az zaza"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/txtPagerMaxMin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/txtPagerDate"
        android:layout_marginBottom="1dp"
        android:layout_marginTop="1dp"
        android:text="Max | Min"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/txtPagerPrecipitations"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/txtPagerMaxMin"
        android:layout_marginBottom="1dp"
        android:layout_marginTop="1dp"
        android:text="Précipitations"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/txtPagerVent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/txtPagerPrecipitations"
        android:layout_marginBottom="1dp"
        android:layout_marginTop="1dp"
        android:text="Vent"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/txtPagerNeige"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/txtPagerVent"
        android:layout_marginBottom="1dp"
        android:layout_marginTop="1dp"
        android:text="Neige"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/txtPagerDate"
        android:src="@android:drawable/spinner_background" />

</RelativeLayout>

main_meteo.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    tools:context=".MainMeteo" >

    <TextView
        android:id="@+id/txtPays"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/txtVille"
        android:layout_centerHorizontal="true"
        android:layout_margin="3dp"
        android:text="Pays"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/txtPagerDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/txtPays"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="5dp"
        android:text="Actuellement" />

    <TextView
        android:id="@+id/txtPagerVent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/txtPagerDate"
        android:layout_below="@+id/txtPagerDate"
        android:layout_margin="2dp"
        android:text="Vent" />

    <TextView
        android:id="@+id/txtHumidite"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/txtPagerVent"
        android:layout_margin="2dp"
        android:text="Humidité" />

    <TextView
        android:id="@+id/txtMoyenneTemps"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/txtVisibilite"
        android:layout_margin="2dp"
        android:text="Mostly" />

    <TextView
        android:id="@+id/txtVisibilite"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/txtcouvNuage"
        android:layout_margin="2dp"
        android:text="Visibilité" />

    <TextView
        android:id="@+id/txtcouvNuage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/txtPrecipitation"
        android:layout_margin="2dp"
        android:text="Couv. nuage" />

    <TextView
        android:id="@+id/txtTemperature"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/txtPagerVent"
        android:text="x°"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/txtRessentie"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/txtTemperature"
        android:text="Ressentie"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/txtPrecipitation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/txtHumidite"
        android:layout_margin="2dp"
        android:text="Precipitation" />

    <TextView
        android:id="@+id/txtVille"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="2dp"
        android:text="Ville"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/txtPagerMaxMin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/txtMoyenneTemps"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="5dp"
        android:text="15 prochains jours" />

    <android.support.v4.view.ViewPager
        android:id="@+id/panelpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/txtPagerMaxMin" >

    </android.support.v4.view.ViewPager>

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/txtPagerMaxMin"
        android:layout_alignLeft="@+id/txtRessentie"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/txtRessentie"
        android:src="@android:drawable/progress_indeterminate_horizontal" />

</RelativeLayout>

If you need more information, just ask me.

the first question before update :

http://www.sendspace.com/file/npf0q8


Solution

  • The problem is you are trying to get a view that is declared in a layout that is not the contentView that was set (activity_main).

    You can try inflating the View for your page_test and grab your textview that way, like so:

    LayoutInflater inflater =(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflater.inflate(R.layout.meteo_pager, null);
    test = (TextView)v.findViewById(R.id.txtPagerDate);
    test.setText("try");