我正在尝试重现这样的应用程序:http: //developer.android.com/training/animation/screen-slide.html所以片段将有一个 TextView 并且我试图对所有片段使用相同的布局并从 TextView.(1,2,3,4,5) 更改文本,但是当我 setText 时我遇到了崩溃。
package com.example.msixthapp;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.widget.TextView;
public class ScreenSlidePagerActivity extends FragmentActivity {
private Fragment a[] = new Fragment [10];
private static final int NUM_PAGES=5;
public static TextView txt;
private ViewPager viewPager;
private PagerAdapter pagerAdapter;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen_slide_pager);
viewPager = (ViewPager)findViewById(R.id.pager);
pagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(pagerAdapter);
for(int i=0;i<NUM_PAGES;i++){
a[i]= new ScreenSlidePageFragmen();
}
}
@Override
public void onBackPressed(){
if (viewPager.getCurrentItem() == 0) {
// If the user is currently looking at the first step, allow the system to handle the
// Back button. This calls finish() on this activity and pops the back stack.
super.onBackPressed();
} else {
// Otherwise, select the previous step.
viewPager.setCurrentItem(viewPager.getCurrentItem() - 1);
}
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter{
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return ScreenSlidePageFragmen.create(position);
}
@Override
public int getCount() {
return NUM_PAGES;
}
}
}
package com.example.msixthapp;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* A simple {@link android.support.v4.app.Fragment} subclass.
*
*/
public class ScreenSlidePageFragmen extends Fragment {
public static String ARG_PAGE="page";
private int mPageNumber;
public ScreenSlidePageFragmen() {
// Required empty public constructor
}
public static ScreenSlidePageFragmen create(int pageNumber) {
ScreenSlidePageFragmen fragmen = new ScreenSlidePageFragmen();
Bundle args = new Bundle();
args.putInt(ARG_PAGE, pageNumber);
fragmen.setArguments(args);
return fragmen;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPageNumber = getArguments().getInt(ARG_PAGE);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup)inflater.inflate(R.layout.fragment_screen_slide_page, container,false);
TextView txt = (TextView)rootView.findViewById(R.id.cont);
// txt.setText(mPageNumber+1);
// ((TextView)rootView.findViewById(R.id.content)).setText(mPageNumber+1);
TextView txte = new TextView(null);
txte.setText(mPageNumber);
rootView.addView(txte);
return rootView;
}
}
和布局
<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"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".ScreenSlidePagerActivity" >
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
<FrameLayout 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:id="@+id/container"
tools:context=".ScreenSlidePageFragmen" >
<TextView
android:id="@+id/cont"
android:gravity="center"
android:textSize="30sp"
android:text="tst"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>