0

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

4

2 回答 2

0

问题是您试图获取在不是设置的 contentView (activity_main) 的布局中声明的视图。

您可以尝试为您的 page_test 增加 View 并以这种方式获取您的 textview,如下所示:

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");
于 2013-04-11T14:21:42.097 回答
0

视图都使用相同的布局应该不是问题。如果您使用的是,ViewPager您应该能够获得View正在显示的内容(或您想要的内容),然后修改对该特定视图的TextView调用值。findViewById例如,要更改TextView当前正在显示的视图中的 a:

获取具有要更改的 TextView 的视图

View v = MainActivity.myPager.getChildAt(position) //position = index of view in pager

在该视图中找到所需的 TextView

TextView nameToChange = (TextView) v.findViewById(R.id.name);

然后你应该能够使用更改文本

nameToChange.setText(newName);
于 2013-04-11T13:41:39.143 回答