在一个屏幕中,我使用两个viewPagers - 一个用于图像自动滚动,另一个用于按钮手动滚动。图像自动滚动工作得很好,但是当我在viewPager上添加了按钮的实现时,我的应用程序开始在setAdapter上为nullpointer崩溃,我正在使用fragmentPagerAdapter。 对于图像autoslider我使用此参考代码 https://github.com/JakeWharton/Android-ViewPagerIndicator
这是我的源代码
layout xml file simple_circles.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- android:layout_weight="0.01" -->
<SearchView
android:id="@+id/search_view"
android:layout_width="fill_parent"
android:layout_height="80dp" >
</SearchView>
<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="400dp">
</android.support.v4.view.ViewPager>
<android.support.v4.view.ViewPager
android:id="@+id/pager_for_buttons"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</android.support.v4.view.ViewPager>
用于在滑块上添加按钮的片段 -
public class ScreenSlidePageFragment extends Fragment {
public static final String ARG_PAGE = "page";
private int mPageNumber;
public static Fragment create(int pageNumber) {
ScreenSlidePageFragment fragment = new ScreenSlidePageFragment();
Bundle args = new Bundle();
args.putInt(ARG_PAGE, pageNumber);
fragment.setArguments(args);
return fragment;
}
public ScreenSlidePageFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPageNumber = getArguments().getInt(ARG_PAGE);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout containing a title and body text.
Button music_button = new Button(getActivity());
music_button.setText("Music");
Button news_button = new Button(getActivity());
news_button.setText("News");
Button videos_button = new Button(getActivity());
videos_button.setText("Videos");
Button photos_button = new Button(getActivity());
photos_button.setText("Photos");
LinearLayout layout = new LinearLayout(getActivity());
layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
//layout.setGravity(Gravity.CENTER);
layout.addView(music_button);
layout.addView(news_button);
layout.addView(videos_button);
layout.addView(photos_button);
return layout;
}
public int getPageNumber() {
return mPageNumber;
}
}
viewPager的活动类和适配器 -
public class SampleCirclesDefault extends FragmentActivity {
// Initialization
TestFragmentAdapter mAdapter;
ViewPager mPager;
PageIndicator mIndicator;
Bitmap bitmap;
ArrayList<String> bandImagesList;
ImageView build_bandapp_img;
boolean pagerMoved;
Runnable Update;
Handler h;
long ANIM_VIEWPAGER_DELAY;
Handler handler;
int NUM_PAGES = 5;
ViewPager buttonPager;
ScreenSlidePagerAdapter buttonAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// No action bar
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
**setContentView(R.layout.simple_circles);**
build_bandapp_img = (ImageView) findViewById(R.id.imageView_for_webview);
build_bandapp_img.setOnClickListener(new OnClickListener() {
// Build bandapp button
@Override
public void onClick(View v) {
Intent i = new Intent(SampleCirclesDefault.this,
BandDetailedActivity.class);
startActivity(i);
}
});
mAdapter = new TestFragmentAdapter(getSupportFragmentManager());// adapter
// for
// viewpager
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
buttonAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
//buttonAdapter = new AdapterForFragmentOfButtonSlider(getSupportFragmentManager());
buttonPager = (ViewPager)findViewById(R.id.pager_for_buttons);
buttonPager.setAdapter(buttonAdapter); //***error -NullPointerException***//
mIndicator = (CirclePageIndicator) findViewById(R.id.indicator);
mIndicator.setViewPager(mPager);// dot dot indicator
buttonPager.setCurrentItem(1);
handler = new Handler();
// autoscrolling of view pager
Update = new Runnable() {
int currentPage = mPager.getCurrentItem();
public void run() {
if (currentPage == NUM_PAGES - 1) {
currentPage = 0;
}
mPager.setCurrentItem(currentPage++, true);
}
};
Timer swipeTimer = new Timer();
swipeTimer.schedule(new TimerTask() {
@Override
public void run() {
handler.post(Update);
}
}, 100, 5000);
}
private class ScreenSlidePagerAdapter extends FragmentPagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
@Override
public Fragment getItem(int position) {
return ScreenSlidePageFragment.create(position);
}
@Override
public int getCount() {
return NUM_PAGES;
}
}
}
堆栈跟踪
01-23 13:56:57.067: W/dalvikvm(18861): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
01-23 13:56:57.098: E/AndroidRuntime(18861): FATAL EXCEPTION: main
01-23 13:56:57.098: E/AndroidRuntime(18861): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.viewpagerindicator.sample/com.viewpagerindicator.sample.SampleCirclesDefault}: java.lang.NullPointerException
01-23 13:56:57.098: E/AndroidRuntime(18861): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-23 13:56:57.098: E/AndroidRuntime(18861): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-23 13:56:57.098: E/AndroidRuntime(18861): at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-23 13:56:57.098: E/AndroidRuntime(18861): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-23 13:56:57.098: E/AndroidRuntime(18861): at android.os.Handler.dispatchMessage(Handler.java:99)
01-23 13:56:57.098: E/AndroidRuntime(18861): at android.os.Looper.loop(Looper.java:137)
01-23 13:56:57.098: E/AndroidRuntime(18861): at android.app.ActivityThread.main(ActivityThread.java:5041)
01-23 13:56:57.098: E/AndroidRuntime(18861): at java.lang.reflect.Method.invokeNative(Native Method)
01-23 13:56:57.098: E/AndroidRuntime(18861): at java.lang.reflect.Method.invoke(Method.java:511)
01-23 13:56:57.098: E/AndroidRuntime(18861): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-23 13:56:57.098: E/AndroidRuntime(18861): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-23 13:56:57.098: E/AndroidRuntime(18861): at dalvik.system.NativeStart.main(Native Method)
01-23 13:56:57.098: E/AndroidRuntime(18861): Caused by: java.lang.NullPointerException
01-23 13:56:57.098: E/AndroidRuntime(18861): at com.viewpagerindicator.sample.SampleCirclesDefault.onCreate(SampleCirclesDefault.java:85)
01-23 13:56:57.098: E/AndroidRuntime(18861): at android.app.Activity.performCreate(Activity.java:5104)
01-23 13:56:57.098: E/AndroidRuntime(18861): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-23 13:56:57.098: E/AndroidRuntime(18861): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-23 13:56:57.098: E/AndroidRuntime(18861): ... 11 more
01-23 13:57:13.356: I/Process(18861): Sending signal. PID: 18861 SIG: 9
我已准确指出了我收到错误的地方。
请提前帮助我解决此错误。
答案 0 :(得分:1)
yes it was because of different layout files
因此,如果buttonPager
为空,则您设置的布局没有buttonPager
。
这证实了我之前评论的内容。
buttonPager = (ViewPager)findViewById(R.id.pager_for_buttons);
buttonPager.setAdapter(buttonAdapter);
同时检查此
http://developer.android.com/training/animation/screen-slide.html