在一个屏幕中,我正在使用两个视图器 - 一个用于图像自动架,另一个用于按钮手工滚动。Image AutoScrolling运行良好,但是当我在ViewPager上添加了按钮的实现时,我的应用程序开始在SetAdapter上为NullPointer崩溃时,我正在使用FragmentPagerAdapter。对于图像自动剪裁器,我正在使用此参考代码https://github.com/jakewharton/android-viewpagerindicator
这是我的源代码
布局xml文件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
我已经准确地指出了我的错误位置。
请帮助我解决此错误,谢谢。
yes it was because of different layout files
因此,如果buttonPager
为null,则设置了一个没有buttonPager
的布局。
这证实了我之前发表的评论。
buttonPager = (ViewPager)findViewById(R.id.pager_for_buttons);
buttonPager.setAdapter(buttonAdapter);
也检查此
http://developer.android.com/training/animation/screen-slide.html